Lanqiao Cup Java Group Basic Exercise Questions 50 Questions

1. Classical problem: There is a pair of rabbits. They give birth to a pair of rabbits every month from the third month after birth. After the baby rabbit grows to the third month, it gives birth to a pair of rabbits every month. If the rabbits do not die, what is the question? What is the total number of rabbits per month?

//This is a Fibonacci sequence problem 

public class test1 {
    public static void main(String args[]){
        int f1=2,f2=2,temp;
        System.out.printf("第1个月有2只兔子\n");
        System.out.printf("第2个月有2只兔子\n");
        for(int i=3;i<=24;i++){
            temp=f2;
            f2=f1+f2;
            f1=temp;
            System.out.printf("第%d个月有%d只兔子\n",i,f2);
        }
    }
}

2. Determine how many prime numbers there are between 101-200 and output all prime numbers.

public class test2 {
    public static void main(String args[]){
        int k=0;
        for(int i=101;i<=200;i++){
            if(check(i)==1){
                System.out.println(i);
                k++;
            }
        }
        System.out.printf("有%d个素数",k);
    }
    public static int check(int n){
        int flag=0;
        for(int i=2;i<n;i++){
            if(n%i==0){
                flag=0;
                break;
            }
            else{
                flag=1;
            }
        }
        return flag;
    }
}

3. Print out all the "narcissus numbers". The so-called "narcissus number" refers to a three-digit number whose cube sum is equal to the number itself. For example: 153 is a "narcissus number" because 153=1 cubed + 5 cubed + 3 cubed.

public class test3 {
    public static void main(String args[]){
        for(int i=100;i<1000;i++){
            if(Math.pow((i/1%10),3)+Math.pow((i/100%10),3)+Math.pow((i/10%10),3)==i){
                System.out.println(i);
            }
        }
    }
}

4. Factor a positive integer into prime factors. For example: input 90, print out 90=2*3*3*5.

public class test4 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n= sc.nextInt();
        System.out.printf("%d=",n);
        int k=2;
        while(k<=n){
            if(k==n){
                System.out.println(n);
                break;
            }
            else if(n%k==0){
                System.out.printf(k+"*");
                n=n/k;
            }
            else{
                k++;
            }
        }
    }
}

5. Use the nesting of conditional operators to complete this question: students with academic scores >= 90 points are represented by A, students with scores between 60-89 are represented by B, and students with scores below 60 are represented by C.

public class test5 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        char grade;
        int x=sc.nextInt();
        grade=x>=90?'A':x>=60?'B':'C';
        System.out.println("等级为:"+grade);
    }
}

6. Input two positive integers m and n, and find their greatest common divisor and least common multiple.

public class test6 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        int max,n=a*b;
        int temp;
        if(a<b){  //把a当做大数
            temp=a;
            a=b;
            b=temp;
        }
        while(b!=0){
            if(a==b){
                max=a;
            }
            else{
                int k=a%b;
                a=b;
                b=k;
            }
        }
        max=a;
        int min=n/max;
        System.out.printf("最大公约数:%d,最小公倍数:%d",max,min);
    }
}

7. Enter a line of characters and count the number of English letters, spaces, numbers and other characters in it.

public class test7 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int shuzi=0,zimu=0,kongge=0,qita=0;
        char[] c=null;
        String s=sc.nextLine();
        c=s.toCharArray();
        for(int i=0;i<c.length;i++){
            if('0'<=c[i]&&c[i]<='9'){
                shuzi++;
            }
            else if(('a'<=c[i]&&c[i]<='z')||('A'<=c[i]&&c[i]<='Z')){
                zimu++;
            }
            else if(c[i]==' '){
                kongge++;
            }
            else{
                qita++;
            }
        }
        System.out.printf("数字:%d,字母:%d,空格:%d,其他:%d",shuzi,zimu,kongge,qita);
    }
}

8. Find the value of s=a+aa+aaa+aaaa+aa...a, where a is a number. For example, 2+22+222+2222+22222 (a total of 5 numbers are added at this time), and the addition of several numbers is controlled by the keyboard.

public class test8 {
    public static void main(String srgs[]){
        Scanner sc=new Scanner(System.in);
        System.out.printf("这个数是:");
        int a=sc.nextInt();
        System.out.printf("几个这个数相加:");
        int n=sc.nextInt();
        int sum=0,b=0;
        for(int i=1;i<=n;i++){
            b=b+a;
            sum=sum+b;
            a=a*10;
            System.out.println(b);
        }
        System.out.println(sum);
    }
}

9. If a number is exactly equal to the sum of its factors, the number is called a "perfect number". For example, 6=1+2+3. Program to find all perfect numbers within 1000.

public class test9 {
    public static void main(String args[]) {
        for(int i=1;i<1000;i++){
            int sum=0;
            for(int j=1;j<i;j++){
                if(i%j==0){
                    sum=sum+j;
                }
            }
            if(sum==i){
                System.out.println(i+" ");
            }
        }
    }
}

10. A ball falls freely from a height of 100 meters. Each time it hits the ground, it bounces back to half of its original height. When it falls again, how many meters does it pass in total when it hits the ground for the 10th time? How high is the 10th rally?

public class test10 {
    public static void main(String args[]){
        double a=100,c=100;
        for(int i=1;i<=10;i++){
            c=c+a;
            a=a/2;
        }
        System.out.printf("共经过"+c+"米\n");
        System.out.printf("第10次反弹"+a+"米");
    }
}

11. There are four numbers 1, 2, 3, and 4. How many different three-digit numbers can be formed without repeated numbers? How many are they?

public class test11 {
    public static void main(String args[]){
        int count=0;
        for(int i=1;i<5;i++){
            for(int j=1;j<5;j++){
                for(int k=1;k<5;k++){
                    if(i!=j&&i!=k&&j!=k){
                        System.out.println(i*100+j*10+k);
                        count++;
                    }
                }
            }
        }
        System.out.println("共有"+count+"个数");
    }
}

12. The bonuses issued by the company are based on profits. When the profit (I) is less than or equal to 100,000 yuan, the bonus can be 10%; when the profit is more than 100,000 yuan and less than 200,000 yuan, the portion less than 100,000 yuan is 10%, and if the profit is more than 100,000 yuan, the bonus is 10%. For the part between 200,000 and 400,000, the commission is 5% for the part above 200,000 yuan; for the part above 400,000 yuan between 400,000 and 600,000, the commission is 3% ; When it is between 600,000 and 1 million, the portion above 600,000 yuan can be commissioned at 1.5%. When it is above 1 million yuan, the portion over 1 million yuan is 1% commission. Enter the profit for the month from the keyboard and ask for payment. Total bonus?

public class test12 {
    public static void main(String args[]){
        double jiangjin=0;
        Scanner sc=new Scanner(System.in);
        double x=sc.nextDouble();
        if(10<=x){
            jiangjin=x*0.1;
        }
        if(10<x&&x<=20){
            jiangjin=10*0.1+(x-10)*0.075;
        }
        if(20<x&&x<=40){
            jiangjin=10*0.1+10*0.075+(x-20)*0.05;
        }
        if(40<x&&x<=60){
            jiangjin=10*0.1+10*0.075+20*0.05+(x-40)*0.03;
        }
        if(60<x&&x<=100){
            jiangjin=10*0.1+10*0.075+20*0.05+20*0.03+(x-60)*0.015;
        }
        if(100<x){
            jiangjin=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(x-100)*0.01;
        }
        System.out.print(jiangjin);
    }
}

13. An integer, after adding 100, it becomes a perfect square number, and when added to 168, it becomes a perfect square number. What is this number?

//Note that negative numbers are also integers, and the loop starts with negative numbers

public class test13 {
    public static void main(String args[]){
        for(int i=-100;i<100000;i++){
            if(check(i+100)==1){
                if(check(i+268)==1) {
                    System.out.println(i);
                }
            }
        }
    }
    public static int check(int n){
        int flag=0;
        if(Math.sqrt(n)%1==0){
            flag=1;
        }
        return flag;
    }
}

14. Enter a certain year, a certain month and a certain day, and determine what day of the year this day is? 

public class test14 {
    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 sum=0,sum1;
        if (year % 400 == 0 ||(year%4==0&&year%100==0)) {
            int[] ch29=new int[]{31,29,31,30,31,30,31,31,30,31,30,31};
            for(int i=0;i<month-1;i++){
                sum=sum+ch29[i];
            }
            sum1=sum+day;
            System.out.println(sum1);
        }
        else{
            int[] ch28=new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
            for(int i=0;i<month-1;i++){
                sum=sum+ch28[i];
            }
            sum1=sum+day;
            System.out.println(sum1);
        }
    }
}

15. Arrange the sizes of the three numbers x, y and z

public class test15 {
    public static void main(String args[]){
        int temp;
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        int y=sc.nextInt();
        int z=sc.nextInt();
        if(x>y){
            temp=y;
            y=x;
            x=temp;
        }
        if(x>z){
            temp=z;
            z=x;
            x=temp;
        }
        if(y>z){
            temp=z;
            z=y;
            y=temp;
        }
        System.out.printf("%d ",x);
        System.out.printf("%d ",y);
        System.out.printf("%d",z);
    }
}

16. Print the multiplication table

public class test16 {
    public static void main(String args[]){
        for(int i=1;i<10;i++){
            System.out.println("\n");
            for(int j=1;j<=i;j++){
                System.out.printf(j+"*"+i+"="+i*j+" ");
            }
        }
    }
}

17. Monkey eating peach problem: The monkey picked a few peaches on the first day, ate half of them immediately, and was not satisfied. He ate one more. The next morning, he ate half of the remaining peaches and one more. . From then on, every morning I ate half and one of the leftovers from the previous day. When I wanted to eat more on the morning of the 10th day, I saw that there was only one peach left. Find out how many were picked on the first day.

//Reverse thinking, (number of peaches on the next day + 1)*2 to get the number of peaches left on the previous day

public class test17 {
    public static void main(String args[]){
        int x=1;
        for(int i=9;i>=1;i--){
            x=(x+1)*2; /*第一天的桃子数是第二天桃子数加1后的2倍*/
        }
        System.out.println(x);
    }
}

18. Two table tennis teams compete, each with three players. Team A consists of three people a, b, and c, and team B consists of three people x, y, and z. The match list has been drawn. Someone asked the players about the roster for the game. a says that he cannot compete with x, and c says that he cannot compete with x and z. Please program a program to find the list of players from the three teams.

public class test18 {
    public static void main(String args[]){
        char[] ch1=new char[]{'a','b','c'};
        char[] ch2=new char[]{'x','y','z'};
        for(int i=0;i<ch1.length;i++){
            for(int j=0;j<ch2.length;j++){
                if(ch1[i]=='a'&&ch2[j]=='x'||ch1[i]=='a'&&ch2[j]=='y'){
                    continue;
                }
                else if(ch1[i]=='b'&&ch2[j]=='y'||ch1[i]=='b'&&ch2[j]=='z'){
                    continue;
                }
                else if(ch1[i]=='c'&&ch2[j]=='x'||ch1[i]=='c'&&ch2[j]=='z'){
                    continue;
                }
                else{
                    System.out.printf(ch1[i]+" vs "+ch2[j]+"\n");
                }
            }
        }
    }
}

19. Print out the following pattern (diamond)

public class test19 {
    public static void main(String args[]){
        int gao=7,kuan=7;//相同的奇数
        for(int i=1;i<=(gao+1)/2;i++){//包括最宽行以上面组成的三角形
            for(int j=0;j<=kuan/2-i;j++){
                System.out.printf(" ");
            }
            for(int k=0;k<i*2-1;k++){
                System.out.printf("*");
            }
            System.out.println();
        }
        for(int i=1;i<=gao/2;i++){//不包括最宽行以下面组成的三角形
            for(int j=0;j<i;j++){
                System.out.printf(" ");
            }
            for(int k=0;k<kuan-i*2;k++){
                System.out.printf("*");
            }
            System.out.println();
        }
    }
}

20. There is a sequence of fractions: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13... Find the sum of the first 20 terms of this sequence.

public class test20 {
    public static void main(String args[]){
        double sum=0,fenzi=2,fenmu=1,t;
        for(int i=1;i<=20;i++){
            sum=sum+fenzi/fenmu;
            t=fenmu;
            fenmu=fenzi;
            fenzi=fenzi+t;
        }
        System.out.println(sum);
    }
}

21. Find the sum of 1+2!+3!+...+20!

public class test21 {
    public static void main(String args[]){
        long sum=0;
        for(int i=1;i<=20;i++){
            sum=check(i);
        }
        System.out.println(sum);
    }
    public static long check(int n){
        long a=1,sum=0;
        if(n==0){
            sum=1;
        }
        else {
            for (int i = 1; i <= n; i++) {
                a = a * i;
                sum = sum + a;
            }
        }
        return sum;
    }
}

22. Use recursive method to find 5! 

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

23. There are 5 people sitting together and ask how old the fifth person is? He said he was 2 years older than the 4th guy. Asked the 4th person how old he was, he said he was 2 years older than the 3rd person. Asked the third person and said he was two years older than the second person. Ask the second person and say he is two years older than the first person. Finally asked the first person and he said he was 10 years old. How old is the fifth person?

public class test23 {
    public static void main(String args[]){
        int a=10;
        for(int i=2;i<=5;i++){
            a=a+2;
        }
        System.out.println(a);
    }
}

24. Given a positive integer with no more than 5 digits, the requirements are: 1. Find out how many digits it has, and 2. Print out the digits in reverse order.

import java.util.Scanner;

public class test24 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        long n=sc.nextLong();
        String s=Long.toString(n);
        char[] ch=s.toCharArray();
        if(ch.length<5&&n%1==0) {
            System.out.println("这个数是"+ch.length+"位数");
            for (int i = ch.length - 1; i >= 0; i--) {//注意i=ch.length-1,要用长度-1,否则会数组越界
                System.out.print(ch[i]);
            }
        }
        else{
            System.out.printf("请输入不多于5位的正整数");
        }
    }
}

25. A 5-digit number, determine whether it is a palindrome number. That is, 12321 is a palindrome number, the ones digit is the same as the thousands digit, and the tens digit is the same as the thousands digit.

import java.util.Scanner;

public class test25 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        if(n>10000&&n<100000){
            if(n/1%10==n/10000%10&&n/10%10==n/1000%10){
                System.out.printf(n+"是回文数");
            }
        }
        else{
            System.out.printf("请输入一个5位正整数");
        }
    }
}

26. Please enter the first letter of the day of the week to determine the day of the week. If the first letters are the same, continue to determine the second letter.

import java.util.Scanner;

public class test26 {
    public static void main(String args[]){
        System.out.printf("请输入第一个字母:");//默认第一个字母大写第二个字母小写
        Scanner sc=new Scanner(System.in);
        char n=sc.next().charAt(0);//charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。
        if(n=='M'){
            System.out.println("Monday");
        }
        else if(n=='W'){
            System.out.println("Wednesday");
        }
        else if(n=='F'){
            System.out.println("Friday");
        }
        else if(n=='T'){
            System.out.printf("请输入第二个字母:");
            Scanner sc1=new Scanner(System.in);
            char n1=sc1.next().charAt(0);
            if(n1=='u'){
                System.out.println("Tuesday");
            }
            if(n1=='h'){
                System.out.println("Thursday");
            }
        }
        else if(n=='S'){
            System.out.printf("请输入第二个字母:");
            Scanner sc1=new Scanner(System.in);
            char n1=sc1.next().charAt(0);
            if(n1=='a'){
                System.out.println("Saturday");
            }
            if(n1=='u'){
                System.out.println("Sunday");
            }
        }
        else{
            System.out.println("无此写法");
        }
    }
}

27. Find prime numbers within 100 

public class test27 {
    public static void main(String args[]){
        for(int i=2;i<100;i++){
            if(check(i)==1){
                System.out.printf(i+" ");
            }
        }
    }
    public static int check(int n){
        int flag=1;
        for(int i=2;i<=Math.sqrt(n);i++){
            if(n%i==0){
                flag=0;
                break;
            }
        }
        return flag;
    }
}

28. Sort 10 numbers

import java.util.Scanner;

public class test28 {
    public static void main(String args[]){//默认升序(从小到大)
        Scanner sc=new Scanner(System.in);
        int[] a=new int[10];
        for(int i=0;i<10;i++){
            a[i]=sc.nextInt();
        }
        for(int i=0;i<a.length-1;i++){
            for(int j=0;j<a.length-1;j++){
                if(a[j]>a[j+1]){//把>改成<就是降序(从大到小)
                    int t=a[j+1];
                    a[j+1]=a[j];
                    a[j]=t;
                }
            }
        }
        for(int k=0;k<=a.length-1;k++){
            System.out.printf(a[k]+" ");
        }
    }
}

29. Find the sum of the diagonal elements of a 3*3 matrix

//I added the elements of the main and secondary diagonals

import java.util.Scanner;

public class test29 {
    public static void main(String args[]){
        int sum=0;
        Scanner sc=new Scanner(System.in);
        int[][] a=new int[3][3];
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                a[i][j]=sc.nextInt();
                System.out.printf("a["+i+"]["+j+"]="+a[i][j]+"\n");
            }
        }
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                System.out.printf(a[i][j]+" ");
            }
            System.out.println();
        }
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                if(i==j||i+j==2) {
                    sum=sum+a[i][j];
                }
            }
        }
        System.out.printf("对角线元素之和:"+sum);
    }
}

30. There is a sorted array. Now enter a number and insert it into the array according to the original rules. 

import java.util.Arrays;
import java.util.Scanner;

public class test30 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int num=sc.nextInt();
        int[] a=new int[]{1,4,23,67,99};//默认升序
        int[] b=new int[a.length+1];
        int t=0;
        for(int i=0;i<a.length;i++){
            if(a[i]<num){
                b[i]=a[i];
            }
            else{
                if (t == 0) {
                    b[i]=num;
                    t++;    //只添加一次num
                }
                b[i+1]=a[i];
            }
        }
        System.out.printf(Arrays.toString(b));
    }
}

31. Output an array in reverse order.

import java.util.Scanner;

public class test31 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int i=0;
        int[] a=new int[9999];
        do{
            a[i]=sc.nextInt();
            i++;
        }while(a[i-1]!=-1);
        for(int j=i-2;j>=0;j--){  //注意j=i-2,不是j=a.length-1,否则没有输入数字的地方会输出0
            System.out.printf(a[j]+" ");
        }
    }
}

32. Take the 4 to 7 digits of an integer a starting from the right end.

import java.util.Scanner;

public class test32 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        long n=sc.nextLong();
        String s=Long.toString(n);
        char[] ch=s.toCharArray();
        int i=ch.length;
        if(ch.length>=7){
            System.out.printf("截取从右端开始的4~7位数是:"+ch[i-7]+ch[i-6]+ch[i-5]+ch[i-4]);
            //System.out.print(ch[i-7]);
            //System.out.print(ch[i-6]);
            //System.out.print(ch[i-5]);
            //System.out.print(ch[i-4]);
        }
        else{
            System.out.println("请输入大于7位的正整数");
        }
    }
}

33. Print out Yang Hui’s triangle (it is required to print out 10 lines, as shown below)

public class test33 {
    public static void main(String args[]){
        int[][] ch=new int[10][];//10行,列数未知
        for(int i=0;i<ch.length;i++){
            ch[i]=new int[i+1];//ch[i]代表第几行
            for(int j=0;j<ch[i].length;j++){
                if(j==0||j==ch[i].length-1){
                    ch[i][j]=1;
                }
                else{
                    ch[i][j]=ch[i-1][j-1]+ch[i-1][j];
                }
            }
        }
        for(int i=0;i<ch.length;i++){
            for(int j=0;j<ch[i].length;j++){
                System.out.print(ch[i][j]+"\t");//“\t”为“转义字符“,代表的是一个tab,也就是8个空格。
            }                                   // \t是补全当前字符串长度到8的整数倍
            System.out.println();
        }
    }
}

34. Input 3 numbers a, b, c, and output them in order of size 

import java.util.Scanner;

public class test34 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        System.out.printf("输入三个数:");
        int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt();
        if(a>b){//默认升序
            int t=b;
            b=a;
            a=t;
        }
        if(a>c){
            int t1=c;
            c=a;
            a=t1;
        }
        if(b>c){
            int t2=c;
            c=b;
            b=t2;
        }
        System.out.printf(a+" "+b+" "+c);
    }
}

35. Input the array, exchange the largest element with the first element, exchange the smallest element with the last element, and output the array. 

import java.util.Scanner;

public class test35 {
    public static void main(String args[]){
        int index1=0,index2=0;
        int[] ch=new int[8];//数组长度为8
        Scanner sc=new Scanner(System.in);
        for(int i=0;i<8;i++){
            ch[i]=sc.nextInt();
        }
        int max=0,min=999;
        for(int i=0;i<ch.length;i++){
            if(ch[i]>max){
                max=ch[i];
                index1=i;
            }
        }
        for(int i=0;i<ch.length;i++){
            if(ch[i]<min){
                min=ch[i];
                index2=i;
            }
        }
        if(index1!=0){
            int t=ch[index1];
            ch[index1]=ch[0];
            ch[0]=t;
        }
        if(index2!=ch.length-1){
            int t2=ch[index2];
            ch[index2]=ch[7];
            ch[7]=t2;
        }
        for(int i=0;i<ch.length;i++){
            System.out.printf(ch[i]+" ");
        }
    }
}

36. There are n integers, and the previous numbers are moved backward by m positions, and the last m numbers become the first m numbers.

import java.util.Scanner;

public class test36 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        System.out.printf("n个数:");
        int n=sc.nextInt();
        int[] a=new int[n];
        System.out.printf("分别是:");
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
        System.out.printf("向后移m个位置:");
        int m=sc.nextInt();
        int[] b=new int[m];
        for(int i=0;i<m;i++){
            b[i]=a[n-m+i];
        }
        for(int i=n-1;i>=m;i--){
            a[i]=a[i-2];
        }
        for(int i=0;i<m;i++){
            a[i]=b[i];
        }
        for(int i=0;i<a.length;i++){
            System.out.printf(a[i]+" ");
        }
    }
}

37. There are n people standing in a circle and numbered in order. Start counting from the first person (counting from 1 to 3). Anyone who reports 3 exits the circle and asks who is the original number left at the end. 

import java.util.Scanner;

public class test37 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        System.out.printf("有n个人围成圈:");
        int n=sc.nextInt();
        boolean[] a=new boolean[n];
        for(int i=0;i<n;i++){
            a[i]=true;
        }
        int renshu=n;
        int baoshu=0;
        int index=0;
        while(renshu>1){
            if(a[index]){
                baoshu++;
                if(baoshu==3){
                    baoshu=0;
                    renshu--;
                    a[index]=false;
                }
            }
            index++;
            if(index==n){
                index=0;
            }
        }
        for(int i=0;i<n;i++){
            if(a[i]==true){
                System.out.printf("最后留下来的是原来的第"+(i+1)+"号\n");
            }
        }
    }
}

38. Write a function to find the length of a string. Enter the string in the main function and output its length.

import java.util.Scanner;

public class test38 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        String n=sc.nextLine();
        System.out.println(check(n));
    }
    public static int check(String n){
        return n.length();
    }
}

39. Write a function. When the input n is an even number, call the function to find 1/2+1/4+...+1/n. When the input n is an odd number, call the function 1/1+1/3+.. .+1/n (using pointer function)

import java.util.Scanner;

public class test39 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        System.out.println(check(n));
    }
    public static double check(int n){
        double ans=0;
        if(n%2==0){
            for(int i=2;i<n+2;i=i+2){
                ans=ans+1.0/i;
            }
        }
        else{
            for(int i=1;i<n+2;i=i+2){
                ans=ans+1.0/i;
            }
        }
        return ans;
    }
}

40. String sorting.

import java.util.Arrays;

public class test40 {
    public static void main(String args[]){
        String[] str=new String[]{"zc","ABc","za","Abc","dcs"};
        Arrays.sort(str);
        for(int i=0;i<str.length;i++){
            System.out.printf(str[i]+" ");
        }
    }
}

41. There are a bunch of peaches on the beach, and five monkeys come to divide them. The first monkey divided the pile of peaches into five parts. If there was one more, the monkey threw the extra one into the sea and took away one part. The second monkey divided the remaining peach into five equal parts, and there was one more. It also threw the extra peach into the sea and took one. The third, fourth, and fifth monkeys all did the same. Yes, how many peaches were there on the beach?

public class test41 {
    public static void main(String args[]) {
        int sum=0;
        for(int i=1;;i++){
            sum=i;
            if((sum-1)%5==0){
                sum=(sum-1)/5*4;//先丢掉一个,然后分成五份,拿走一份还剩四份
                if((sum-1)%5==0){
                    sum=(sum-1)/5*4;
                    if((sum-1)%5==0){
                        sum=(sum-1)/5*4;
                        if((sum-1)%5==0){
                            sum=(sum-1)/5*4;
                            if((sum-1)%5==0){
                                System.out.println(i);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}

42.809*??=800*??+9*??+1 where ?? represents a two-digit number, the result of 8*?? is a two-digit number, and the result of 9*?? is a three-digit number. Find the two-digit number represented by ?? and the result after 809*??.

public class test42 {
    public static void main(String args[]){
        for(int i=10;i<100;i++){
            if(10<=8*i&&8*i<100){
                if(100<=9*i&&9*i<10000){
                    if(809*i==800*i+9*i+1){
                        System.out.printf("答案:"+i);
                    }
                    else{
                        System.out.println("无解");
                    }
                }
            }
        }
    }
}

43. Find the odd numbers that can be formed from 0-7.

//The composition of 1 digit is 4.

//The composition of 2 digits is 7*4.

//The composition of 3 digits is 7*8*4.

//The composition of 4 digits is 7*8*8*4.

//...... 

public class test43 {
    public static void main(String args[]){
        long sum=32;
        System.out.printf("组成1位数的奇数:4个\n");
        System.out.printf("组成1位数的奇数:28个\n");
        for(int i=3;i<=8;i++){
            int n=7*((int)Math.pow(8,(i-2)))*4;
            sum=sum+n;
            System.out.printf("组成"+i+"位数的奇数:"+n+"个\n");
        }
        System.out.printf("和为:"+sum+"个\n");
    }
}

44. An even number can always be expressed as the sum of two prime numbers.

import java.util.Scanner;

public class test44 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        if(n%2==0){
            for(int i=2;i<n/2;i++){
                for(int j=i+1;j<n;j++){
                    if((check(i))==1&&(check(j)==1)){
                        if(i+j==n) {
                            System.out.printf(n + "=" + i + "+" + j + "\n");
                        }
                    }
                }
            }
        }
        else{
            System.out.printf("请输入一个偶数\n");
        }
    }
    public static int check(int n){
        int flag=1;
        for(int i=2;i<=Math.sqrt(n);i++){
            if(n%i==0){
                flag=0;
                break;
            }
        }
        return flag;
    }
}

45. Determine how many 9’s a prime number can be divisible by 

import java.util.Scanner;

public class test45 {//能被9整除不就不是素数了吗...
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        if(check1(n)==1){//判断是不是素数
            System.out.printf(n+"能被"+check2(n)+"个9整除\n");
        }
        else{
            System.out.printf("请输入一个素数");
        }
    }
    public static int check1(int n){
        int flag=1;
        for(int i=2;i<=Math.sqrt(n);i++){
            if(n%i==0){
                flag=0;
                break;
            }
        }
        return flag;
    }
    public static int check2(int n){//能被几个9整除
        int ans=0;
        if(n%9==0){
            ans++;
            n=n/9;
        }
        else{
            return ans;
        }
        return ans;
    }
}

46. ​​Two string concatenation procedures 

import java.util.Scanner;

public class test46 {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        System.out.printf("请输入两个字符串:\n字符串1:");
        String str1=sc.nextLine();
        System.out.printf("字符串2:");
        String str2=sc.nextLine();
        System.out.printf("连接这两个字符串:"+str1+str2);
    }
}

47. Read the integer values ​​of 7 numbers (1-50). Each time a value is read, the program prints out the number of * of the value. 

import java.util.Scanner;

public class test47 {
    public static void main(String args[]){
        System.out.printf("请输入7个1~50内的整数:\n");
        Scanner sc=new Scanner(System.in);
        int i=1,n;
        while(i<=7){
            do{
                System.out.printf("第"+i+"个数:");
                n=sc.nextInt();
            }while(1>n||n>50);
            for (int j = 1; j <= n; j++) {
                System.out.printf("*");
            }
            i++;
            System.out.println();
        }
    }
}

48. A company uses public phones to transmit data. The data is a four-digit integer, which is encrypted during the transmission process. The encryption rules are as follows: add 5 to each digit, and then replace the number with the remainder divided by 10. Then swap the first and fourth digits, and the second and third digits.

import java.util.Scanner;

public class test48 {
    public static void main(String args[]){
        System.out.printf("请输入一个四位数:");
        Scanner sc=new Scanner(System.in);
        int[] a=new int[4];
        int n=sc.nextInt();
        if(1000<=n&&n<10000) {
            int ge=n/1%10,shi=n/10%10,bai=n/100%10,qian=n/1000%10;
            a[0]=qian;
            a[1]=bai;
            a[2]=shi;
            a[3]=ge;
            for (int i = 0; i < a.length; i++) {
                a[i]=(a[i]+5)%10;
            }
            int t1=a[0];
            a[0]=a[3];
            a[3]=t1;
            int t2=a[1];
            a[1]=a[2];
            a[2]=t2;
            for (int i = 0; i < a.length; i++) {
                System.out.printf(a[i] + " ");
            }
        }
        else{
            System.out.printf("您输入的数不是四位数!");
        }
    }
}

49. Count the number of times a certain substring appears in a string 

import java.util.Scanner;

public class test49 {
    public static void main(String args[]){
        int num=0;
        System.out.printf("输入一个字符串:");
        Scanner sc=new Scanner(System.in);
        String str1=sc.nextLine();
        System.out.printf("输入该字符串的子串:");
        String str2=sc.nextLine();
        if(str1.equals("")||str2.equals("")){
            System.out.printf("你没有输入字符串或子串!");
        }
        else{
            for (int i = 0; i <= str1.length()-str2.length(); i++) {
                if (str1.substring(i, i+str2.length()).equals(str2)) {
                    num++;
                }
            }
            System.out.printf("子串"+str2+"在字符串"+str1+"中出现了"+num+"次");
        }
    }
}

50. There are five students, each student has grades in 3 courses. Enter the above data from the keyboard (including student number, name, grades of three courses), calculate the average grade, and combine the original data with the calculated average Scores are stored in the disk file "stud".

import java.io.*;
import java.util.*;

public class test50 {
    public static void main(String args[]){
        String s;
        int sum=0;
        float avg=0;
        Scanner sc=new Scanner(System.in);
        String[][] str=new String[5][6];
        for(int i=0;i<str.length;i++){
            System.out.printf("第"+(i+1)+"个学生学号:");
            str[i][0]=sc.nextLine();
            System.out.printf("第"+(i+1)+"个学生姓名:");
            str[i][1]=sc.nextLine();
            for(int j=2;j<5;j++){
                System.out.printf("第"+(j-1)+"个成绩:");
                str[i][j]=sc.nextLine();
            }
            System.out.println();
        }
        //计算平均数
        for(int i=0;i<str.length;i++){
            sum=0;
            for(int j=2;j<5;j++){
                sum=sum+Integer.parseInt(str[i][j]);
            }
            avg=(float)(sum)/3;
            str[i][5]=String.valueOf(avg);
            System.out.printf("%.2f",avg);
        }
        try{
            File file=new File("D://java//untitled3//src//file.txt");
            if(file.exists()){
                System.out.printf("这个文件已存在!");
            }
            else{
                System.out.printf("文件创建成功!");
                file.createNewFile();
            }
            BufferedWriter out=new BufferedWriter(new FileWriter(file));
            for(int i=0;i<str.length;i++){
                for(int j=0;j<6;j++){
                    s=str[i][j];
                    out.write(s+"\r\n");
                }
            }
            out.close();
            System.out.printf("数据已存入"+file.getPath());
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
}

Guess you like

Origin blog.csdn.net/s44Sc21/article/details/131699707#comments_28283884