011
2
3
4
5
6
7
8
9public class HelloWorld {
public static void main(String []args) {
System.out.println(" J A V V A ");
System.out.println(" J A A V V A A");
System.out.println("J J AAAAA V V AAAAA");
System.out.println(" JJ A A V A A");
}
}
021
2
3
4
5
6
7
8public class HelloWorld {
public static void main(String []args) {
float pai;
pai=4*(1-1/3+1/5-1/7+1/9-1/11+1/13);
System.out.println("pai="+pai);
}
}
031
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
27import java.util.Scanner;
public class A*B{
public static void main(String[] args) {
double a[3][3],b[3][3];
double [][] A={{a[0][0],a[0][1],a[0][2]}
{a[1][0],a[1][1],a[1][2]}
{a[2][0],a[2][1],a[2][2]}}
double [][] B={{b[0][0],b[0][1],b[0][2]}
{b[1][0],b[1][1],b[1][2]}
{b[2][0],b[2][1],b[2][2]}}
double [][] C={{c[0][0],c[0][1],c[0][2]}
{c[1][0],c[1][1],c[1][2]}
{c[2][0],c[2][1],c[2][2]}}
//输入
Scanner in=new Scanner(System in);
a[3][3]=scanner.nextLine();
b[3][3]=scanner.nextLine();
//计算
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
double m+=a[i][j]*b[j][i]
}
}
}
}
04 矩阵相乘算法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
40public class ArrayMult {
public static void main(String[] args) {
int[][] a = {{1,2},{3,4},{5,6}};
int[][] b = {{1,2,3},{4,5,6}};
//m表示数组a的行数,n表示数组b的列数,s表示数组a的列数和数组b的行数
int m = a.length, n = b[0].length, s1 = a[0].length, s2 = b.length;
//判断两个数组是否能够相乘求积
if(s1 != s2) {
System.out.println("数组不能相乘,数组a的列数不等于数组b的行数");
return;
}
//动态创建数组c用来保存结果
int[][] c = new int[m][n];
for(int i=0; i<m; i++) //i表示数组c的每一行
for(int j=0; j<n; j++) { //j表示数组c的每一列
int temp = 0;
for(int k=0; k<s1; k++) //k表示数组a的列号和数组b的行号
temp+=a[i][k]*b[k][j];
c[i][j] = temp;
}
print(a);
print(b);
print(c);
}
private static void print(int[][] c) {
int m = c.length;
int n = c[0].length;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(c[i][j]<10)
System.out.print(" ");
System.out.print(c[i][j] + " ");
}
System.out.println();
}
System.out.println("--------------------------------------");
}
}
061
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
38import java.util.Scanner;
public class FingNearstPoints{
public static void main(String[] args) {
//输入
Scanner input=new Scanner(System in);
System.out.println("Enter the number of points:");
int numberOfPoints=intput.nextLine();
//
double[][] points=new double[numberOfPoints][2];
System.out.println("Enter"+numberOfPoints+"points");
for(int i=0;i<points.length;i++){
points[i][0]=input.nextDouble();
points[i][1]=input.nextDouble();
}
int p1=0,p2=1;
double shortDistance=distance(points[p1][0],points[p1][1],points[p2][0],points[p2][1]);
for(int i=0;i<points.length;i++){
for(int j=i+1;j<points.length;j++){
double distance=distance(points[i][0],points[i][10],points[j][0],points[j][10]);
if(shortDistance>distance){
p1=i;p2=j;
shortDistance=distance;
}
}
}
System.out.println("The two points are"+"("+points[p1][0]+","+points[p1][1]+")and("+points[p2][0]+","+points[p2][1]+")");
}
public static double distance (double x1,double y1,double x2,double y2)
{
return Math.sqrt(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
}
}
}
03 日历 时钟
1 | package textchess; |
4 棋盘覆盖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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226package textchess;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.print.attribute.standard.JobHoldUntil;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class chessBoard extends JFrame {
private int tr, tc, dr, dc, size;//定义各成员变量
int tile = 1;
float red,green,blue;
JPanel centerPanel;
JPanel southPanel;
JButton[][] button;
JTextField TrText, TcText, DrText, DcText, SizeText;
JLabel TrLabel, TcLabel, DrLabel, DcLabel, SizeLabel;
JButton OKButton;
JButton CancelButton;
JPanel panel = new JPanel();
public chessBoard() {
super();
setTitle("棋盘覆盖");
this.setResizable(false);
centerPanel = new JPanel();
southPanel = new JPanel();
OKButton = new JButton("确定或开始");
OKButton.addActionListener(new OKButtonAction());
CancelButton = new JButton("取消或清除");
CancelButton.addActionListener(new OKButtonAction());
setBounds(300, -10, 900, 900);//设置窗口大小与位置
TrText = new JTextField("0",2);//定义各组件
TcText = new JTextField("0",2);
DrText = new JTextField("0",2);
DcText = new JTextField("0",2);
SizeText = new JTextField("4",2);
TrLabel = new JLabel("起始方格坐标x: ");
TcLabel = new JLabel("起始方格坐标y: ");
DrLabel = new JLabel("特殊方格坐标x: ");
DcLabel = new JLabel("特殊方格坐标y: ");
SizeLabel = new JLabel("棋盘规模size: ");
TrText.setEnabled(false);
TcText.setEnabled(false);
int tR = Integer.parseInt(TrText.getText());
int tC = Integer.parseInt(TcText.getText());
int dR = Integer.parseInt(DrText.getText());
int dC = Integer.parseInt(DcText.getText());
int Size = 1;
for (int i=0;i<Integer.parseInt(SizeText.getText());i++)
Size*=2;
tr = tR;
tc = tC;
dr = dR;
dc = dC;
size = Size;
southPanel.add(CancelButton);//添加各组件到窗体
southPanel.add(TrLabel);
southPanel.add(TrText);
southPanel.add(TcLabel);
southPanel.add(TcText);
southPanel.add(DrLabel);
southPanel.add(DrText);
southPanel.add(DcLabel);
southPanel.add(DcText);
southPanel.add(SizeLabel);
southPanel.add(SizeText);
southPanel.add(OKButton);
getContentPane().add(southPanel, BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class gridLayout {
public gridLayout() {
centerPanel.setLayout(new GridLayout(0, size));
button = new JButton[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
button[i][j] = new JButton();
if (i == dr && j == dc) {
button[i][j].setBackground(Color.BLUE);
button[i][j].setText("<html><font size='3' color='white'>棋盘覆盖<br>Done By Java!</font></html>");
button[i][j].setEnabled(false);
}
centerPanel.add(button[i][j]);
}
}
}
private void sleep()
{
for (int i=0;i<100;i++)
for(int j=0;j<1000;j++);
}
public void ChessBoard(int tr, int tc, int dr, int dc, int size) {//算法实现
if (size == 1) // 棋盘方格大小为1,说明递归到最里层
return;
int t = tile++;// 每次递增1
Random rd = new Random();
red=rd.nextFloat();
green=rd.nextFloat();
blue=rd.nextFloat();
Color col = new Color(red,green,blue);
sleep();
int s = size / 2; // 棋盘中间的行、列号(相等的)
// 检查特殊方块是否在左上角子棋盘中
if (dr < tr + s && dc < tc + s) // 在
ChessBoard(tr, tc, dr, dc, s);
else // 不在,将该子棋盘右下角的方块视为特殊方块
{
button[tr + s - 1][tc + s - 1].setBackground(col);
button[tr + s - 1][tc + s - 1].setEnabled(false);
button[tr + s - 1][tc + s - 1].setText("<html><Font size='4',color='white'>"+t+"</Font></html>");
ChessBoard(tr, tc, tr + s - 1, tc + s - 1, s);
sleep();
}
// 检查特殊方块是否在右上角子棋盘中
if (dr < tr + s && dc >= tc + s) // 在
ChessBoard(tr, tc + s, dr, dc, s);
else // 不在,将该子棋盘左下角的方块视为特殊方块
{
button[tr + s - 1][tc + s].setBackground(col);
button[tr + s - 1][tc + s].setEnabled(false);
button[tr + s - 1][tc + s ].setText("<html><Font size='4',color='white'>"+t+"</Font></html>");
ChessBoard(tr, tc + s, tr + s - 1, tc + s, s);
sleep();
}
// 检查特殊方块是否在左下角子棋盘中
if (dr >= tr + s && dc < tc + s) // 在
ChessBoard(tr + s, tc, dr, dc, s);
else // 不在,将该子棋盘右上角的方块视为特殊方块
{
button[tr + s][tc + s - 1].setBackground(col);
button[tr + s][tc + s - 1].setEnabled(false);
button[tr + s ][tc + s - 1].setText("<html><Font size='4',color='white'>"+t+"</Font></html>");
ChessBoard(tr + s, tc, tr + s, tc + s - 1, s);
sleep();
}
// 检查特殊方块是否在右下角子棋盘中
if (dr >= tr + s && dc >= tc + s) // 在
ChessBoard(tr + s, tc + s, dr, dc, s);
else // 不在,将该子棋盘左上角的方块视为特殊方块
{
button[tr + s][tc + s].setBackground(col);
button[tr + s][tc + s].setEnabled(false);
button[tr + s ][tc + s ].setText("<html><Font size='4',color='white'>"+t+"</Font></html>");
ChessBoard(tr + s, tc + s, tr + s, tc + s, s);
sleep();
}
}
}
public class OKButtonAction implements ActionListener {//点下一个按钮时的事件响应
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton whichButton = (JButton) e.getSource();//获取点击的事件,即是点下了哪个按钮
String whichName = whichButton.getActionCommand();
if(whichName.equals("确定或开始")) {
getContentPane().add(centerPanel, BorderLayout.CENTER);
int tR = Integer.parseInt(TrText.getText());
int tC = Integer.parseInt(TcText.getText());
int dR = Integer.parseInt(DrText.getText());
int dC = Integer.parseInt(DcText.getText());
int Size = 1;
for (int i=0;i<Integer.parseInt(SizeText.getText());i++)
Size*=2;
tr = tR;
tc = tC;
dr = dR;
dc = dC;
size = Size;
try {
gridLayout grid = new gridLayout();
grid.ChessBoard(tr, tc, dr, dc, size);
centerPanel.updateUI();
} catch (Exception EX) {
EX.printStackTrace();
}
panel.removeAll();
OKButton.setEnabled(false);
}
if (whichName.equals("取消或清除")) {//当你点下一个提示按钮时的事件响应
JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setText("<html><Font size='+8',color='red'><center><b><br> 您取消了操作或是 <br><Font size='+8',color='blue'><center>您清除了前一个棋盘……" +
"<br><Font size='+8',color='green'><center>下面是关于题目的介绍<br><br><br><br><br><br> </b></Font></html>");
// JLabel l = new JLabel("题目要求");
JTextArea area = new JTextArea("在一个2k x 2k ( 即:2^k x 2^k )个方格组成的棋盘中,恰有一个方格与其他方格不同," +
"称该方格为一特殊方格,且称该棋盘为一特殊棋盘。在棋盘覆盖问题中,要用4种不同形态的L型骨牌覆盖给定的特殊棋盘上除特殊方格以外的所有方格," +
"且任何2个L型骨牌不得重叠覆盖。",7,60);
area.setLineWrap(true);
area.setBackground(Color.blue);
area.setForeground(Color.white);
area.setFont (new Font("SansSerif", Font.PLAIN, 14));
area.setEditable(false);
// FlowLayout layout = new FlowLayout();
// layout.addLayoutComponent(arg0, arg1)
panel.add(label,centerPanel );
// panel.add(l,southPanel);
panel.add(area,southPanel);
getContentPane().add(panel, BorderLayout.CENTER);
panel.updateUI();
tile=1;
centerPanel.removeAll();
OKButton.setEnabled(true);
}
}
}
public static void main(String[] args) {//主函数方法实现
chessBoard chess = new chessBoard();
chess.setVisible(true);
Runtime run = Runtime.getRuntime();
run.gc();//手动清除数据垃圾
}
}