Some code for initial Java

1. The number of occurrences of 9 (in 0-100)

public class TestDemo {
    public static void main(String[] args) {
        //求0-100中9出现的次数
        int i=0;
       int count=0; //利用count进行计数
       for(i=0;i<=100;i++){
           if(i%10==9){
               count++;
           }
           if(i/10==9){//此处不能用else if(因为99有两个9需要被计算两次)
               count++;
           }
       }
        System.out.println("0-100中9出现的次数为:"+count);
    }
}

2. Output all leap years in 1000-2000

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("1000-2000中闰年如下:");
        int i = 1000;
        for (i = 1000; i <= 2000; i++) {
            //加括号更好理解;前者为公历闰年;后者为世纪闰年
            if (((i % 4 == 0) && (i % 100) != 0) || (i % 400 == 0)) {
                System.out.println(i);
            }
        }
    }
}

3. Print prime numbers (all prime numbers within 0-100)

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("素数如下:");
    int i=1;
    int j=1;
    for(i=1;i<=100;i++){
        int count=0;
        for(j=1;j<=i;j++){
            if(i%j==0){
                count++;
            }
        }
        if(count==2){
            //一个素数只有1和它本身两个因数;
            //上面的循环也是从1开始到其本身结束,与此处形成对应
            System.out.println(i);
    }
    }
    }
}

4. Enter a number to determine whether it is a prime number

import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("请输入一个数并判断其是否为素数:");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        suShu(n);
    }

    public static void suShu(int x) {
        int i = 2;
        for (i = 2; i < x; i++) {
            if (x % i == 0)
                System.out.println(x+"不是素数");
        }
            System.out.println(x + "是素数");
        }
    }

5. Print the X-shape

import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("打印X型:");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        daYin(n);
    }

    public static void daYin(int x) {
        int i = 0;
        int j = 0;
        for (i = 0; i < x; i++) {
            for (j = 0; j < x; j++) {
                if (i == j) {
                    System.out.println("*");
                } else if (i + j
                        == x - 1) {
                    System.out.println("*");
                }else
            System.out.println(" ");
            }
        }
        System.out.println();}
}

6. Calculate the score

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("请计算1-1/2+1/3-1/4+1/5+...+...1/99-1/100的结果");
        int i=1;
         double sum=0.0;
         int flg=1;
          for(i=1;i<=100;i++){
              sum=sum+1.0/i*flg;//先思考连加的形式,再在加的基础上更改成为减
              flg=-flg;
          }
        System.out.println(sum);
}

}

 7. Daffodil (enter an integer so that it satisfies the law of daffodils; for example: 153=1^3+5^3+3^3)

import java.util.Scanner;
public class TestDemo {
    public static void main(String[] args) {
        System.out.println("请输入一个数,求得0-这个数所有满足水仙花的数如下:");
        //求是几位数,利用count进行计数
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        findNum(n);
    }

    public static void findNum(int n) {

        for (int i = 0; i <= n; i++) {
            //count从1开始是因为数至少都是一位数
            int count = 1;
            int tmp = i;
            while (tmp / 10 != 0) {
                count++;
                tmp /= 10;
            }//用临时变量tmp是为了确保i值的不变性,为后续i继续计算做铺垫
            tmp =i;
            int sum = 0;
            while (tmp != 0) {
                //Math.pow(底数, 指数);
                //Math.pow(a, 3);等价于   a*a*a;
                sum += Math.pow(tmp % 10, count);
                tmp /= 10;
            }
            if (sum == i ){
                System.out.println(i);
            }
        }
    }
}


8. Greatest common divisor (method: use tossing and dividing method)

Dividing by turning and turning : that is, dividing a larger number by a smaller number, taking the divisor as the dividend, the remainder as the divisor, and dividing again until the remainder

import java.util.Scanner;
public class TestDemo {
    public static void main(String[] args) {
        System.out.println("请输入两个数,并求出两个数里的最大公约数:");
   Scanner sc=new Scanner(System.in);
   int n1=sc.nextInt();
   int n2=sc.nextInt();
   int tmp=0;
   while(n1%n2!=0){
       tmp=n1%n2;
       n1=n2;
       n2=tmp;
   }

        System.out.println(n2);

   }

}

The divisor found when zero is the greatest common divisor

9. The number of 1's in binary

import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("请输入一个数,并求出它的二进制中1的个数");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println(geShu(n));
}
public static int geShu(int x){
    int count=0;
    while((x&1)!=0){
        count++;
        x=x>>>1;//>>>相比于>>的好处在于无符号的限制,就可以计算类似于-1的位数
    }
   return count;


    }
}

The principle is as follows 

 10. Simulated login (you can log in three passwords, enter if you enter it successfully, and announce the end if you fail to enter it three times)

import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("请输入你的密码");
        login();
    }

    public static void login() {
        Scanner sc = new Scanner(System.in);
        int count = 3;
        while (count != 0) {
            String str = sc.nextLine();//每次输入的需要,确保密码错误的时候可以再输入的保证
            if (str.equals("123456")) {
             //比较两个字符串是否相等,不能直接用=进行连接。
             //要调用equals函数进行调用
                System.out.println("密码正确!");
               break;
            } else {
                count--;
                System.out.println("密码错误,你还有" + count + "次机会");

            }


        }
    }
}

11. Output each bit of a number (note that the output is in reverse order here)

import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("请输入一个数字,并倒序输出它的每一位如下:");
   Scanner sc=new Scanner(System.in);
     int n=sc.nextInt();
     everyOne(n);
}
public static void everyOne(int x){
        while(x!=0){
            System.out.println(x%10);//求出末位
           x/=10;//缩小一位数,比如,百位变成十位
        }

    }
}

12. Find the multiplication table of n*n (the multiplication table of n*n can be generated correspondingly after inputting a number)

import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("请输入一个数n,并求由这个数n构成的n*n的乘法表");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i=1;
        int j=1;
        for(i=1;i<=n;i++){
            for(j=1;j<=i;j++){
                int ret=i*j;
                System.out.println(i+"*"+j+"="+ret);
            }
        }
        System.out.println();
    }
}

13. Find Numbers That Occur Once

import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("找出不同的数字");
        int []array={1,2,3,2,1};
        int n=array[0];
        int i=0;
        for(i=1;i< array.length;i++){
            n=n ^array[i];
        }
        System.out.println("只出现一次的数字为:"+n);
    }
}

14. Enter a number and find its factorial

import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("输入一个数n并求它的阶乘:");
   Scanner sc=new Scanner(System.in);
   int n=sc.nextInt();
      int i=1;
      int ret=1;
      for(i=1;i<=n;i++){
          ret*=i;
      }
        System.out.println(ret);
    }
}

15. Find the sum of factorials

import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("输入一个数n并求1到n的阶乘之和:");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(allSum(n));
    }
//求某个数的阶乘
    public static int fac(int x) {
        int i = 1;
        int ret = 1;
        for (i = 1; i <= x; i++) {
            ret *= i;
        }
        return ret;
    }
//求阶乘之和
    public static int allSum(int x) {
        int sum = 0;
        int i = 0;
        for (i = 1; i <= x; i++) {
            sum = sum +fac(i);}
        return sum;
    }
}

16. Make odd numbers come before even numbers

import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {
        //调整数组顺序使得奇数位于偶数之前。
        int[] array = {1, 2, 3, 4, 5};
        System.out.println("数组交换前:");
        printNum(array);
        paiXu(array);
        System.out.println("交换数组后:");
        printNum(array);
    }

    public static void printNum(int[] array) {
        int i = 0;
        for (i = 0; i < array.length; i++) {
            System.out.print(array[i]);
        }
        System.out.println();

    }

    public static void paiXu(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            while (array[left] % 2 != 0 && left < right) {
                left++;
            }
            while (array[right] % 2 == 0 && left < right) {
                right--;
            }
            if (left < right) {
                int tmp = array[left];
                array[left] = array[right];
                array[right] = tmp;
            }

        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_58850105/article/details/121016587