本文共 5353 字,大约阅读时间需要 17 分钟。
Given a none-negative number, print out the sum of its digits.
Input Format:
A none-negative integer number.Output Format:
The sum of its all digits.Sample Input:
123 Sample Output: 6import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); int s=0; while(n>0){ s+=n%10; n=n/10; } System.out.println(s); }}
请你实现一个累加器。输入n个非负整数,输出他们的和。 1<n<1000,而每个数则<10000。
输入格式:
输入包括两行。 第一行:包括一个整数n,表示总共有n个数。 第二行:包含n个整数。输出格式:
输出n个数的和。输入样例:
4 3 2 1 4 输出样例: 10import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; int s=0; for(int i=0;i
输入一组数,将其中的奇数、偶数分别求和并输出
输入格式:
输入n,然后输入n个整数输出格式:
奇数和=x, 偶数和=y 其中x,y分别代表奇数和与偶数和。逗号(,)后面有一个空格。输入样例:
10 1 2 1 1 1 2 1 2 2 2输出样例:
奇数和=5, 偶数和=10import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; int x=0; int y=0; for(int i=0;i
水仙花数是指一个N位正整数(7≥N≥3),它的每个位上的数字的N次幂之和等于它本身。例如:153=13+53+33。 要求编写程序,计算所有N位水仙花数。
输入格式:
输入一个正整数N(3≤N≤7)。输出格式:
按递增顺序输出所有N位水仙花数,每个数字占一行。输入样例:
在这里给出一组输入。例如:3
输出样例:
在这里给出相应的输出。例如:153
370 371 407import java.util.Scanner;import static java.lang.Math.pow;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long num = (int) pow(10, n - 1); if (n == 7) { System.out.println("1741725"); System.out.println("4210818"); System.out.println("9800817"); System.out.println("9926315"); } else { for (long i = num; i < 10 * num; i++) { long sum = 0; long x = 0; long t = i; while (t > 0) { x = t % 10; t = t / 10; sum += pow(x, n); } if (i == sum) { System.out.println(i); } } } }}
一年一度的升旗手选拔又要到了,学校要求每个班级选出一位同学做升旗手的候选人。因 为升旗手对身高有严格的要求,所以班主任决定选班级里个子最高的同学(如果两位同学 一样高,则选任意一位)。你能很快地给老师答案么?
输入格式:
输入包括两行。 第一行:包括一个整数n,表示班级里共有n位同学。 第二行:包含n个三位数,表示每一位同学的身高。输出格式:
输出身高最高的同学的身高。输入样例:
4 130 125 129 140输出样例:
140import java.util.Scanner;import static java.lang.Math.pow;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[]=new int[n]; for(int i=0;i
369寝室是比较特殊的寝室,因为别的寝室都住了四个人,而369寝室只有三个人。也因为这个原因,寝室里的三位同学感情特别好。但是,毕业在即,三位小伙伴马上要分别。为了在未来的某个日子可以见面,三位小伙伴有了一个约定,假设在未来的某一年,三位小伙伴的年龄的末尾正好出现3、6、9三个数,那么他们会再次相聚。
现在问题来了,假设今年三位小伙伴的年龄分别是x,y,z,那么,他们三人最早几年后可以相聚呢?
输入格式:
输入数据包括三个整数x,y,z,分别表示三位伙伴的年龄。输出格式:
如果小伙伴最早在n年后可以相见(不包括当前这一年),那么请输出这个n;如果100年内都不存在这样的情况,输出:so sad!输入样例:
25 22 28输出样例:
1import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int f = 0; for (int i = 1; i <= 100; i++) { a++; b++; c++; a %= 10; b %= 10; c %= 10; if (a == 0 || b == 0 || c == 0) { continue; } if (a % 3 == 0 && b % 3 == 0 && c % 3 == 0) { if (a != b && a != c && b != c) { f = i; break; } } } if(f==0){ System.out.println("so sad!"); } else{ System.out.println(f); } }}
输入一个正整数n,输出具有n层的上三角数字三角形。
输入格式:
只有一个正整数n,1<=n<=100。输出格式:
一个上三角数字三角形,每个数字占四个字符位置。输入样例:
5 输出样例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextInt(); long sum=0; for(long i=0;i
计算机内部用二进制来表达所有的值。一个十进制的数字,比如24,在一个32位的计算机内部被表达为00000000000000000000000000011000。可以看到,从左边数过来,在第一个1之前,有27个0。我们把这些0称作前导的零。
现在,你的任务是写一个程序,输入一个整数,输出在32位表达下它前导的零的个数。
输入格式:
一个整数,在32位的整数可以表达的范围内。输出格式:
一个整数,表达输入被表达为一个32位的二进制数时,在第一个1之前的0的数量。输入样例:
256输出样例:
23import java.util.Scanner;import static java.lang.Math.pow;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i = 1; if (n == 0) { System.out.println(32); } else if (n > 0 && n <= pow(2, 32)) { while (n > 1) { n = n / 2; i++; } System.out.println(32 - i); } else { System.out.println(0); } }}
把给定的十进制数29除以2,商为14,所得的余数1是二进制数的最低位的数码,再将14除以2,商为7,余数为0。再将7除以2,商为3,余数为1,再将3除以2,商为1,余数为1,再将1除以2,商为0,余数为1是二进制数的最高位的数码。具体过程如下:
转载地址:http://mjnlf.baihongyu.com/