Java Programing

二叉树遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
TreeNode root=null;
root=CreatBinaryTree();
inorderTraversal(root);
}
public static TreeNode CreatBinaryTree(){
TreeNode t=null;
@SuppressWarnings("resource")
Scanner scaner = new Scanner(System.in);
String s = scaner.next();
char c = s.charAt(0);
int val=c-'0';
if(c!='#')
{
t=new TreeNode(val);
t.left=CreatBinaryTree();
t.right=CreatBinaryTree();
}
return t;
}
public static void inorderTraversal(TreeNode root) {
// write your code here
if(root!=null)
{
inorderTraversal(root.left);
System.out.print(root.val+" ");
inorderTraversal(root.right);
}
}
}

国庆节小动画程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class GuoqingStart extends JFrame {
Guoqing gq;
public static void main(String[] args) {
new GuoqingStart();
}
public GuoqingStart() {
gq=new Guoqing();
this.addMouseListener(gq);
this.add(gq);
new Thread(gq).start();
this.setTitle("国庆快乐");
this.setLocation(100, 0);
this.setSize(1000,700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Guoqing extends JPanel implements MouseListener,Runnable{
Bullet bullet;
Vector<Bullet> bullets;
static Vector<Piece> pieces;
int times=1;
public Guoqing() {
bullets=new Vector<Bullet>();
pieces=new Vector<Piece>();
}
public void paint(Graphics g) {
super.paint(g);
times++;
g.setColor(Color.black);
g.fillRect(0, 0, 2000, 1000);
g.setColor(Color.lightGray);
for(int i=0;i<this.bullets.size();i++){
Bullet b=this.bullets.get(i);
if(b.isLive){
b.move();
g.fillOval(b.x,b.oldY, 15, 15);
}
}
for(int i=0;i<this.pieces.size();i++){
Piece b=this.pieces.get(i);
if(b.isLive){
if(b.color==0){
g.setColor(Color.red);
}else if(b.color==1){
g.setColor(Color.blue);
}else if(b.color==2){
g.setColor(Color.cyan);
}else if(b.color==3){
g.setColor(Color.green);
}else{
g.setColor(Color.gray);
}
if(times%2==0){
b.move();
}
g.fillOval((int)b.x, (int)b.y, (int)b.w, (int)b.h);
}
}
}
public void mousePressed(MouseEvent e) {
bullet=new Bullet(e.getX(),e.getY());
this.bullets.add(bullet);
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
@Override
public void run() {
while(true){
this.repaint();
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class Piece{
double x,y;
double w=15;
double h=15;
double velocityX, velocityY;
boolean isLive=true;
int color=0;
int times=1;
public Piece(double x,double y,double velocity,double angle,int color) {
this.x=x;
this.y=y;
this.color=color;
velocityX = velocity * Math.cos(angle);
velocityY = velocity * Math.sin(angle);
}
public void move(){
velocityX *= 0.75;
velocityY += 1.0;
velocityY *= 0.75;
y += velocityY;
x += velocityX;
times++;
if(times%7==0){
this.w-=2;
this.h-=2;
if(this.w<=4){
Guoqing.pieces.remove(this);
}
}
}
}
class Bullet{
int x;
int y;
int oldY=700;
boolean isLive=true;
Piece piece;
Random r=new Random(System.currentTimeMillis());
public Bullet(int x, int y) {
this.x = x;
this.y = y;
}
public void move(){
oldY-=8;
if(oldY<=y){
this.isLive=false;
int color=r.nextInt(4);
for(int i=0;i<200;i++){
double angleXy = r.nextDouble() * 2 * Math.PI;
double angleZ = r.nextDouble() * 2 * Math.PI;
double velocity = 72 * Math.cos(angleZ);
piece=new Piece(x,oldY,velocity, angleXy,color);
Guoqing.pieces.add(piece);
}
}
}
}