下面是使用Java编写的个人答案(非参考答案,建议自己参考网上的C语言代码):
第1题:给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit 0~9)各出现了多少次。
例如输入:
1 99
输出
9 20 20 20 20 20 20 20 20 20
public class test1 {
public static void main(String[] args) {
String a, b;
int[] num = new int[10];
for (int i = 0; i < 10; i++) {
num[i] = 0;
}
Scanner in = new Scanner(System.in);
a = in.next();
b = in.next();
BigInteger ansA = new BigInteger(a);
BigInteger ansB = new BigInteger(b);
//考虑从a到b-1的所有数
while (!ansA.equals(ansB)) {
char[] compare = ansA.toString().toCharArray();
for (int i = 0; i < ansA.toString().length(); i++) {
for (int j = 0; j < 10; j++) {
if (compare[i] - '0' == j) {
num[j]++;
}
}
}
ansA = ansA.add(new BigInteger("1"));
}
//单独考虑b
for (int i = 0; i < ansB.toString().length(); i++) {
char[] compare = ansB.toString().toCharArray();
for (int j = 0; j < 10; j++) {
if (compare[i] - '0' == j) {
num[j]++;
}
}
}
for (int i = 0; i < 10; i++) {
System.out.println(num[i]);
}
}
}
C代码:(不考虑大整数...)
int main() {
int hashTable[10] = {0};
int a, b;
while (scanf("%d %d", &a, &b) != EOF) {
for (int i = a; i <= b; i++) {
int n = i;
while (n > 0) {
int x = n % 10;
hashTable[x]++;
n = n / 10;
}
}
for (int i = 0; i < 10; i++) {
printf("%d ", hashTable[i]);
}
printf("\n");
}
return 0;
}
第2题:输入一个整数n(0
例如输入3 ,显示
1
2 3
4 5 6
例如输入5,显示
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
public class test2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int count = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = n - i - 1; j < n; j++) {
System.out.printf("%3d", count);
count++;
}
System.out.println();
}
}
}
C代码
#include
#include
#include
using namespace std;
int main() {
int n;
while(scanf("%d", &n) != EOF) {
int x = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
printf(" ");
}
for (int j = n - i - 1; j < n; j++) {
printf("%3d", x);
x++;
}
printf("\n");
}
}
return 0;
}
第3题:因为151既是一个质数又是一个回文数(从左到右和从右到左看是一样的),所以151是回文质数。
写一个程序来找出范围[a,b]间的所有回文质数,a,b由键盘输入。(这题眼熟吗?对啊!因为又有超大整数这个坑)
public class test3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = in.next();
String b = in.next();
in.close();
BigInteger ansA = new BigInteger(a);
BigInteger ansB = new BigInteger(b);
while (!ansA.equals(ansB)) {
if (isPrimeNumber(ansA)) {
char count[] = ansA.toString().toCharArray();
int time = 0;
for (int i = 0; i < ansA.toString().length() / 2; i++) {
if (count[i] == count[count.length - i - 1]) {
time++;
}
}
if (time == ansA.toString().length() / 2) {
System.out.println(ansA.toString());
}
}
ansA = ansA.add(new BigInteger("1"));
}
}
private static boolean isPrimeNumber (BigInteger num) {
BigInteger i = new BigInteger("2");
while (i.compareTo(num) <= 0) {
if ((num.mod(i)).equals(new BigInteger("0"))) {
break;
}
i = i.add(new BigInteger("1"));
}
return i.equals(num);
}
}
第4题:输入一个N(N<=10)阶方阵,按照如下方式调整方阵:
1.将第一列中最大数所在的行与第一行对调。
2.将第二列中从第二行到第N行最大数所在的行与第二行对调。
依此类推...
N-1.将第N-1列中从第N-1行到第N行最大数所在的行与第N-1行对调。
N.输出这个方阵
输入:
包含多组测试数据,每组测试数据第一行为一个整数N,表示方阵的阶数.
接下来输入这个N阶方阵.
输出:
调整后的方阵
样例输入:
4
3 6 8 7
6 7 5 3
8 6 5 3
9 8 7 2
样例输出:
9 8 7 2
6 7 5 3
3 6 8 7
8 6 5 3
public class test4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int num[][] = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
num[i][j] = in.nextInt();
}
}
for (int i = 0; i < n; i++) {
int max = i;
for (int j = i; j < n; j++) {
if (num[j][i] > num[max][i]) {
max = j;
}
}
int temp[] = new int[n];
for (int j = 0; j < n; j++) {
temp[j] = num[i][j];
num[i][j] = num[max][j];
num[max][j] = temp[j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(num[i][j] + " ");
}
System.out.println();
}
}
}
第5题:两个数字字符串相加
有定义:char s1[200],s2[200],s3[200]
若输入s1和s2非全数字字符串,显示输入错误;
否则计算s1与s2相加后的结果,存放于s3并显示。
例如输入:
999999999999999999999
999999999999999999999
例如输出
1999999999999999999998
输入:
1234567890123456789
876543211
输出:
1234567891000000000
public class test5 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s1 = in.nextLine();
String s2 = in.nextLine();
char ansA[] = s1.toCharArray();
char ansB[] = s2.toCharArray();
for (int i = 0; i < s1.length(); i++) {
if (ansA[i] < '0' || ansA[i] > '9') {
System.out.println("输入错误");
System.exit(0);
}
}
for (int i = 0; i < s2.length(); i++) {
if (ansB[i] < '0' || ansB[i] > '9') {
System.out.println("输入错误");
System.exit(0);
}
}
BigInteger a = new BigInteger(s1);
BigInteger b = new BigInteger(s2);
System.out.println(a.add(b));
}
}
C代码:
#include
#include
#include
using namespace std;
struct bign {
int d[1010];
int len;
bign () {
memset(d, 0, sizeof(d));
}
};
bign change(char str[]) {
bign a;
a.len = strlen(str);
for (int i = 0; i < a.len; i++) {
a.d[i] = str[a.len - i - 1] - '0';
}
return a;
}
bign add(bign a, bign b) {
bign c;
int carry = 0;
for (int i = 0; i < a.len || i < b.len; i++) {
int temp = a.d[i] + b.d[i] + carry;
c.d[c.len++] = temp % 10;
carry = temp / 10;
}
if (carry != 0) {
c.d[c.len++] = carry;
}
return c;
}
int main() {
char str1[200], str2[200];
bool flag = true;
while(scanf("%s %s", str1, str2) != EOF) {
int len1 = strlen(str1), len2 = strlen(str2);
for (int i = 0; i < len1; i++) {
if (str1[i] > '9' || str1[i] < '0') {
flag = false;
}
}
for (int i = 0; i < len2; i++) {
if (str2[i] > '9' || str2[i] < '0') {
flag = false;
}
}
if (flag == false) {
printf("input error!\n");
continue;
}
bign a = change(str1);
bign b = change(str2);
bign c = add(a, b);
for (int i = c.len - 1; i >= 0; i--) {
printf("%d", c.d[i]);
}
printf("\n");
}
return 0;
}