7-1 1!+2!+……+N! (20 分)
给定一个整数N,编写程序求1!+2!+……+N!(0<N<100)
输入格式:
输入一个整数N
输出格式:
输出1!+2!+……+N!
输入样例:
在这里给出一组输入。例如:
6
输出样例:
在这里给出相应的输出。例如:
873
Accepted Code 1
import java.util.Scanner;
import java.lang.Math;
import java.util.Arrays;
public class Main {
public static int func(int n) {
if(n == 0)
return 1;
else
return n * func(n-1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long result = 0;
while(n != 0) {
result += func(n);
n--;
}
System.out.println(result);
}
}
7-2 完数 (20 分)
一个数如果恰好等于它的因子之和,这个数就称为"完数"。 例如,6的因子为1、2、3,而6=1+2+3,因此6是"完数"。 编程序找出N之内的所有完数。
输入格式:
整数N
输出格式:
N之内的所有完数,每个完数之间用逗号(英文半角)分隔开。注意:1不作为完数
输入样例:
在这里给出一组输入。例如:
30
输出样例:
在这里给出相应的输出。例如:
6,28
Accepted Code 2
import java.util.Scanner;
import java.lang.Math;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int T = 0;
for (int i = 2; i < N; i++) {
int tmp = 0;
for (int j = 1; j < i; j++) {
if (i % j == 0) {
tmp += j;
}
}
if (i == tmp) {
if (T == 0) {
System.out.print(i);
T++;
}
else System.out.print("," + i);
}
}
}
}
7-3 n个a数字求和 (20 分)
求Sn=a+aa+aaa+…+aa…aaa(有n个a)之值,其中a是一个数字(1<=a<=9)。例如:2+22+222+2222+22222(a=2,n=5),a和n由键盘输入。
输入格式:
a,n(说明:a是出现在各个数字每一位的数字,n是最大数的位数)
输出格式:
Sn=a+aa+aaa+…+aa…aaa的值
输入样例:
在这里给出一组输入。例如:
2 3
输出样例:
在这里给出相应的输出。例如:
246
Accepted Code 3
import java.util.Scanner;
import java.lang.Math;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int n = sc.nextInt();
long sa = 0, num = 0;
for (int i = 0; i < n; i++) {
sa += (long)(a * Math.pow(10, i));
num += sa;
}
System.out.println(num);
}
}
7-4 数字统计 (20 分)
输入一个长整型的数,统计其中0、1、2、3、4、5、6、7、8、9各个数字的个数,并将结果合成一个整数。(前面的0不输出)
输入格式:
长整型数
输出格式:
合成后的整数
输入样例:
在这里给出一组输入。例如:
234353632
输出样例:
在这里给出相应的输出。例如:
24111000
Accepted Code 4
import java.util.Scanner;
import java.lang.Math;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr1 = new int[10];
long n = sc.nextLong();
long tmp = n;
while (tmp > 0) {
arr1[(int) (tmp % 10)]++;
tmp /= (long)10;
}
int num = 0;
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != 0) {
System.out.print(arr1[i]);
if (num < i)
num = i;
}
}
for (int j = num + 1; j < 10; j++) {
System.out.print("0");
}
System.out.println();
}
}