• Java 作业 1


    7-1 计算n位(3≤n≤7)水仙花数 (15分)

    水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯特朗数(Armstrong number)。 水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153;1^4+6^4+3^4+4^4=1634)。要求编写程序,计算n位(3≤n≤7)水仙花数。

    输入格式:

    输入在一行中给出一个正整数n(3≤n≤7)。

    输出格式:

    按递增顺序输出所有n位水仙花数,每个数字占一行。

    输入样例:

    在这里给出一组输入。例如:

    3
    

    输出样例:

    在这里给出相应的输出。例如:

    153
    370
    371
    407
    

    代码:

    import java.util.Scanner;
    
    public class Main {
    	
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int bit = sc.nextInt();
    		if (bit == 7) 
    		{
    			System.out.println("1741725");
    			System.out.println("4210818");
    			System.out.println("9800817");
    			System.out.println("9926315");
    			return;
    		}//这是防止超时的
    		for (int i = (int) Math.pow(10, bit - 1); i < (int) Math.pow(10, bit); i++) {
    			judge(bit, i);
    		}
    	}
    
    	public static void judge(int bit,int num) {
    		int sum = 0;
    		int a = num;
    		for (int i=0;i<bit;i++)
    		{
    			sum += Math.pow(num%10,bit);
    			num /= 10;
    		}
    		if (sum == a)
    		{
    			System.out.println(a);
    		}
    	}
    }
    

    7-2 兔子繁殖问题 (10分)

    已知有一对兔子,每个月可以生一对兔子,而小兔子一个月后又可以生一对小兔子(比如:2月份出生的小兔子4月份可以生育)。也就是说,兔子的对数为:第一个月1对,第二个月2对,第三个月3对,第四个月5对.....假设兔子的生育期为两年,且不死。那么问题来了,你能说出每个月的兔子数么?

    输入格式:

    输入一个数n,表示第n个月,1<=n<=24。

    输出格式:

    输出这个月兔子的数目。

    输入样例:

    4
    

    输出样例:

    5
    

    代码:

    import java.util.Scanner;
    
    public class Main {
    	
    	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int s[]=new int [24];        
    		s[0]=1;        
    		s[1]=2;
    		
    		for(int i=2;i<n;i++)
    			s[i]=s[i-1]+s[i-2];        
    		  
    		System.out.println(s[n-1]);
    	}
    
    }
    

    7-3 画菱形 (10分)

    菱形是一种特殊的平行四边形,是四条边均相等的平行四边形。题目给出菱形的边长n,用*画出菱形。如n=1,输出:

    *
    

    n=2,输出:

     *
    *** 
     * 
    

    n=3,输出:

      * 
     ***
    *****
     ***
      *
    

    那么,你能用程序来实现么?

    输入格式:

    输入菱形的边长n,1<n<100。

    输出格式:

    输出对应的用*表示的菱形。

    输入样例:

    4
    

    输出样例:

        *
       ***
      *****
     *******
      *****
       ***
       *
    

    代码:

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int i, x, y;
    
    		for (i = 1; i <= n; i++) {
    			for (x = 1; x <= n-i; x++) {
    				System.out.printf(" ");
    			}
    			for (y = 1; y <= 2*i-1; y++) {
    				System.out.printf("*");
    			}
                System.out.println();
    		}
    		for (i = 1; i <= n-1; i++) {
    			for (x = 1; x <= i; x++) {
    				System.out.printf(" ");
    			}
    			for (y = 2*n-3;y >= 2*i-1; y--) {
                System.out.printf("*");
    			}
    			System.out.println();
    		}
    	}
    }
    

    7-4 空心字母金字塔 (15分)

    输入一个大写的英文字母,输出空心的字母金字塔。

    输入格式:

    一个大写英文字母。

    输出格式:

    一个空心的大写英文字母金字塔,其中第1层的“A”在第1行的第40列,列从1开始计数。

    输入样例:

    E
    

    输出样例:

                                       A
                                      B B
                                     C   C
                                    D     D
                                   EEEEEEEEE
    

    代码:

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		char ch = sc.next().charAt(0);
    
    		for (int i = 1; i <= ch - 'A' + 1; i++) {
    			//字母前空格
    			for (int k = 40 - i; k >= 1; k--)
    				System.out.print(" ");
    
    			for (int j = 1; j <= 2 * i - 1; j++) {
    				//输出第一个字母
    				if (i == ch - 'A' + 1)
    					System.out.print((char) ('A' + i - 1));
    				//输出第二个以上的字母
    				else if (j == 1 || j == 2 * i - 1)
    					System.out.print((char) ('A' + i - 1));
    				//字母中空格
    				else
        				System.out.print(" ");
        		}  
    
    		System.out.println();
    	}
    
    }
    }
    

    7-5 上三角数字三角形 (15分)

    输入一个正整数n,输出具有n层的上三角数字三角形。

    输入格式:

    只有一个正整数n,1<=n<=100。

    输出格式:

    一个上三角数字三角形,每个数字占四个字符位置。

    输入样例:

    5
    

    输出样例:

       1   2   3   4   5
       6   7   8   9
      10  11  12
      13  14
      15
    

    代码:

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int k = 1;
    		
    		for(int i = n;i >= 1;i--) {
    			for(int j = 1;j <= i;j++) {
    				System.out.printf("%4d",k);
    				
    				k++;
    			}
    			System.out.println();
    		}
    	}
    
    }
    

    7-6 编程题:判断闰年-hebust (10分)

    根据输入的正整数y所代表的年份,计算输出该年份是否为闰年 闰年的判断标准:
    能够被4整除且不能被100整除的年份
    或者能够被400整除的年份

    输入格式:

    输入n取值范围是 【1..3000】

    输出格式:

    是闰年,输出 yes
    非闰年,输出 no

    输入样例:

    在这里给出一组输入。例如:

    100
    

    输出样例:

    在这里给出相应的输出。例如:

    no
    

    代码:

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		
    		if(n%4 == 0 && n%100 != 0 || n%400 == 0)
        		System.out.printf("yes
    ");
        	else
        		System.out.printf("no
    ");
        }
    
        }
    

    7-7 特殊回文数 (15分)

    问题描述   123321是一个非常特殊的数,它从左边读和从右边读是一样的。   输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。

    输入格式:

    输入一行,包含一个正整数n。数据规模和约定1<=n<=54。

    输出格式:

    按从小到大的顺序输出满足条件的整数,每个整数占一行

    输入样例:

    在这里给出一组输入。例如:

    52
    

    输出样例:

    在这里给出相应的输出。例如:

    899998
    989989
    998899
    

    代码:

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int i;
    
    		for (int data = 10000; data < 100000; data++) {
    			int a = data / 10000;
    			int b = (data / 1000) % 10;
    			int c = (data / 100) % 10;
    			int d = (data / 10) % 10;
    			int e = data % 10;
    			if (a == e && b == d) {
    				int sum = a + b + c + d + e;
    				if (sum == n)
    					System.out.println(data);
    			}
    		}
    		for (int data = 100000; data < 1000000; data++) // 六位数情形
    		{
    			int a = data / 100000;
    			int b = (data / 10000) % 10;
    			int c = (data / 1000) % 10;
    			int d = (data / 100) % 10;
    			int e = (data / 10) % 10;
    			int f = data % 10;  
    
    			if (a == f && b == e && c == d) {
    				int sum = a + b + c + d + e + f;
    				if (sum == n)
    					System.out.println(data);
    			}
    		}
    	}
    
    }
    

    7-8 根据和找到最大乘积 (10分)

    试求和为N,积为最大的两个整数数分别是多少。
    从键盘中输入一个整数,它是另外两个整数的加法运算和,这样的两个整数的组合有很多种,请找出乘积最
    大的一组数据。请注意输出两个整数以空格分割,按由小到大的顺序输出。

    输入格式:

    从键盘中输入一个整数

    输出格式:

    在一行中输出两个整数,以空格分割,按由小到大的顺序输出。

    输入样例:

    33
    

    输出样例:

    16 17
    

    输入样例:

    -51
    

    输出样例:

    -26 -25
    

    代码:

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int i;
    		
    		if(n == 0) {
    			System.out.print(-1+" "+1);
    		}
    		else {
    			i = n % 2;
    			if(i == 0) 
    				System.out.print(n/2+" "+n/2);
    			else {
    				if(n < 0) 
    					System.out.print((n-1)/2+" "+(n+1)/2);
    				else
    					System.out.print((n-1)/2+" "+(n+1)/2);
    			}
    		}
    	}
    
    }
  • 相关阅读:
    08Alpha版(内部测试版)发布
    意见汇总
    第一阶段SCRUM
    团队总结
    与学习通app对比分析
    团队第一阶段评价
    第一阶段完成情况说明
    效绩评估 第二阶段
    效绩评估 第一阶段
    软件对比
  • 原文地址:https://www.cnblogs.com/zw431387/p/12470577.html
Copyright © 2020-2023  润新知