下面是使用Java編寫的個人答案(非參考答案,建議自己參考網(wǎng)上的C語言代碼):
第1題:給定兩個正整數(shù)a和b爹耗,求在[a,b]中的所有整數(shù)中,每個數(shù)碼(digit 0~9)各出現(xiàn)了多少次簿煌。
例如輸入:
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的所有數(shù)
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"));
}
//單獨(dú)考慮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代碼:(不考慮大整數(shù)...)
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題:輸入一個整數(shù)n(0<n<10),顯示n行如下規(guī)律圖形鉴吹。
例如輸入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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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題:因?yàn)?51既是一個質(zhì)數(shù)又是一個回文數(shù)(從左到右和從右到左看是一樣的)豆励,所以151是回文質(zhì)數(shù)夺荒。
寫一個程序來找出范圍[a,b]間的所有回文質(zhì)數(shù),a,b由鍵盤輸入良蒸。(這題眼熟嗎技扼?對啊诚啃!因?yàn)橛钟谐笳麛?shù)這個坑)
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)階方陣淮摔,按照如下方式調(diào)整方陣:
1.將第一列中最大數(shù)所在的行與第一行對調(diào)。
2.將第二列中從第二行到第N行最大數(shù)所在的行與第二行對調(diào)始赎。
依此類推...
N-1.將第N-1列中從第N-1行到第N行最大數(shù)所在的行與第N-1行對調(diào)和橙。
N.輸出這個方陣
輸入:
包含多組測試數(shù)據(jù),每組測試數(shù)據(jù)第一行為一個整數(shù)N,表示方陣的階數(shù).
接下來輸入這個N階方陣.
輸出:
調(diào)整后的方陣
樣例輸入:
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題:兩個數(shù)字字符串相加
有定義:char s1[200],s2[200]造垛,s3[200]
若輸入s1和s2非全數(shù)字字符串魔招,顯示輸入錯誤;
否則計(jì)算s1與s2相加后的結(jié)果五辽,存放于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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}