forked from forging2012/JavaArithmetic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmeticTwo.java
More file actions
559 lines (390 loc) · 12.6 KB
/
ArithmeticTwo.java
File metadata and controls
559 lines (390 loc) · 12.6 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package basic;
/**
* Created by zxc on 2018/3/23.
*/
public class ArithmeticTwo {
public static void main(String[] args) {
/* String roman = "XVIII";
int num = romanToNumber(roman, 0, roman.length() - 1);
System.out.println("关注公众号:Java3y--------------->" + num);*/
// beerAndDrink();
/* String s = "HELLO WORLD";
char[] ch = new char[s.length()];
for (int i = 0; i < s.length(); i++) {
ch[i] = (char) encode(s.charAt(i), 3);
}*/
int gcd = gcd(35, 5);
System.out.println("关注公众号:Java3y------------>" + gcd);
}
/**
* 求最大公约数
*
* @param num1
* @param num2
*/
public static int gcd(int num1, int num2) {
// 求余数
int r = num1 % num2;
// 如果余数为0,那么除数就是最大公约数
if (r == 0) {
return num2;
} else {
// 否则,则用除数和余数来进行运算
return gcd(num2, r);
}
}
/**
* 右移
*/
public static int rotateRight(int ch) {
if (ch >= 'A' && ch <= 'Y') {
return ch + 1;
} else if (ch >= 'a' && ch <= 'y') {
return ch + 1;
} else if (ch == 'Z') {
return 'A';
} else if (ch == 'z') {
return 'a';
} else {
return ch;
}
}
/**
* 左移
*/
public static int rotateLeft(int ch) {
if (ch >= 'B' && ch <= 'Z') {
return ch - 1;
} else if (ch >= 'b' && ch <= 'z') {
return ch - 1;
} else if (ch == 'A') {
return 'Z';
} else if (ch == 'a') {
return 'z';
} else {
return ch;
}
}
/**
* 加密
*
* @param ch
* @param shift
* @return
*/
public static int encode(int ch, int shift) {
// 如果没有移动,则直接返回
if (shift == 0) {
return ch;
} else if (shift > 0) {
// 如果shift移动的是正数,那么就向右移动
for (int i = 0; i < shift; i++) {
ch = rotateRight(ch);
}
return ch;
} else {
// 如果shift移动的是负数,那么就向左移动
for (int i = 0; i < -shift; i++) {
ch = rotateLeft(ch);
}
return ch;
}
}
/**
* 啤酒与饮料题目
*/
public static void beerAndDrink() {
// 啤酒
for (int i = 0; i < 36; i++) {
// 饮料
for (int j = 0; j < 44; j++) {
// 钱刚好花光了,并且啤酒比饮料少
if (2.3 * i + j * 1.9 == 82.3 && i < j) {
System.out.println("关注公众号:Java3y--------------->啤酒买了" + i);
}
}
}
}
/**
* 将罗马数字转成阿拉伯数字,实际上就是一个查表的过程
* -
*
* @param roman
* @return
*/
public static int digitsToValues(char roman) {
// 定义罗马数字
char digits[] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
// 罗马数字对应的阿拉伯数字
int values[] = {1, 5, 10, 50, 100, 500, 1000};
for (int i = 0; i < digits.length; i++) {
if (digits[i] == roman) {
return values[i];
}
}
return 0;
}
/**
* 找到当前罗马数字最大值的角标
*
* @param digits
* @return
*/
public static int findMaxIndex(String digits, int L, int R) {
// 假设第一个是最大的
int max = digitsToValues(digits.charAt(L));
int maxIndex = L;
for (int i = L; i < R; i++) {
// 将罗马数字转成是阿拉伯数字
int num = digitsToValues(digits.charAt(i));
if (max < num) {
max = num;
maxIndex = i;
}
}
return maxIndex;
}
/**
* 将罗马数字转成阿拉伯数字
*
* @param romanNumber
* @param L
* @param R
*/
public static int romanToNumber(String romanNumber, int L, int R) {
// 如果只有一个罗马数字,那么可以直接返回了(递归出口)
if (L == R) {
return digitsToValues(romanNumber.charAt(L));
} else if (L > R) { // 如果L和R已经越界了,那么说明没有值了
return 0;
} else {
// 找到当前罗马数字最大值的角标
int maxIndex = findMaxIndex(romanNumber, L, R);
// 得到最大值
int max = digitsToValues(romanNumber.charAt(maxIndex));
// 在最大值左边的,则用最大值减去左边的
int left = romanToNumber(romanNumber, L, maxIndex - 1);
// 在最大值右边的,则用最大值加上右边的
int right = romanToNumber(romanNumber, maxIndex + 1, R);
return max - left + right;
}
}
/**
* 画星星
*/
public static void drawStar() {
// 我要画5行的星星
int row = 5;
for (int i = 1; i <= 5; i++) {
// 空格数等于最大行数 - 当前行数
for (int j = 1; j <= row - i; j++) {
System.out.print(" ");
}
// 星星数等于(当前行数*2-1)
for (int j = 1; j <= i * 2 - 1; j++) {
System.out.print("*");
}
// 每画一行就换一次行
System.out.println();
}
}
/**
* 找出数组的单个数字
*
* @param nums
* @return
*/
public static void singleNumber(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int count = countNumber(nums, nums[i]);
// 如果该元素只出现一次,那么就是它了!
if (count == 1) {
System.out.println("关注公众号:Java3y--->单一的元素是:" + nums[i]);
return;
}
}
}
/**
* 找出每个元素出现的次数
*
* @param nums 数组
* @param value 想知道出现次数的元素
*/
public static int countNumber(int[] nums, int value) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (value == nums[i]) {
count++;
}
}
// 返回该元素出现的次数
return count;
}
/**
* 找出数组的单个数字
*
* @param nums
* @param numsSize
* @return
*/
public static int singleNumber(int[] nums, int numsSize) {
// 第一个数和数组后面的数做^运算,留下的必然是单个数字
int k = nums[0];
for (int i = 1; i < numsSize; i++) {
k = (k ^ nums[i]);
}
return k;
}
/**
* 移动元素0到数组最后
*
* @param arrays
*/
public static void moveZero(int[] arrays) {
// 记录该数组有多少个0元素
int zero = 0;
for (int i = 0; i < arrays.length; i++) {
// 只要元素不为0,那么就往前面移动
if (arrays[i] != 0) {
arrays[i - zero] = arrays[i];
} else {
// 如果为0,那么zero ++
zero++;
}
}
// 1. 前面已经将非0的元素移动到数组的前面了
// 2. 将为0的元素填满数组,填充的位置就从length - zero开始
int j = arrays.length - zero;
while (j < arrays.length) {
arrays[j] = 0;
j++;
}
System.out.println("关注公众号:Java3y---->" + arrays);
}
/**
* 移动元素0到数组最后
*
* @param arrays
*/
public static void moveZero2(int[] arrays) {
// 在j前面的元素都不是0
int j = 0;
for (int i = 0; i < arrays.length; i++) {
if (arrays[i] != 0) {
// 跟j进行交换,保证j的前面都不是0
int temp = arrays[i];
arrays[i] = arrays[j];
arrays[j] = temp;
j++;
}
}
// 直至i遍历完毕后,j前面都不是0,j-i都是0(这就完成我们的任务了)
System.out.println("关注公众号:Java3y---->" + arrays);
}
/**
* 找到缺失的数字
*
* @param arrays
*/
public static void missingNumber(int[] arrays) {
// 定义要填充到新数组的数字(随意)
int randomNumber = 89898980;
// 创建一个新的数组(比已缺失的数组多一个长度)
int[] newArrays = new int[arrays.length + 1];
// 填充特殊的数字进新数组中
for (int i = 0; i < newArrays.length; i++) {
// 随意填充数组到新数组中
newArrays[i] = randomNumber;
}
// 遍历题目的数组并使用index替代掉新数组的元素
for (int i = 0; i < arrays.length; i++) {
// 题目数组的值[0,1,2,3,4,...n]其中有一个缺失
int index = arrays[i];
// 重新填充到新数组上,index对应着题目数组的值
newArrays[index] = 3333333;
}
// 遍历新数组,只要还有值为89898980,那么那个就是缺失的数字
for (int i = 0; i < newArrays.length; i++) {
if (newArrays[i] == randomNumber) {
System.out.println("关注公众号:Java3y---->缺失的数字是:" + i);
}
}
}
/**
* 利用等差公式找到缺失的数字
*
* @param arrays
*/
public static void missingNumber2(int[] arrays) {
// 套用等差求和公式
int sum = (arrays[0] + arrays[arrays.length - 1]) * (arrays.length + 1) / 2;
// 遍历数组,得出的sum减去数组每一位元素,最后即是缺失的数字
for (int value : arrays) {
sum = sum - value;
}
System.out.println("关注公众号:Java3y---->缺失的数字是:" + sum);
}
/**
* 删除下标为k的元素
*/
public static void deleteK() {
//固定的常量(比数组元素的个数要大)
int N = 10;
int[] arrays = new int[N];
//对数组进行初始化
for (int i = 0; i < 8; i++) {
arrays[i] = i;
}
//要删除下标
int k = 7;
for (int i = k; i < N - 1; i++) {
arrays[i] = arrays[i + 1];
}
System.out.println("公众号:Java3y" + arrays);
}
/**
* 找出常用的数字:
* 给你一个长度为n的数组,其中有一个数字出现的次数至少为n/2,找出这个数字
*/
public static void findMajorityElement(int[] arrays) {
//构建一个静态栈
int[] stack = new int[arrays.length];
// 栈的front指针
int front = -1;
// 遍历给出的数组
for (int i = 0; i < arrays.length; i++) {
// 判断该栈为空,那么直接将元素入栈
if (front == -1) {
stack[++front] = arrays[i];
} else if (stack[front] == arrays[i]) { // 该元素是否与栈的元素一致-->继续入栈
stack[++front] = arrays[i];
} else {
// 只要不一致,就出栈
front--;
}
}
// 只要该数字出现次数大于数组长度的2/1,那么留下来的数字肯定在栈顶中
System.out.println("关注公众号:Java3y--->" + stack[0]);
}
public static void findMajorityElement2(int[] arrays) {
// 装载栈的元素
int candidate = -1;
// 栈的大小(长度)
int count = 0;
// 遍历给出的数组
for (int i = 0; i < arrays.length; i++) {
// 判断该栈为空,那么直接将元素入栈
if (count == 0) {
candidate = arrays[i];
count++;
} else if (candidate == arrays[i]) { // 该元素是否与栈的元素一致-->入栈(栈多一个元素)
count++;
} else {
// 只要不一致-->出栈(栈少一个元素)
count--;
}
}
// 只要该数字出现次数大于数组长度的2/1,那么留下来的数字肯定在栈顶中
System.out.println("关注公众号:Java3y--->" + candidate);
}
}