FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaArithmetic/src/basic/ArithmeticOne.java at master · feinibuai/JavaArithmetic · GitHub
feinibuai
JavaArithmetic
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaArithmetic
/
src
/
basic
/
ArithmeticOne.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
378 lines (251 loc) · 7.39 KB
Breadcrumbs
JavaArithmetic
/
src
/
basic
/
ArithmeticOne.java
Copy path
File metadata and controls
378 lines (251 loc) · 7.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package
basic
;
/**
* Created by zxc on 2018/3/23.
*/
public
class
ArithmeticOne
{
public
static
void
main
(
String
[]
args
) {
//int num = countWord("Please follow the Wechat public number");
//isUgly(30);
//System.out.println("关注公众号:Java3y--->" + "Please follow the Wechat public number");
//Factorial(3);
Factorial
(
7
);
}
/**
* 判断一个数字是不是ugly number(分解出来的质因数只有2、3、5这3个数字)
*
* @param num
*/
public
static
void
isUgly
(
int
num
) {
if
(
num
<=
0
) {
System
.
out
.
println
(
"不是"
);
}
else
{
while
(
num
%
2
==
0
) {
num
=
num
/
2
;
}
while
(
num
%
3
==
0
) {
num
=
num
/
3
;
}
while
(
num
%
5
==
0
) {
num
=
num
/
5
;
}
if
(
num
==
1
) {
System
.
out
.
println
(
"是"
);
}
else
{
System
.
out
.
println
(
"是"
);
}
}
}
/**
* 判断是否是2的某次方
*/
public
static
void
isPowerOfTwo
() {
int
num
=
8
;
/*
if (num == 0) {
System.out.println("不是");
}
while (num % 2 == 0) {
num = num / 2;
}
if (num == 1) {
System.out.println("是");
} else {
System.out.println("不是");
}
*/
if
(
num
<=
0
) {
System
.
out
.
println
(
"不是"
);
}
else
if
(
num
==
1
) {
System
.
out
.
println
(
"是"
);
}
else
{
if
((
num
& (
num
-
1
)) ==
0
) {
System
.
out
.
println
(
"是"
);
}
else
{
System
.
out
.
println
(
"不是"
);
}
}
}
/**
* 给定两个字符串s和t,判断这两个字符串中的字母是不是完全一样(顺序可以不一样)
*/
public
static
void
isAnagram
() {
//分别存储字符串的字符
char
[]
array1
=
new
char
[
26
];
char
[]
array2
=
new
char
[
26
];
String
s1
=
"pleasefollowthewechatpublicnumber"
;
String
s2
=
"pleowcnumberthewechatpubliasefoll"
;
for
(
int
i
=
0
;
i
<
s1
.
length
();
i
++) {
char
value
=
s1
.
charAt
(
i
);
// 算出要存储的位置
int
index
=
value
-
'a'
;
array1
[
index
]++;
}
for
(
int
i
=
0
;
i
<
s2
.
length
();
i
++) {
char
value
=
s2
.
charAt
(
i
);
// 算出要存储的位置
int
index
=
value
-
'a'
;
array2
[
index
]++;
}
for
(
int
i
=
0
;
i
<
26
;
i
++) {
if
(
array1
[
i
] !=
array2
[
i
]) {
System
.
out
.
println
(
"不相同"
);
return
;
}
}
System
.
out
.
println
(
"相同"
);
}
/**
* 输入一段字符,计算出里面单词的个数
*
* @param str 一段文字
*/
public
static
int
countWord
(
String
str
) {
// 0 表示空格状态,1 表示非空格状态
int
flag
=
0
;
// 单词次数
int
num
=
0
;
for
(
int
i
=
0
;
i
<
str
.
length
();
i
++) {
if
(
String
.
valueOf
(
str
.
charAt
(
i
)).
equals
(
" "
)) {
flag
=
0
;
}
else
if
(
flag
==
0
) {
num
++;
flag
=
1
;
}
}
return
num
;
}
/**
* 猴子吃桃问题
*
* @param x 天数
*/
public
static
int
monkeyQue
(
int
x
) {
/*
//循环方式:
int x = 1;
for (int i = 1; i <= 9; i++) {
x = (x + 1) * 2;
}
*/
if
(
x
<=
0
) {
return
0
;
}
else
if
(
x
==
1
) {
return
1
;
}
else
{
return
2
*
monkeyQue
(
x
-
1
) +
2
;
}
}
/**
* 打印杨辉三角形
*/
public
static
void
PascalTriangle
() {
//打印十行的杨辉三角形
int
[][]
arrays
=
new
int
[
10
][];
//行数
for
(
int
i
=
0
;
i
<
arrays
.
length
;
i
++) {
//初始化第二层的大小
arrays
[
i
] =
new
int
[
i
+
1
];
//列数
for
(
int
j
=
0
;
j
<=
i
;
j
++) {
//是第一列并且行数等于列数,那么通通为1
if
(
i
==
0
||
j
==
0
||
j
==
i
) {
arrays
[
i
][
j
] =
1
;
}
else
{
//当前值等于头上的值+头上左边的值
arrays
[
i
][
j
] =
arrays
[
i
-
1
][
j
] +
arrays
[
i
-
1
][
j
-
1
];
}
}
}
System
.
out
.
println
(
"公众号:Java3y"
+
"-------------------------------"
);
for
(
int
[]
array
:
arrays
) {
for
(
int
value
:
array
) {
System
.
out
.
print
(
value
+
"
\t
"
);
}
System
.
out
.
println
();
}
System
.
out
.
println
(
"公众号:Java3y"
+
"-------------------------------"
);
}
/**
* 数组对角线之和
*/
public
static
void
arraySum
() {
int
[][]
arrays
= {
{
23
,
106
,
8
,
234
},
{
25
,
9
,
73
,
19
},
{
56
,
25
,
67
,
137
},
{
33
,
22
,
11
,
44
},
};
//和
int
sum
=
0
;
for
(
int
i
=
0
;
i
<
arrays
.
length
;
i
++) {
for
(
int
j
=
0
;
j
<
arrays
[
i
].
length
;
j
++) {
if
(
i
==
j
) {
sum
=
sum
+
arrays
[
i
][
j
];
}
}
}
System
.
out
.
println
(
"公众号:Java3y"
+
sum
);
}
/**
* 1-n的阶乘之和
*/
public
static
void
Factorial
(
int
n
) {
//总和
double
sum
=
0
;
//阶乘值,初始化为1
double
factorial
=
1
;
for
(
int
i
=
1
;
i
<=
n
;
i
++) {
factorial
=
factorial
*
i
;
sum
= (
int
) (
sum
+
factorial
);
}
System
.
out
.
println
(
"公众号:Java3y"
+
" "
+
sum
);
}
/**
* 求出二维数组每列的最小值
*/
public
static
void
minArray
() {
//二维数组
int
[][]
arrays
= {
{
23
,
106
,
8
,
234
},
{
25
,
9
,
73
,
19
},
{
56
,
25
,
67
,
137
}
};
//获取列数
int
maxColLength
=
arrays
[
0
].
length
;
//使用一个数组来装载每列最小的值
int
[]
minArray
=
new
int
[
maxColLength
];
//控制列数
for
(
int
i
=
0
;
i
<
maxColLength
;
i
++) {
//假设每列的第一个元素是最小的
int
min
=
arrays
[
0
][
i
];
//控制行数
for
(
int
j
=
1
;
j
<
arrays
.
length
;
j
++) {
//找到最小值
if
(
arrays
[
j
][
i
] <
min
) {
min
=
arrays
[
j
][
i
];
}
}
//赋值给装载每列最小的值的数组
minArray
[
i
] =
min
;
}
System
.
out
.
println
(
"公众号:Java3y"
+
" "
+
minArray
);
}
/**
* 求"1!+4!(2的平方)+9!(3的平方)+...+n的值
*/
public
static
void
calculate
() {
double
sum
=
0
;
for
(
int
i
=
1
;
i
<=
3
;
i
++) {
//得到平方数
int
square
=
i
*
i
;
//阶乘值,从1开始
double
factorial
=
1
;
//求阶乘
for
(
int
j
=
1
;
j
<=
square
;
j
++) {
factorial
=
factorial
*
j
;
}
sum
=
sum
+
factorial
;
}
System
.
out
.
println
(
"公众号:Java3y"
+
" "
+
sum
);
}
}
Back
|
FazBrowse Home
|
New Git URL