เป็นการวาดรูปต้นไม้ binary search tree ครับ
class Tree node
import java.awt.Color;
import java.awt.Graphics;
public class TreeNode {
public int data;
public TreeNode left, right;
public TreeNode() {
}
public TreeNode(int x) {
data = x;
}
public void draw(Graphics g, int x, int y) {
g.setColor(Color.pink);
g.fillOval(x, y, 40, 40);
g.setColor(Color.black);
g.drawString("" + data, x + 15, y + 23);
}
}
class BSTree
import java.awt.Graphics;
public class BSTree {
public TreeNode root;
public void add(int x) {
if (root == null) {
root = new TreeNode(x);
return;
}
add(x, root);
}
public void add(int x, TreeNode n) {
if (n == null)
return;
if (n.data == x)
return;
if (n.data < x) {
if (n.right == null) {
n.right = new TreeNode(x);
} else {
add(x, n.right);
}
} else {
if (n.left == null) {
n.left = new TreeNode(x);
} else {
add(x, n.left);
}
}
}
public int numNode(TreeNode n) {
if (n == null)
return 0;
int k = 0;
if (n.left != null) {
k = k + numNode(n.left);
}
if (n.right != null) {
k = k + numNode(n.right);
}
return k + 1;
}
public int height(TreeNode n) {
if (n == null)
return -1;
if (n.left == null && n.right == null) {
return 0;
}
int kl = 0;
int kr = 0;
if (n.left != null) {
kl = height(n.left);
}
if (n.right != null) {
kr = height(n.right);
}
return kl > kr ? kl + 1 : kr + 1;
}
int stepX;
int stepY;
public void draw(Graphics g, int width, int height) {
int num = numNode(root);
int h = height(root);
stepX = width / (num + 1);
stepY = height / (h + 1);
draw(g, 1, num, 1, root,-1,-1);
}
public int draw(Graphics g, int start_x, int end_x, int height, TreeNode t,int x_mom ,int y_mom) {
int num_l = numNode(t.left);
int x_now=(num_l + start_x) * stepX;
int y_now= height * stepY;
t.draw(g, x_now, y_now);
if (t.left != null) {
draw(g, start_x, start_x + num_l - 1, height + 1, t.left,x_now, y_now);
}
if (t.right!= null) {
draw(g, start_x + num_l + 1, end_x, height + 1, t.right,x_now, y_now);
}
if(x_mom != -1 && y_mom !=-1)
{
g.drawLine(x_mom+15, y_mom+15, x_now+15, y_now+15);
}
return 0;
}
}
call GUI
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI extends JPanel {
BSTree t;
public GUI() {
t = new BSTree();
for (int i = 0; i < 100; i++) {
int k=(int) (Math.random() * 100);
t.add(k);
System.out.println(k);
}
System.out.println(t.height(t.root));
JFrame f = new JFrame();
f.add(this);
f.setSize(600, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
if(t!=null){
t.draw(g, this.getWidth(), this.getHeight());
}
}
public static void main(String[] args) {
new GUI();
}
}
** ดัดแปลงจากในหนังสือของอาจารย์สมชาย โครงสร้างข้อมูล : ฉบับวาจาจาวา (สมชาย ประสิทธิ์จูตระกูล)**
[ view entry ] ( 21093 views ) | permalink | ( 3 / 2499 )
ยังไม่เสร็จสมบูรณ์เป็นโจทย์ให้นร.ไปทำต่อนะครับ
Public Class Form1
Dim a(5, 5) As Double
Dim colors(5, 5) As Color
Dim rand As Random = New Random
Dim ballsize As Integer = 20
Dim x, y, vx, vy As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 0 To a.GetUpperBound(0)
For j = 0 To a.GetUpperBound(0)
a(i, j) = 1
colors(i, j) = Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255))
Next
Next
Me.DoubleBuffered = True
x = 300
y = 300
vx = rand.NextDouble * 20 - 10
vy = rand.NextDouble * 20 - 10
Timer1.Start()
Timer1.Interval = 10
End Sub
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
Dim w, h As Integer
w = Me.Width
h = 80
For i = 0 To a.GetUpperBound(0)
For j = 0 To a.GetUpperBound(0)
If a(i, j) = 1 Then
e.Graphics.FillRectangle(New SolidBrush(colors(i, j)), _
CInt(j * w / a.GetLength(1)), _
CInt(i * h / a.GetLength(0)), _
CInt(w / a.GetLength(1)), _
CInt(h / a.GetLength(0)))
End If
Next
Next
e.Graphics.FillEllipse(New SolidBrush(Color.Red), _
CInt(x - ballsize), _
CInt(y - ballsize), _
CInt(ballsize * 2), _
CInt(ballsize * 2))
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
Refresh()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim w, h As Integer
w = Me.Width
h = 80
x = x + vx
y = y + vy
If x + ballsize > Me.Width Then
x = Me.Width - ballsize
vx = vx * -1
End If
If y + ballsize > Me.Height Then
y = Me.Height - ballsize
vy = vy * -1
End If
If x - ballsize < 0 Then
x = 0 + ballsize
vx = vx * -1
End If
If y - ballsize < 0 Then
y = 0 + ballsize
vy = vy * -1
End If
For i = 0 To a.GetUpperBound(0)
For j = 0 To a.GetUpperBound(0)
If a(i, j) = 1 Then
If x - ballsize < j * w / a.GetLength(1) + w / a.GetLength(1) And _
x - ballsize > j * w / a.GetLength(1) And _
y - ballsize < i * h / a.GetLength(0) + h / a.GetLength(0) And _
y - ballsize > i * h / a.GetLength(0) _
Then
a(i, j) = 0
vy = vy * -1
End If
End If
Next
Next
Refresh()
End Sub
End Class
[ view entry ] ( 2603 views ) | permalink | ( 3 / 2278 )
โจทย์การบ้าน นักเรียน ม.3
-------------
โรงแรมในฝัน (Hotel)
โรงแรมแคนทารีฮิลล์เป็นโรงแรมขนาดใหญ่ซึ่งมีจํานวนห้องพักไม่จํากัด โดย มีรายละเอียดประเภทห้องพัก
และราคาที่พักดังตารางต่อไปนี้
ประเภทของห้อง จํานวนคนที่พักมากที่สุดต่อห้อง ราคาต่อห้อง(บาท)
ห้องเดี่ยว 1 500
ห้องคู่ 2 800
ห้องกลาง 5 1,500
ห้องพักรวม 15 3,000
งานของคุณ
ให้เขียนโปรแกรมในการคํานวณหาห้องพักให้กับคนที่ต้องการเข้าพักจํานวน n คน โดยให้มีราคารวมของห้องพักต่ําที่สุด
ข้อมูลนําเข้า
มีหนึ่งบรรทัด เป็นจํานวนเต็มบวกที่เป็นค่าของ n โดยที่1 ≤ n ≤ 1,000,000
ข้อมูลส่งออก
มีหนึ่งบรรทัด เป็นจํานวนเต็มบวกที่เป็นราคารวมของห็องพักต่ำที่สุด
ตัวอย่างที่ 1
ข้อมูลนําเข้า
21
ข้อมูลส่งออก
5000
ตัวอย่างที่ 2
ข้อมูลนําเข้า
24
ข้อมูลส่งออก
6000าเข้า
24
ข้อมูลส่งออก
6000
idea (อาจจะไม่ใช่วิธีที่ดีที่สุดนะคราาาาบ)
#include <stdio.h>
#include <conio.h>
void main()
{
int price[4] = {500,800,1500,300};
int num[4] = {1,2,5,15};
int data[1000][4];
int data_price[1000];
data[0][0] =0; data[0][1] =0; data[0][2] =0; data[0][3] =0; data_price[0]=0;
data[1][0] =1; data[1][1] =0; data[1][2] =0; data[1][3] =0; data_price[1]=500;
data[2][0] =0; data[2][1] =2; data[2][2] =0; data[2][3] =0; data_price[2]=800;
data[3][0] =1; data[3][1] =2; data[3][2] =0; data[3][3] =0; data_price[3]=1300;
data[4][0] =0; data[4][1] =0; data[4][2] =4; data[4][3] =0; data_price[4]=1500;
data[5][0] =0; data[5][1] =0; data[5][2] =5; data[5][3] =0; data_price[5]=1500;
data[6][0] =1; data[6][1] =0; data[6][2] =5; data[6][3] =0; data_price[6]=2000;
data[7][0] =0; data[7][1] =2; data[7][2] =5; data[7][3] =0; data_price[7]=2300;
data[8][0] =1; data[8][1] =2; data[8][2] =5; data[8][3] =0; data_price[8]=2800;
data[9][0] =0; data[9][1] =0; data[9][2] =0; data[9][3] =9; data_price[9]=3000;
data[10][0] =0; data[10][1] =0; data[10][2] =0; data[10][3] =10; data_price[10]=3000;
data[11][0] =0; data[11][1] =0; data[11][2] =0; data[11][3] =11; data_price[11]=3000;
data[12][0] =0; data[12][1] =0; data[12][2] =0; data[12][3] =12; data_price[12]=3000;
data[13][0] =0; data[13][1] =0; data[13][2] =0; data[13][3] =13; data_price[13]=3000;
data[14][0] =0; data[14][1] =0; data[14][2] =0; data[14][3] =14; data_price[14]=3000;
data[15][0] =0; data[15][1] =0; data[15][2] =0; data[15][3] =15; data_price[15]=3000;
for(int i=16;i< 100;i++)
{
int min_index= 0;
int min= 2147483647 ;
for(int j=1;j<= i/2;j++)
{
int sum = data_price[j] + data_price[i-j];
if(sum < min)
{
min = sum;
min_index = j;
}
}
data_price = min;
data[0] = data[i-min_index][0]+data[min_index][0];
data[1] = data[i-min_index][1]+data[min_index][1];
data[2] = data[i-min_index][2]+data[min_index][2];
data[3] = data[i-min_index][3]+data[min_index][3 ];
}
for(int i=0;i< 100;i++)
{
printf(" = %d:\t" ,i );
for(int j=0;j< 4;j++)
{
printf("%d\t" ,data[j] );
}
printf(" = %d\n" ,data_price );
}
getch();
}
[ view entry ] ( 1570 views ) | permalink | ( 3.1 / 77 )
Code นี้เขียนโดย นร.ของเรา คนหนึ่ง ปีหนึ่งจาก ม.มหิดล
STEP 1
load Lib. จาก http://www.sfml-dev.org/
STEP 2
config Visual Studio ตามนี้เลย
http://www.sfml-dev.org/tutorials/2.1/start-vc.php
STEP 3
ตัวอย่าง Code
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <stdio.h>
int main()
{
printf("\n");
printf("\n");
printf("\n ************* ******** ********* **** ***** *******");
printf("\n *************** ******** *********** ***** ***** ***********");
printf("\n ****** *** **** ************* ****** ***** *************");
printf("\n ****** *** **** ************** ******* ***** **** ****");
printf("\n ****** *** **** ************** ******** ***** *** ***");
printf("\n ****** *** **** ***** ***** ********* ***** ** **");
printf("\n ****** *** **** ***** ***** ********** ***** ** **");
printf("\n ***************** **** ***** ***** **************** ** **");
printf("\n *************** **** ************** ****** ******* ** **");
printf("\n ****** **** ************** ****** ****** ** **");
printf("\n ****** **** ***** ***** ***** ***** *** ***");
printf("\n ****** **** ***** ***** ***** **** **** ****");
printf("\n ****** **** ***** ***** ***** *** *************");
printf("\n ****** ******** ***** ***** ***** ** ***********");
printf("\n **** ******** **** **** *** * *******");
printf("\n");
sf::RenderWindow window(sf::VideoMode(500, 200), "SFML works!");
sf::RectangleShape rect[15];
for(int i=0;i< 15 ; i++)
{
rect.setSize (sf::Vector2f(20, 120));
rect.setPosition (20 + i * 22,50);
rect.setFillColor(sf::Color::White);
}
sf::RectangleShape rect2[14];
for(int j=0;j< 14 ; j++)
{
rect2[j].setSize (sf::Vector2f(16, 57));
rect2[j].setPosition (20 + j * 22+13,50);
rect2[j].setFillColor(sf::Color(255,50,100));
}
window.setKeyRepeatEnabled(false);
sf::SoundBuffer buffer[15];
sf::Sound sound[15];
for(int i=0;i<15 ; i++)
{
char temp [30];
sprintf (temp,"Notes\\%d.wav",i+1);
//printf("loading file %s ...\n",temp);
if (!buffer.loadFromFile(temp))
return -1;
sound.setBuffer(buffer);
}
int a[15];
for(int k=0;k<15;k++)
{
a[k]=0;
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
///////////////////////////////////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='a')
{
if(a[0] == 0)
{
sound[0].play(); // 1
printf("0");
rect[0].setFillColor(sf::Color(50,50,50));
}
a[0] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::A)
{
a[0] = 0;
rect[0].setFillColor(sf::Color::White );
}
}
/////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='s')
{
if(a[1] ==0)
{
sound[1].play(); // 2
printf("0");
rect[1].setFillColor(sf::Color(50,50,50));
}
a[1] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::S)
{
a[1] = 0;
rect[1].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='d')
{
if(a[2] ==0)
{
sound[2].play(); // 3
printf("0");
rect[2].setFillColor(sf::Color(50,50,50));
}
a[2] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::D)
{
a[2] = 0;
rect[2].setFillColor(sf::Color::White );
}
}
/////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='f')
{
if(a[3] ==0)
{
sound[3].play(); // 4
printf("0");
rect[3].setFillColor(sf::Color(50,50,50));
}
a[3] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::F)
{
a[3] = 0;
rect[3].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='g')
{
if(a[4] ==0)
{
sound[4].play(); // 5
printf("0");
rect[4].setFillColor(sf::Color(50,50,50));
}
a[4] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::G)
{
a[4] = 0;
rect[4].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='h')
{
if(a[5] ==0)
{
sound[5].play(); // 6
printf("0");
rect[5].setFillColor(sf::Color(50,50,50));
}
a[5] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::H)
{
a[5] = 0;
rect[5].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='j')
{
if(a[6] ==0)
{
sound[6].play(); // 7
printf("0");
rect[6].setFillColor(sf::Color(50,50,50));
}
a[6] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::J)
{
a[6] = 0;
rect[6].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='k')
{
if(a[7] == 0)
{
sound[7].play(); // 8
printf("0");
rect[7].setFillColor(sf::Color(50,50,50));
}
a[7] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::K)
{
a[7] = 0;
rect[7].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='l')
{
if(a[8] == 0)
{
sound[8].play(); // 9
printf("0");
rect[8].setFillColor(sf::Color(50,50,50));
}
a[8] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::L)
{
a[8] = 0;
rect[8].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='z')
{
if(a[9] ==0)
{
sound[9].play(); // 10
printf("0");
rect[9].setFillColor(sf::Color(50,50,50));
}
a[9] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::Z)
{
a[9] = 0;
rect[9].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='x')
{
if(a[10] ==0)
{
sound[10].play(); // 11
printf("0");
rect[10].setFillColor(sf::Color(50,50,50));
}
a[10] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::X)
{
a[10] = 0;
rect[10].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='c')
{
if(a[11] ==0)
{
sound[11].play(); // 12
printf("0");
rect[11].setFillColor(sf::Color(50,50,50));
}
a[11] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::C)
{
a[11] = 0;
rect[11].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='v')
{
if(a[12] == 0)
{
sound[12].play(); // 13
printf("0");
rect[12].setFillColor(sf::Color(50,50,50));
}
a[12] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::V)
{
a[12] = 0;
rect[12].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='b')
{
if(a[13] == 0)
{
sound[13].play(); // 14
printf("0");
rect[13].setFillColor(sf::Color(50,50,50));
}
a[13] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::B)
{
a[13] = 0;
rect[13].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='n')
{
if(a[14] == 0)
{
sound[14].play(); // 15
printf("0");
rect[14].setFillColor(sf::Color(50,50,50));
}
a[14] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::N)
{
a[14] = 0;
rect[14].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
window.clear();
for(int i=0;i< 15 ; i++)
{
window.draw (rect );
}
for(int i=0;i< 14 ; i++)
{
if(i!=3&&i!=6&&i!=10)
{
window.draw (rect2 );
}
}
window.display();
}
for(int i=0;i<15 ; i++)
{
sound.stop();
}
return 0;
}
STEP 4
หวังว่า ผู้อ่านจะสนุกนะครับ
http://expert-programming-tutor.com
[ view entry ] ( 2022 views ) | permalink | ( 3 / 2520 )
hello world!
[ view entry ] ( 1669 views ) | permalink | ( 2.9 / 2376 )
<<First <Back | 1 | 2 | 3 | 4 | 5 |