网上的一些java算法题的个人练习笔记

  1 package com.test;
  2
  3 import java.io.BufferedReader;
  4 import java.io.InputStreamReader;
  5 import java.util.Arrays;
  6
  7 import org.junit.Test;
  8
  9 public class Test01 {
 10
 11     /**
 12      * 键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
 13      * @throws Exception
 14      */
 15     @Test
 16     public void test01()throws Exception{
 17
 18         BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
 19
 20         String str=bfr.readLine();
 21         int countNum = 0;//统计数字的个数
 22         int countChar = 0;//统计英文字母的个数
 23         int countSpace = 0;//统计空格的个数
 24         int countOthers = 0;//统计其它字符的个数
 25
 26         for(int i=0; i < str.length();i++){
 27             char s = str.charAt(i);
 28
 29             if(s > ‘0‘ && s < ‘9‘){
 30                 countNum++;
 31             }else if( (s > ‘a‘ && s < ‘z‘) || (s > ‘A‘ && s < ‘Z‘)){
 32                 countChar++;
 33             }else if(s == ‘ ‘){
 34                 countSpace++;
 35             }else{
 36                 countOthers++;
 37             }
 38         }
 39         System.out.println("数字个数:"+countNum);
 40         System.out.println("英文字母个数:"+countChar);
 41         System.out.println("空格个数:"+countSpace);
 42         System.out.println("其他字符个数:"+countOthers);
 43
 44     }
 45
 46     /**
 47      * 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
 48      */
 49     @Test
 50     public void test02() throws Exception{
 51         System.out.println("请输入要叠加的数,大于0小于10");
 52         BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
 53         System.out.println("请输入需要相加的个数,大于0");
 54         BufferedReader bfr1 = new BufferedReader(new InputStreamReader(System.in));
 55
 56         int num = Integer.parseInt(bfr.readLine());
 57         int total = Integer.parseInt(bfr1.readLine());
 58         int b = (int)nums(num , total);
 59         System.out.println("result is :"+b);
 60     }
 61
 62     public double nums(int num,int total){
 63         double result = 0;
 64         double a = 0;
 65
 66         for(int i = 0 ; i < total ; i++){
 67
 68             a = num*(Math.pow(10, i))+a;
 69             result += a;
 70         }
 71         return result;
 72     }
 73
 74     /**
 75      * 一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3.编程 找出1000以内的所有完数。
 76      * 答案:6,28是完数
 77      */
 78     @Test
 79     public void test03() throws Exception{
 80         System.out.println("请输入X以内的完数,不包括X:");
 81         BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
 82
 83         int num = Integer.parseInt(bfr.readLine());
 84         //先获得一个数所有的因子
 85         for(int i = 1; i < num ; i++){
 86             int total = 0;
 87             for(int j = 1 ; j < i ; j++){
 88                 if(i % j == 0){
 89                     total += j;
 90                 }
 91             }
 92             if(total == i){
 93                 System.out.println("数"+i+"是完数");
 94             }
 95         }
 96     }
 97     /***
 98      * 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
 99      * @throws Exception
100      */
101     @Test
102     public void test04() throws Exception{
103
104         double higth10 = 0; //计算第10次的反弹高度
105         double total10 = 0; //计算10次经过的米数
106         //int num = 0; //下落的次数
107         //100/2 100/2/2 100/2/2/2
108         for(int i= 1 ; i < 10 ; i++){
109
110             double a =100/(Math.pow(2, i-1))+100/(Math.pow(2, i));
111             total10 += a;
112
113             if(i == 9){
114                 higth10 = 100/(Math.pow(2, i+1));
115             }
116         }
117
118         System.out.println("第10次落地时,共经过"+total10+"米,第10次反弹"+higth10+"米");
119
120     }
121     /**
122      * 猴子吃桃问题。猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉了一半,
123      * 又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩一个桃子了。求第一天共摘了多少桃子。
124      * @throws Exception
125      */
126     @Test
127     public void test05() throws Exception{
128         int total = 1; //记录猴子摘下的桃子总数
129         //x/2+1 (x-(x/2+1))+1
130         //1 (1+1)*2 (4+1)*2
131         for(int i =2 ; i <= 10; i++){
132             total = (total+1)*2;
133         }
134         System.out.println("猴子第一天摘的总的桃子数是:"+total+"个");
135     }
136
137     /**
138      * 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
139      * @throws Exception
140      */
141     @Test
142     public void test06() throws Exception{
143         int[] nums = {1,2,3,4}; //数组保存所有的数
144         for(int i = 0 ; i < 4 ;i++){ //循环获得百位数
145             int num3 = nums[i];
146             for(int j =0 ; j < 4 ; j++){//循环获得十位数
147
148                 int num2 = nums[j];
149                 if(num3 == num2){
150                     continue;
151                 }else{
152                     for(int k = 0 ; k < 4;k++){ //循环获得个位数
153                         int num1 = nums[k];
154                         if(num1 ==  num2 || num1 == num3){
155                             continue;
156                         }else{
157                             System.out.println(num3+""+num2+""+num1);
158                         }
159                     }
160                 }
161             }
162         }
163     }
164
165     /**
166      * 题目:判断101-200之间有多少个素数,并输出所有素数。
167      * @throws Exception
168      */
169     @Test
170     public void test07() throws Exception{
171         //素数,除了1和本身之外不能被整除的数
172         //注意:return是跳出所有的循环,break是跳出当前的循环
173         for(int i = 101;i < 201  ; i++ ){
174             boolean isSuShu = true;
175             for(int j = 2 ; j < i ; j++){
176                 if(i%j == 0){
177                     isSuShu = false;
178                     break;
179                 }
180             }
181
182             if(isSuShu){
183                 System.out.println("素数:"+i);
184             }
185         }
186     }
187
188     /**
189      * 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
190      * 答案:156
191      * @throws Exception
192      */
193     @Test
194     public void test08() throws Exception{
195
196         //x+100 x+100+168
197
198         for(int i = 0 ; i < 100000 ; i++){
199             int a = i + 100;
200             double value =  Math.sqrt(a);
201             String origin = String.valueOf(value);
202             String[] values= origin.split("\\.");
203
204             for(int j = 0 ; j < values.length ; j++){
205                 if(values[j].equals("0")){
206
207                     for(int m = 0 ; m < 100000 ; m++){
208                         int b = i + 168;
209                         double value1 =  Math.sqrt(b);
210                         String origin1 = String.valueOf(value1);
211                         String[] values1= origin1.split("\\.");
212
213                         for(int n = 0 ; n < values1.length ; n++){
214                             if(values1[j].equals("0")){
215                                 System.out.println("这个数就是:"+i);
216                                 return;
217                             }
218                         }
219                     }
220                 }
221             }
222         }
223     }
224
225     /**
226      * 输入某年某月某日,判断这一天是这一年的第几天?
227      * @throws Exception
228      */
229     @Test
230     public void test09() throws Exception{
231         System.out.println("输入某年某月某日,格式如:2014-07-15");
232         BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
233
234         String time = bfr.readLine();
235         String[] times = time.split("-");
236         int year = 0;
237         int month = 0;
238         int day = 0;
239         for(int i = 0 ; i < times.length ; i++){
240             year = Integer.parseInt(times[0]);
241             month = Integer.parseInt(times[1]);
242             day = Integer.parseInt(times[2]);
243         }
244         //思路,判断是否是闰年,是:366天,不是:365天
245         //闰年的条件:
246         //①、普通年能被4整除的为闰年。(如2004年就是闰年,1901年不是闰年)
247         //②、世纪年能被400整除而不能被3200整除的为闰年。(如2000年是闰年,3200年不是闰年)
248         //③、对于数值很大的年份能整除3200,但同时又能整除172800则又是闰年.(如172800年是闰年,8640
249
250         boolean isRun = false ; //isRun是否是闰年,默认不是
251         if(year % 4 == 0){//能被四整除,有可能是闰年
252
253             if(year % 400 == 0 && year % 3200 == 0){
254                 isRun = false;
255             }else if(year % 3200 ==0 && year % 172800 != 0){
256                 isRun = false;
257             }else{
258                 isRun = true;
259             }
260         }else{
261             isRun = false;
262         }
263
264         if(isRun){//366天
265
266             switch(month){
267                 case 1:
268                     System.out.println("这一天是这一年的第"+day+"天");
269                     break;
270                 case 2:
271                     System.out.println("这一天是这一年的第"+(31 + day)+"天");
272                     break;
273                 case 3:
274                     System.out.println("这一天是这一年的第"+(60 + day)+"天");
275                     break;
276                 case 4:
277                     System.out.println("这一天是这一年的第"+(91 + day)+"天");
278                     break;
279                 case 5:
280                     System.out.println("这一天是这一年的第"+(121 + day)+"天");
281                     break;
282                 case 6:
283                     System.out.println("这一天是这一年的第"+(152 + day)+"天");
284                     break;
285                 case 7:
286                     System.out.println("这一天是这一年的第"+(182 + day)+"天");
287                     break;
288                 case 8:
289                     System.out.println("这一天是这一年的第"+(213 + day)+"天");
290                     break;
291                 case 9:
292                     System.out.println("这一天是这一年的第"+(244 + day)+"天");
293                     break;
294                 case 10:
295                     System.out.println("这一天是这一年的第"+(274 + day)+"天");
296                     break;
297                 case 11:
298                     System.out.println("这一天是这一年的第"+(305 + day)+"天");
299                     break;
300                 case 12:
301                     System.out.println("这一天是这一年的第"+(335 + day)+"天");
302                     break;
303             }
304
305         }else{//365天
306             switch(month){
307             case 1:
308                 System.out.println("这一天是这一年的第"+day+"天");
309                 break;
310             case 2:
311                 System.out.println("这一天是这一年的第"+(31 + day)+"天");
312                 break;
313             case 3:
314                 System.out.println("这一天是这一年的第"+(59 + day)+"天");
315                 break;
316             case 4:
317                 System.out.println("这一天是这一年的第"+(90 + day)+"天");
318                 break;
319             case 5:
320                 System.out.println("这一天是这一年的第"+(120 + day)+"天");
321                 break;
322             case 6:
323                 System.out.println("这一天是这一年的第"+(151 + day)+"天");
324                 break;
325             case 7:
326                 System.out.println("这一天是这一年的第"+(181 + day)+"天");
327                 break;
328             case 8:
329                 System.out.println("这一天是这一年的第"+(212 + day)+"天");
330                 break;
331             case 9:
332                 System.out.println("这一天是这一年的第"+(243 + day)+"天");
333                 break;
334             case 10:
335                 System.out.println("这一天是这一年的第"+(273 + day)+"天");
336                 break;
337             case 11:
338                 System.out.println("这一天是这一年的第"+(304 + day)+"天");
339                 break;
340             case 12:
341                 System.out.println("这一天是这一年的第"+(334 + day)+"天");
342                 break;
343         }
344         }
345     }
346     /**
347      * 输入三个整数x,y,z,请把这三个数由小到大输出。
348      * @throws Exception
349      */
350     @Test
351     public void test10() throws Exception{
352         System.out.println("请输入第一个整数,回车结束!");
353         BufferedReader bfr1 = new BufferedReader(new InputStreamReader(System.in));
354
355         System.out.println("请输入第二个整数,回车结束!");
356         BufferedReader bfr2 = new BufferedReader(new InputStreamReader(System.in));
357
358         System.out.println("请输入第三个整数,回车结束!");
359         BufferedReader bfr3 = new BufferedReader(new InputStreamReader(System.in));
360
361         int num1 = Integer.parseInt(bfr1.readLine());
362         int num2 = Integer.parseInt(bfr2.readLine());
363         int num3 = Integer.parseInt(bfr3.readLine());
364
365         int[] nums = new int[5];
366         nums[0] = num1;
367         nums[1] = num2;
368         nums[2] = num3;
369         nums[3] = 1;
370         nums[4] = 6;
371         //冒泡排序法:思路是相邻两个进行比较,依次向后一位移动,有顺序改变的需要调换位置,重新赋值
372         for(int i = 0 ; i < nums.length ; i++){
373             for(int j = i+1 ; j < nums.length ; j++){
374                 if(nums[i] > nums[j]){ //i 大于 j ,先把i值用tem临时变量保存,再将j的值给i,最后将tem的值给j,即可实现i和j的对换
375                     int tem = nums[i];
376                     nums[i] = nums[j];
377                     nums[j] = tem;
378                 }
379             }
380         }
381     }
382
383     @Test
384     public void test11() throws Exception{
385                int[] array=new int[]{44,213,134,11,77,78,23,43};
386                QuickSort(array, 0, array.length-1);
387                for(int i=0;i<array.length;i++)
388                {
389                    System.out.println((i+1)+"th:"+array[i]);
390                }
391     }
392
393     public void QuickSort(int[] array,int start,int end){
394            if(start<end)
395            {
396                int key=array[start];//初始化保存基元
397                int i=start,j;//初始化i,j
398                for(j=start+1;j<=end;j++) {
399
400                    if(array[j]<key)//如果此处元素小于基元,则把此元素和i+1处元素交换,并将i加1,如大于或等于基元则继续循环
401                    {
402                        int temp=array[j];
403                        array[j]=array[i+1];
404                        array[i+1]=temp;
405                        i++;
406                    }
407
408                }
409                array[start]=array[i];//交换i处元素和基元
410                array[i]=key;
411                QuickSort(array, start, i-1);//递归调用
412                QuickSort(array, i+1, end);
413
414            }
415     }
416
417     /**
418      * 递归方法sum,求1+2+...+100 的求和
419      * @throws Exception
420      */
421     @Test
422     public void test12() throws Exception{
423         System.out.println(Sum(100));
424     }
425
426     public int Sum(int num){
427
428         if(num > 0){
429             return num + Sum(num-1);
430         }else{
431             return 0;
432         }
433
434     }
435     /**
436      * 递归方法sum,求1*2*...*6 的求和
437      * @throws Exception
438      */
439     @Test
440     public void test13(){
441
442         System.out.println(test13_1(6));
443
444     }
445
446     public static int test13_1(int n) {
447         if (1 == n)
448             return 1;
449         else
450             return n*test13_1(n-1);
451     }
452
453     /**
454      * 递归方法约数,求两个数的最大公约数   ,用两个数的绝对值与这两个数较小的那个一直比较,直到相等为止。
455      */
456     @Test
457     public void test14(){
458
459         System.out.println(ZuiDaGongYueShu(20,30));
460
461     }
462
463     public int ZuiDaGongYueShu(int num1 , int num2){
464         if(num1 == num2){
465             return num1 ;
466         }else{
467
468             return ZuiDaGongYueShu(JueDuiZhi(num1-num2),BiJiao(num1,num2));
469         }
470     }
471
472     public int JueDuiZhi (int num){
473         return num > 0? num : -num;
474     }
475
476     public int BiJiao(int num1 , int num2){
477         return num1-num2> 0 ? num2 : num1;
478     }
479
480
481
482 }
时间: 2024-08-14 08:39:23

网上的一些java算法题的个人练习笔记的相关文章

趣味Java算法题(附答案)

[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? //这是一个菲波拉契数列问题 public class lianxi01 { public static void main(String[] args) { System.out.println("第1个月的兔子对数:    1"); System.out.println("第2个月的兔子对数:    1"

25道经典Java算法题

即使做web开发,也会遇到各种各种需要解决的算法问题,本文节选部分经典练手算法,并提供相关参考答案,希望对你有所帮助[程序1]题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? //这是一个菲波拉契数列问题 public class test01 { public static void main(String[] args) { int f1=1,f2=1,f; int M=30; System.

【JAVA算法题】职业抢劫

题目 /*You are a professional robber planning to rob houses along a street. * Each house has a certain amount of money stashed, * the only constraint stopping you from robbing each of them is that * adjacent houses have security system connected and *

面试-java算法题

1.编写一个程序,输入n,求n!(用递归的方式实现). public static long fac(int n){ if(n<=0) return 0; else if(n==1) return 1; else return n*fac(n-1); } public static void main(String [] args) { System.out.println(fac(6)); } 2.编写一个程序,有1,2,3,4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? pu

经典java算法题及答案

1.使用Java语言编写代码,将一个正整数分解质因数,例如:输入90,打印输出90=2*3*3*5. 答案:public  class  Math{ public  static void  main(String[] args) { int n,i;System.out.println("\nplease input a number:\n");Scanner input=new Scanner(System.in); n=input.nextInt(); System.out.pr

Java算法题2.判断101-200之间有多少个素数,并输出所有素数。

[原创 转载注明出处] 题目2:判断101-200之间有多少个素数,并输出所有素数. 思路: 素数(质数)就是除了1和它本身以外,没有其他的数可以被它整除 java代码实现: 1 package jichu; 2 3 public class jichu2 4 { 5 public static void main(String[] args) 6 { 7 //for循环遍历101-200 8 for(int i = 101; i < 200; i++) 9 { 10 boolean b = f

Java算法题3.打印出所有的“水仙花数”。

[原创 转载注明出处] 题目3:打印出100-999所有的“水仙花数”. 思路: 水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身. (例如:1^3 + 5^3+ 3^3 = 153) Java代码实现 1 package jichu; 2 3 public class jichu3 4 { 5 public static void main(String[] args) 6 { 7 int b, s, g; 8 for(int i = 100; i <

Java算法题:兔子问题

题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 解题思路: 1 public int exp(int month){ 2 if(month == 1 || month == 2){ 3 return 1; 4 }else{ 5 return exp(month-1)+exp(month-2); 6 } 7 } 8 @Test 9 public void testExp(){ 10 Formatt

Java算法题:求素数

题目:判断101-200之间有多少个素数,并输出所有素数. 思路:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数. 具体代码: 1 public Vector exp(int first, int end) { 2 Vector v = new Vector(); 3 boolean b; 4 for (int i = first; i <= end; i++) { 5 b = true;// 假设是质数 6 for (int j = 2;