自学内容网 自学内容网

java—循环语句

题目一:余数

715. 余数 - AcWing题库

代码 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        
        for(int i = 2; i < 10000; i ++) {
            if(i%n==2) System.out.println(i);
        }
    }
}

题目二:六个奇数

710. 六个奇数 - AcWing题库

代码 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int x = sc.nextInt();
        int flag = 0;
        
        while(flag < 6) {
            if(x%2==1) {
                System.out.println(x);
                flag ++;
            }
            x ++;
        }
    }
}

题目三:乘法表

711. 乘法表 - AcWing题库

 代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        
        for(int i = 1; i <= 10; i ++) {
            System.out.printf("%d x %d = %d\n",i,n,i*n);
        }
    }
}

题目四:实验

718. 实验 - AcWing题库

 代码(" ".equals(ch))

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int total = 0, cnum = 0, rnum = 0, fnum = 0;
        for(int i = 0; i < n; i ++) {
            int x; String ch;
            x = sc.nextInt(); ch = sc.next();
            total += x;
            if("C".equals(ch)) cnum += x;
            if("R".equals(ch)) rnum += x;
            if("F".equals(ch)) fnum += x;
        }
        System.out.printf("Total: %d animals\n",total);
        System.out.printf("Total coneys: %d\n",cnum);
        System.out.printf("Total rats: %d\n",rnum);
        System.out.printf("Total frogs: %d\n",fnum);
        double _c = cnum*100.0/total, _r = rnum*100.0/total;
        double _f = fnum*100.0/total;
        System.out.printf("Percentage of coneys: %.2f %%\n",_c);
        System.out.printf("Percentage of rats: %.2f %%\n",_r);
        System.out.printf("Percentage of frogs: %.2f %%\n",_f);
    }
}

题目五:区间2

713. 区间 2 - AcWing题库

代码 

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int ans = 0;
        for(int i = 0; i < n; i ++) {
            int x = sc.nextInt();
            if(x>=10 && x<=20) ans ++;
        }
        System.out.printf("%d in\n",ans);
        System.out.printf("%d out\n",n-ans);
    }
}

题目六:连续奇数的和 2

719. 连续奇数的和 2 - AcWing题库

 代码(有负数取余)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        while(n > 0) {
            int x = sc.nextInt(), y = sc.nextInt();
            if(x>y) {
                int t = x;
                x = y;
                y = t;
            }
            int ans = 0;
            for(int i = x+1; i < y; i ++) {
                if(i%2==1 || i%2==-1) ans += i;
            }
            System.out.println(ans);
            
            n --;
        }
    }
}

题目七:简单斐波那契

717. 简单斐波那契 - AcWing题库

代码 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int a = 0, b = 1;
        if(n>0) System.out.printf("%d ",a);
        if(n>1) System.out.printf("%d ",b);
        if(n>2) {
            for(int i = 3; i <= n; i ++) {
                int c = a + b;
                a = b; b = c;
                System.out.printf("%d ",b);
            }

        }
    }
}

代码2 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int a = 0, b = 1;
        for(int i = 0; i < n; i ++) {
            System.out.printf("%d ",a);
            int c = a+b;
            a = b; b = c;
        }
    }
}

题目八:数字序列和它的和

722. 数字序列和它的和 - AcWing题库

 代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int m = sc.nextInt(), n = sc.nextInt();
        while(m>0 && n>0) {
            if(m>n) {
                int t = m;
                m = n; n = t;
            }
            
            int sum = 0;
            for(int i = m; i <= n; i ++) {
                sum += i;
                System.out.printf("%d ",i);
            }
            System.out.printf("Sum=%d\n",sum);
            
            m = sc.nextInt(); n = sc.nextInt();
        }
    }
}

题目九:完全数

725. 完全数 - AcWing题库

 代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        for(int i = 0; i < n; i ++) {
            
            int x = sc.nextInt();
            int sum = -x;// 消除本身影响
            
            for(int j = 1; j <= x/j; j ++) {
                
                if(x%j==0) {
                    sum += j;
                    if(j!=x/j) sum += x/j;
                }
                
            }
            if(sum == x) System.out.printf("%d is perfect\n",x);
            else System.out.printf("%d is not perfect\n",x);
        }
    }
}

代码2(目的是消除1所对的自身影响)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        for(int i = 0; i < n; i ++) {
            
            int x = sc.nextInt();
            int sum = 0;// 消除本身影响
            
            for(int j = 1; j <= x/j; j ++) {
                
                if(x%j==0 ) {
                    sum += j;
                    if(j!=x/j) sum += x/j;
                    if(x/j==x) sum -= x;
                }
                
            }
            if(sum == x) System.out.printf("%d is perfect\n",x);
            else System.out.printf("%d is not perfect\n",x);
        }
    }
}

题目十:质数

726. 质数 - AcWing题库

 代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        for(int i = 0; i < n; i ++) {
            int x = sc.nextInt();
            
            int flag = 1;
            for(int j = 2; j <= x/j; j ++) {
                if(x%j!=0) continue;
                else {flag = 0; break; }
            }
            if(flag==1) System.out.printf("%d is prime\n",x);
            else System.out.printf("%d is not prime\n",x);
        }
    }
}

题目十一:菱形

727. 菱形 - AcWing题库

代码 (画图分析即可)

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        
        for(int i = 1; i <= n/2; i ++) {
            for(int j = 1; j <= n; j ++) {
                if(j>=n/2-i+2 && j<=n/2+i) System.out.printf("*");
                else System.out.printf(" ");
            }
            System.out.println("");
        }
        for(int i = 1; i <= n; i ++) System.out.printf("*");
        
        for(int i = n/2+2; i <= n; i ++) {
            System.out.println("");
            for(int j = 1; j <= n; j ++) {
                if(j>=i-n/2 && j<=3*n/2-i+1) System.out.printf("*");
                else System.out.printf(" ");
            }
        }
    }
}


原文地址:https://blog.csdn.net/2401_87338545/article/details/144321349

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!