zoukankan      html  css  js  c++  java
  • 自己做题的简单的算法

    1,题目:判断101-200之间有多少个素数,并输出所有素数。

     1 package example;
     2 
     3 public class Test {
     4 
     5     /**
     6      * @param args
     7      */
     8     public static void main(String[] args) {
     9         int count =0;
    10         for(int i=101;i<=201;i++){
    11         if(issushu(i)==true){
    12             count++;
    13             System.out.print(i+" ");
    14         }
    15         }
    16         System.out.println(count+"个素数");
    17     }
    18     
    19 
    20     private static boolean issushu(int i) {
    21         for(int j=2;j<i;j++)
    22         {
    23             if(i%j==0)
    24                 return false;        
    25         }
    26         return true;
    27     }    
    28 }

    101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 21个素数

    2题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身

     1 package example;
     2 
     3 public class Test {
     4 
     5     /**
     6      * @param args
     7      */
     8     public static void main(String[] args) {
     9         
    10         for(int i=100;i<=999;i++){
    11         if(ishua(i)==true)
    12             System.out.print(i+" ");
    13         
    14         }
    15     }
    16     
    17 
    18     private static boolean ishua(int i) {
    19         int j=i/100;
    20         int q=(i-100*j)/10;
    21         int p=i-100*j-10*q;
    22         if(j*j*j+q*q*q+p*p*p==i)
    23             return true;
    24         else 
    25             return false;
    26     }    
    27 }

    153 370 371 407

    3将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5

    package example;
    
    public class Test {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            fenjie(90);
                
        }
        public static void fenjie(int n) {
            for (int i = 2; i <= n; i++)
            {
                if (n % i == 0) 
                {
                    System.out.print(i);
                    if(n!=i)
                    {
                        System.out.print("*");
                    }
                    fenjie(n/i);
                }
            }
            System.exit(0); 
        }
            
    }    

    2*3*3*5

    4【程序6】题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

    class test  
    {
        public static void main (String[] args) throws java.lang.Exception
        {
            System.out.println(yue(8,20));
            System.out.println(bei(8,20));
        }
        public static int yue(int i,int j)
        {         
            int max=0;
            for (int x=1;x<=i||x<=j ;x++ ) 
            {
                if(i%x==0&&j%x==0)
                max=x;
            }
            return max;
        }
        public static int bei(int i,int j)
        {    
            int x=i>j?i:j;
            while(x%i!=0||x%j!=0)
            {
                x++;
            }
            return x;
        }
    }

    4
    40

    答案有说道辗转相除法,可以看看了解下。

    5题目:输入某年某月某日,判断这一天是这一年的第几天

    package example;
    
    import java.util.Calendar;
    import java.util.Scanner;
    
    public class Test {
    
        /**
         * 输入某年某月某日,判断这一天是这一年的第几天
         */
        public static void main(String[] args) {
        
        Scanner sc =new Scanner(System.in);
        int year=sc.nextInt();
        int month=sc.nextInt();
        int day=sc.nextInt();
        int days=getday(year, month,day);
        System.out.println("第"+days+"天");
                
        }
    
        private static int getday(int year, int month, int day) {
            Calendar ca=Calendar.getInstance();
            ca.set(year, month, day);
            return ca.get(Calendar.DAY_OF_YEAR); 
        
        }
    
        
    }    

    2016
    2
    20
    第80天

    6 题目:输入三个整数x,y,z,请把这三个数由小到大输出

    package example;
    
    import java.util.Arrays;import java.util.Scanner;
    
    public class Test {
    
        /**
         *  题目:输入三个整数x,y,z,请把这三个数由小到大输出
         */
        public static void main(String[] args) {
        
        System.out.println("输入三个整数");
        Scanner sc =new Scanner(System.in);
        int[] arr=new int[3];
        int a=sc.nextInt();
        int b=sc.nextInt();
        int c=sc.nextInt();
        arr[0]=a;arr[1]=b;arr[2]=c;
        Arrays.sort(arr);
        for(int i:arr){
            System.out.print(i+" ");
        }
         
        }

    输入三个整数
    45
    78
    33
    33 45 78

    7:输出9*9口诀乘法表。

    package example;
    
    public class Test {
    
        /**
         *  :输出9*9口诀乘法表。
         */
        public static void main(String[] args) {
        
         for(int i=1;i<=9;i++)
         {
             for(int j=i;j<=9;j++)
             {
                 System.out.print(i+"*"+j+"="+i*j+"	");//制表符
                
             }
             System.out.println();
         }
        }
    
    }

    1*1=1    1*2=2    1*3=3    1*4=4    1*5=5    1*6=6    1*7=7    1*8=8    1*9=9    
    2*2=4    2*3=6    2*4=8    2*5=10    2*6=12    2*7=14    2*8=16    2*9=18    
    3*3=9    3*4=12    3*5=15    3*6=18    3*7=21    3*8=24    3*9=27    
    4*4=16    4*5=20    4*6=24    4*7=28    4*8=32    4*9=36    
    5*5=25    5*6=30    5*7=35    5*8=40    5*9=45    
    6*6=36    6*7=42    6*8=48    6*9=54    
    7*7=49    7*8=56    7*9=63    
    8*8=64    8*9=72    
    9*9=81

    8题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

    package example;
    
    public class Test {
    
        /**
         *  :猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,
         *  又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
         *  以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,
         *  见只剩下一个桃子了。求第一天共摘了多少
         */
        public static void main(String[] args) {
          int i=1;
          for(int j=1;j<10;j++){
           i=back(i);
          }
          System.out.println(i);
         
        }
    
        private static int back(int i) {
            i++;
            i*=2;
            return i;
        }
    
    }    

    1534

    9题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。

    package example;
    
    public class Test {
        public static void main(String[] args) {
            char q,w,e;  //对应adc各自对手
            for(q='x';q<='z';q++)
            for(w='x';w<='z';w++){
                if(q!=w)
                for(e='x';e<='z';e++){
                    if(q!=e && w!=e){
                    if(q!='x'&&e!='x'&&e!='z')
                    System.out.println(" a的对手"+q+"
     b的对手"+w+"
     c的对手"+e);
    
               }
    
             }
    
             }
      }    
    }

     a的对手z
     b的对手x
     c的对手y

    10题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。

    package example;
    
    public class Test {
        public static void main(String[] args) {
            double i=0;
            double k=1;
            double temp=1;
            double sum=0;
            for(int j=1;j<=20;j++){
                i=k;
                k=temp+k;
                temp=i;
                sum+=k/i;
                System.out.println((int)k+"/"+(int)i);
                }
            System.out.println(sum);
            }
      }

    2/1
    3/2
    5/3
    8/5
    13/8
    21/13
    34/21
    55/34
    89/55
    144/89
    233/144
    377/233
    610/377
    987/610
    1597/987
    2584/1597
    4181/2584
    6765/4181
    10946/6765
    17711/10946
    32.66026079864164

    11求1+2!+3!+...+20!的和

    package example;
    
    public class Test {
        public static void main(String[] args) {
            long sum=0;
            for(int i=1;i<=20;i++){
                sum+=getchen(i);
            }
            System.out.println(sum);
            
            }
    
        
    
        private static long getchen(int i) {
            if(i==1)
                return 1;
            else return i*getchen(i-1);
                
        }
      }

    2561327494111820313

     12  题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?

    package example;
    
    public class Test {
        public static void main(String[] args) {
            
            System.out.println(getage(5));
        
            }
    
        private static int getage(int i) {
            if(i==1)
                return 10;
            return 2+getage(i-1) ;
        }
      }

    18

    13 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

    package example;
    
    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) {
            System.out.println("请输入一个五位数以内的正整数");
            Scanner sc=new Scanner(System.in);
            int a=sc.nextInt();
            if(a<0||a>100000){
                System.out.println("输入不符合要求");
                System.exit(0);
            }
            if (a >= 0 && a <= 9) {
                System.out.println(a + "是一位数");
                System.out.println("按逆序输出是:"  + a);
            } else if (a >= 10 && a <= 99) {
                System.out.println(a + "是二位数");
                System.out.println("按逆序输出是:");
                converse(a);
            } else if (a >= 100 && a <= 999) {
                System.out.println(a + "是三位数");
                System.out.println("按逆序输出是:");
                converse(a);
            } else if (a >= 1000 && a <= 9999) {
                System.out.println(a + "是四位数");
                System.out.println("按逆序输出是:");
                converse(a);
            } else if (a >= 10000 && a <= 99999) {
                System.out.println(a + "是五位数");
                System.out.println("按逆序输出是:");
                converse(a);
            }
        }
    
        private static void converse(int a) {
            StringBuffer sb = new StringBuffer(String.valueOf(a));
            sb.reverse();
            System.out.println(sb);
        }    
    }

    请输入一个五位数以内的正整数
    12345
    12345是五位数
    按逆序输出是:
    54321

    14题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

    package example;
    
    import java.util.Scanner;
    
    
    public class Test {
        public static void main(String[] args) throws Exception  {
            System.out.println("请输入一个五位数");
            Scanner sc=new Scanner(System.in);
            int a=sc.nextInt();
            if(a<10000||a>99999)
                throw new Exception("输入不是五位数"); 
            int q=a/10000;
            int w=a/1000-q*10;
            int e=a/100-q*100-w*10;
            int r=a/10-q*1000-w*100-e*10;
            int x=a-q*10000-w*1000-e*100-r*10;
            if(q==x&&w==r)
                System.out.println(a+"是回文数");
        }
    }
    /* 
     请输入一个五位数
    12321
    12321是回文数 
    */

    打印出杨辉三角形(10行)

    1
    1   1
    1   2   1
    1   3   3   1
    1   4   6   4   1
    1   5   10   10   5   1

    package example;
    
    
    
    
    public class Test {
        public static void main(String[] args) throws Exception  {
            int arr[][]=new int[10][10];
            for(int i=0;i<10;i++){
                for(int j=0;j<=i;j++)
                {
                    arr[i][j]=1;
                    if(i>1&&j>0){
                        arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
                    }
                    System.out.print(arr[i][j]+" ");
                }
                System.out.println();
            }
        }
    }
    /* 
    1 
    1 1 
    1 2 1 
    1 3 3 1 
    1 4 6 4 1 
    1 5 10 10 5 1 
    1 6 15 20 15 6 1 
    1 7 21 35 35 21 7 1 
    1 8 28 56 70 56 28 8 1 
    1 9 36 84 126 126 84 36 9 1 
    
    */
  • 相关阅读:
    【STM32F429】第4章 RTX5操作系统移植(MDK AC5)
    【STM32F407】第4章 RTX5操作系统移植(MDK AC5)
    【STM32H7】第3章 RTX5操作系统介绍
    【STM32F429】第3章 RTX5操作系统介绍
    【STM32F407】第3章 RTX5操作系统介绍
    【STM32H7】第2章 初学RTX5准备工作
    【STM32F429】第2章 初学RTX5准备工作
    Hystrix解析(一)
    Eureka源码解析(五)
    Eureka源码分析(四)
  • 原文地址:https://www.cnblogs.com/xurui1995/p/5186059.html
Copyright © 2011-2022 走看看