2018年武漢大學(xué)計(jì)算機(jī)復(fù)試上機(jī)題

下面是使用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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市杆逗,隨后出現(xiàn)的幾起案子乡翅,更是在濱河造成了極大的恐慌,老刑警劉巖罪郊,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蠕蚜,死亡現(xiàn)場離奇詭異,居然都是意外死亡悔橄,警方通過查閱死者的電腦和手機(jī)靶累,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進(jìn)店門腺毫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人挣柬,你說我怎么就攤上這事潮酒。” “怎么了邪蛔?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵急黎,是天一觀的道長。 經(jīng)常有香客問我店溢,道長叁熔,這世上最難降的妖魔是什么委乌? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任床牧,我火速辦了婚禮,結(jié)果婚禮上遭贸,老公的妹妹穿的比我還像新娘戈咳。我一直安慰自己,他們只是感情好壕吹,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布著蛙。 她就那樣靜靜地躺著,像睡著了一般耳贬。 火紅的嫁衣襯著肌膚如雪踏堡。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天咒劲,我揣著相機(jī)與錄音顷蟆,去河邊找鬼。 笑死腐魂,一個胖子當(dāng)著我的面吹牛帐偎,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蛔屹,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼削樊,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了兔毒?” 一聲冷哼從身側(cè)響起漫贞,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎育叁,沒想到半個月后迅脐,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡擂红,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年仪际,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了围小。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡树碱,死狀恐怖肯适,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情成榜,我是刑警寧澤框舔,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站赎婚,受9級特大地震影響刘绣,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜挣输,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一纬凤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧撩嚼,春花似錦停士、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至逻族,卻和暖如春蜻底,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背聘鳞。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工薄辅, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人搁痛。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓长搀,卻偏偏與公主長得像,于是被迫代替她去往敵國和親鸡典。 傳聞我的和親對象是個殘疾皇子源请,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,612評論 2 350