暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

java经典算法题2

CodeJie 2021-09-06
113

题目21:打印出图案(菱形)

    package com.example.test;
    public class Test21 {
    public static void main(String[] args) {
    for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= 4-i ; j++) {
    System.out.print(" ");
    }
    for (int j = 1; j <= 2*i-1; j++) {
    System.out.print("*");
    }
    System.out.println();
    }
    for (int i = 3; i >= 1; i--) {
    for (int j = 1; j <= 4-i ; j++) {
    System.out.print(" ");
    }
    for (int j = 1; j <= 2*i-1; j++) {
    System.out.print("*");
    }
    System.out.println();
    }
    }
    }


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

      package com.example.test;


      public class Test22 {
      public static void main(String[] args) {
      float sum=0,ver=2;
      for(int i=1;i<=20;i++) {
      sum+=ver/i;
      ver+=i;
      }
      System.out.println(sum);
      }
      }


      题目23:求1+2!+3!+…+20!的和

        package com.example.test;


        public class Test23 {
        public static void main(String[] args) {
        long sum=0,var=1;
        for(int i=1;i<=5;i++) {
        var=var*i;
        sum+=var;
        }
        System.out.println(sum);
        }
        }


        题目24:利用递归方法求5!。

          package com.example.test;


          public class Test24 {
          public static void main(String[] args) {
          System.out.println(fac(5));
          }
          public static int fac(int i) {
          if(i==1){
          return 1;
          }else {
          return i*fac(i-1);
          }
          }
          }


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

            package com.example.test;


            public class Test25 {
            public static void main(String[] args) {
            int age=10;
            for(int i=2;i<=5;i++) {
            age+=2;
            }
            System.out.println("第五个人" + age + "岁");
            }
            }


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

              package com.example.test;
              import java.util.Scanner;
              public class Test26 {
              public static void main(String[] args) {
              Scanner input=new Scanner(System.in);
              System.out.println("请输入:");
              String toString=input.nextLine();
              char[] num=toString.toCharArray();
              System.out.println("输入的是" + num.length + "位数");
              for(int i=num.length;i>0;i--) {
              System.out.print(num[i-1]);
              }
              }
              }


              题目27:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。

                package com.example.test;
                import java.util.*;
                public class Test27 {
                public static void main(String[] args) {
                getChar tw = new getChar();
                System.out.println("请输入星期的第一个大写字母:");
                char ch = tw.getChar();
                switch(ch) {
                case 'M':
                System.out.println("Monday");
                break;
                case 'W':
                System.out.println("Wednesday");
                break;
                case 'F':
                System.out.println("Friday");
                break;
                case 'T': {
                System.out.println("请输入星期的第二个字母:");
                char ch2 = tw.getChar();
                if(ch2 == 'U'){System.out.println("Tuesday"); }
                else if(ch2 == 'H') {System.out.println("Thursday");}
                else {System.out.println("无此写法!");}
                };
                break;
                case 'S': {
                System.out.println("请输入星期的第二个字母:");
                char ch2 = tw.getChar();
                if(ch2 == 'U'){System.out.println("Sunday"); }
                else if(ch2 == 'A'){System.out.println("Saturday"); }
                else {System.out.println("无此写法!");}
                };
                break;
                default:System.out.println("无此写法!");
                }
                }
                static class getChar{
                public char getChar() {
                Scanner s = new Scanner(System.in);
                String str = s.nextLine();
                char ch = str.charAt(0);
                if(ch<'A' || ch>'Z') {
                System.out.println("输入错误,请重新输入");
                ch=getChar();
                }
                return ch;
                }
                }
                }




                题目28:求100之内的素数

                  package com.example.test;
                  public class Test28 {
                  public static void main(String[] args) {
                  int a = 1;
                  for (int i = 2; i <= 100 ; i++) {
                  for (int j = 2; j < i ; j++) {
                  a = i%j;
                  if(a == 0){
                  break;
                  }
                  }
                  if (a != 0){
                  System.out.println(i+"是素数");
                  }
                  }
                  }
                  }


                  题目29:将一个数组逆序输出

                    package com.example.test;
                    public class Test29 {
                    public static void main(String[] args) {
                    int[] arr = {1,2,3,4,5,6};
                    for (int i = arr.length-1; i >= 0; i--) {
                    System.out.print(arr[i] + " ");
                    }
                    }
                    }


                    题目30:取一个整数a从左端开始的4~7位。

                      package com.example.test;


                      import java.util.Scanner;
                      public class Test30 {
                      public static void main(String[] args) {
                      Scanner input=new Scanner(System.in);
                      System.out.print("请输入一个7位以上的正整数:");
                      String toString=input.nextLine();
                      char[] a=toString.toCharArray();
                      int j=a.length;
                      if(j<7) {
                      System.out.println("error!");
                      }
                      System.out.println(a[3]+""+a[4]+""+a[5]+""+a[6]);
                      }
                      }


                      题目31:打印出杨辉三角形(要求打印出10行)

                        package com.example.test;
                        public class Test31 {
                        public static void main(String[] args) {
                        int[][] a = new int[10][10];
                        for(int i=0; i<10; i++) {
                        a[i][i] = 1;
                        a[i][0] = 1;
                        }
                        for(int i=2; i<10; i++) {
                        for(int j=1; j<i; j++) {
                        a[i][j] = a[i-1][j-1] + a[i-1][j];
                        }
                        }
                        for(int i=0; i<10; i++) {
                        for(int k=0; k<2*(10-i)-1; k++) {
                        System.out.print(" ");
                        }
                        for(int j=0; j<=i; j++) {
                        System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                        }
                        }
                        }


                        题目32:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+…+1/n,当输入n为奇数时,调用函数1/1+1/3+…+1/n(利用指针函数)

                          package com.example.test;
                          import java.util.Scanner;
                          public class Test32 {
                          public static void main(String[] args) {
                          Scanner input =new Scanner(System.in);
                          System.out.println("输入n的值:");
                          int n=input.nextInt();
                          System.out.println(sum(n));
                          }
                          public static double sum(int n) {
                          double sum=0;
                          if(n%2==0) {
                          for(int i=2;i<=n;i+=2) {
                          sum+=(double)1/i;
                          }
                          }else {
                          for(int i=1;i<=n;i+=2) {
                          sum+=(double)1/i;
                          }
                          }
                          return sum;
                          }
                          }


                          题目33:字符串排序。

                            package com.example.test;


                            import java.util.ArrayList;
                            import java.util.Collection;
                            import java.util.Collections;
                            public class Test33 {
                            public static void main(String[] args) {
                            ArrayList<String> list = new ArrayList<>();
                            list.add("2222");
                            list.add("5555");
                            list.add("3333");
                            Collections.sort(list);
                            for (int i = 0; i < list.size(); i++) {
                            System.out.println(list.get(i));
                            }


                            }
                            }


                            题目34:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

                              package com.example.test;


                              import java.util.*;
                              public class Test34 {
                              public static void main(String args[]) {
                              Scanner s = new Scanner(System.in);
                              int num=0,temp;
                              do{
                              System.out.print("请输入一个4位正整数:");
                              num = s.nextInt();
                              }while (num<1000||num>9999);
                              int a[]=new int[4];
                              a[0] = num/1000; //取千位的数字
                              a[1] = (num/100)%10; //取百位的数字
                              a[2] = (num/10)%10; //取十位的数字
                              a[3] = num%10; //取个位的数字
                              for(int j=0;j<4;j++){
                              a[j]+=5;
                              a[j]%=10;
                              }
                              for(int j=0;j<=1;j++){
                              temp = a[j];
                              a[j] = a[3-j];
                              a[3-j] =temp;
                              }
                              System.out.print("加密后的数字为:");
                              for(int j=0;j<4;j++)
                              System.out.print(a[j]);
                              }
                              }


                              题目35:输入正整数n,判断从1到n之中,数字1一共要出现几次。例如1123这个数,则出现了两次1。例如15,那么从1到15之中,一共出现了8个1。

                                package com.example.test;
                                import java.util.Scanner;
                                public class Test35 {
                                public static void main(String[] args) {
                                System.out.println("输入一个正整数:");
                                Scanner scanner = new Scanner(System.in);
                                int n = scanner.nextInt();
                                int num = 0;
                                for (int i = 0; i <=n ; i++) {
                                String str = String.valueOf(i);
                                for (int j = 0; j < str.length(); j++) {
                                if(String.valueOf(str.charAt(j)).equals("1")) {
                                num = num + 1;
                                }
                                }
                                }
                                System.out.println("您输入的正整数有"+num+"个1");


                                }
                                }


                                题目36:打印斐波拉契数列

                                  package com.example.test;
                                  //方法一
                                  public class Test36_1 {
                                  public static void main(String[] args) {
                                  int a = 0;
                                  int b = 1;
                                  for(int i = 1;i <= 5;i++) {
                                  //循环打印a,b两个数,即两个两个打印
                                  System.out.print(a + "\t" + b + "\t");
                                  //打印第三、四个数
                                  a = a + b;
                                  b = a + b;
                                  }
                                  }
                                  }


                                  //方法二
                                  public class Test36_2 {
                                  public static void main(String[] args) {


                                  //建立一个长度为10的数组用于存放数列中的数
                                  int[] arr = new int[10];
                                  //先定义数列中的第一个和第二个数
                                  arr[0] = 1;
                                  arr[1] = 1;
                                  //建立一个for循环,打印数组中的元素
                                  for(int i = 0;i < arr.length;i++) {
                                  //判断:当打印第三个数前,给第三个数赋值
                                  if(i > 1) {
                                  arr[i] = arr[i - 2] + arr[i - 1];
                                  }
                                  System.out.print(arr[i] + "\t");
                                  }
                                  }
                                  }


                                  //方法三
                                  public class Test36_3 {
                                  //建立一个函数,用于计算数列中的每一项
                                  public static int fib(int num) {
                                  //判断:是否是第一个数和第二个数
                                  if(num == 1 || num == 2) {
                                  return 1;
                                  }else {
                                  //循环调用本函数
                                  return fib(num - 2) + fib(num - 1);
                                  }
                                  }


                                  //主函数(程序入口)
                                  public static void main(String[] args) {


                                  //建立一个for循环,用于打印第一个至第十个数字
                                  for(int i = 1;i <= 10;i++) {
                                  Test36_3 test36_3 = new Test36_3();
                                  //调用函数进行打印
                                  System.out.print(test36_3.fib(i) + "\t");
                                  }
                                  }
                                  }


                                  题目37:产生10个10内的随机数

                                    package com.example.test;
                                    import java.util.Random;
                                    public class Test37 {
                                    public static void main(String[] args) {


                                    //实例化Random()
                                    Random r = new Random();
                                    for (int i = 0; i <10 ; i++) {
                                    int b1 = r.nextInt(10);
                                    System.out.print(b1);
                                    System.out.print(" ");
                                    }
                                    System.out.println();
                                    //调用Math函数
                                    for (int i = 0; i <10 ; i++) {
                                    int b2 = (int)(Math.random()*10);
                                    System.out.print(b2);
                                    System.out.print(" ");
                                    }
                                    }
                                    }


                                    题目38:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩) ,计算出平均成绩,把原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。

                                      package com.example.test;


                                      import java.io.*;
                                      import java.util.*;
                                      public class Test38 {
                                      public static void main(String[] args){
                                      Scanner ss = new Scanner(System.in);
                                      String [][] a = new String[5][6];
                                      for(int i=1; i<6; i++) {
                                      System.out.print("请输入第"+i+"个学生的学号:");
                                      a[i-1][0] = ss.nextLine();
                                      System.out.print("请输入第"+i+"个学生的姓名:");
                                      a[i-1][1] = ss.nextLine();
                                      for(int j=1; j<4; j++) {
                                      System.out.print("请输入该学生的第"+j+"个成绩:");
                                      a[i-1][j+1] = ss.nextLine();
                                      }
                                      System.out.println("\n");
                                      }
                                      //以下计算平均分
                                      float avg;
                                      int sum;
                                      for(int i=0; i<5; i++) {
                                      sum=0;
                                      for(int j=2; j<5; j++) {
                                      sum=sum+ Integer.parseInt(a[i][j]);
                                      }
                                      avg= (float)sum/3;
                                      a[i][5]=String.valueOf(avg);
                                      }
                                      //以下写磁盘文件
                                      String s1;
                                      try {
                                      File f = new File("C:\\stud");
                                      if(f.exists()){
                                      System.out.println("文件存在");
                                      }else{
                                      System.out.println("文件不存在,正在创建文件");
                                      f.createNewFile();//不存在则创建
                                      }
                                      BufferedWriter output = new BufferedWriter(new FileWriter(f));
                                      for(int i=0; i<5; i++) {
                                      for(int j=0; j<6; j++) {
                                      s1=a[i][j]+"\r\n";
                                      output.write(s1);
                                      }
                                      }
                                      output.close();
                                      System.out.println("数据已写入c盘文件stud中!");
                                      } catch (Exception e) {
                                      e.printStackTrace();
                                      }
                                      }
                                      }



                                      文章转载自CodeJie,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                                      评论