文章標題

NUMBER 1
給出兩個正整數m和n,請求出這個兩個數的最大公約數(greatest common divisor,後面簡寫爲gcd)和最小公倍數( least common multiple,後面簡稱lcm)。
測試樣例:
input:
25 45
output:
the gcd about these two numbers is: 5
the lcm about these two numbers is: 225

#include<stdio.h>
int gac(int a, int b);
int main() {
    int m, n;
    scanf("%d%d", &m, &n);
    ans = gac(m, n);
    printf("%d\n",ans);
    printf("%d\n",(m*n)/ans); 
    return 0;
}
int gac(int a, int b) {
    if (b == 0) 
        return a;
    return gcd(b, a%b);
}    

Number2
Write a program that read a number n and n integers (2 <= n <= 100), then print the second largest integers (in the n integers).

Sample Input

5

3 2 1 5 4

Sample Output

4

#include<stdio.h>
int main() {
    int n;
    int t, max1 = INT_MIN, max2 = INT_MIN;
    while(n--) {
        sacnf("%d", &t);
        if (t > max1) {
            max2 = max1;
            max1 = t;
        } else if (t > max2) {
            max2 = t;
        }
     }
     printf("%d", max2);
     return 0;
}     

Number3
Input three integer numbers within [0, 9] in the type of char , output their ascii codes;
Convert these three characters into int type, add them and output the summation.

Input: three characters should be inputed in one line without any blank space. It is recommended to use the getchar() function.
Output: one ascii code occupies one single line. Every output ends with a ‘\n’.

Sample:

Input:

234

Output:

50

51

52

9

#include<stdio.h>
#include<stdlib.h>

int main() {
    char charA, charB, charC;
    int intA, intB, intC;
    int sum = 0;
    charA = getchar();
    charB = getchar();
    charC = getchar();
    printf("%d\n", charA);
    printf("%d\n", charB);
    printf("%d\n", charC);
    intA = charA - '0';
    intB = charB - '0';
    intC = charC - '0';
    sum = intA + intB + intC;
    printf("%d\n", sum);
    return 0;
}

Number 4
以撒將要遠行。當以撒的媽媽想要殺死以撒時,以撒決定逃跑。以撒的武器,是他的眼淚。好運的是,以撒獲得了一個道具,這個道具
使得以撒可以發射出三滴眼淚。只要以撒擁有至少三滴眼淚時,他就會一次性發射三滴眼淚來戰鬥(如果不夠三滴就不發射)。而如果
他把所有的眼淚都發射完畢(也就是剩餘0滴眼淚),以撒就會傷心的死去。假設現在以撒擁有N滴眼淚,(以撒的人生非常悲劇,他常
常哭泣,積攢了許多眼淚,所以N非常大,總之longlong肯定是存不下,但不會超過100位數)如果以撒會傷心的死去,輸出God,否則,
輸出Issac

樣例輸入1:
5
樣例輸出1:
Issac
樣例輸入2:
1234567890987654321
樣例輸出2:
God

#include<stdio.h>
int main() {
    char s{100];
    scanf("%s", s);
    int n = strlen(s);
    for (int i = 0; i < n; i++) {
        int id = s[i] - '0';
        sum = sum + id;
    }
    sum = sum % 3;
    if (sum == 0) {
        printf("God\n");
    } else {
        printf("Issac\n");
    }
    return 0;
}

Number 5
1只公雞值5文錢;1只母雞值3文錢;3只小雞值1文錢。請問用文錢買100只雞,公雞、母雞和小雞各有幾隻?
實際題目中會按照M文錢買N只雞的形式
按公雞母雞小雞的順序分別輸出結果,一組解答佔一行,解答按照公雞數目從大到小排序(其次母雞,再次小雞)

無解時請輸出 no answer

如輸入爲:

100 100

則輸出:

12 4 84

8 11 81

4 18 78

0 25 75

如輸入爲:

1 4

則輸出爲:

no answer

#include<stdio.h>
int main() {
    int a, b, c;
    int money, number, flag;
    scanf("%d %d", &money, &number);
    flag = 0;
    for (a = money/5; a >= 0; a--)
        for (b = money/3; b >= 0; b--)
            for (c = money; c >= 0; c--) {
                if (a + b + c*3 == number && a*5 + b*3 + c == money) {
                    flag++;
                    printf("%d %d %d\n", a, b, c*3);
                 }
            }
    if ( flag == 0 )
        printf("no answer\n");
    return 0;
}

You should encrypt a plain text(which contains A-Z,a-z) to a cipher text(which contains A-Z).

The encrypting rule:

  1. convert the lowercase to the upper case. for example, ‘h’–>’H’.

    If the character is lower case or space, you should not to do that.

  2. cycle shift n position. for example, if n =2, then ‘z’ –> ‘B’, ‘H’–>’J’, ‘h’–>’J’.

    If the character is space, you should not to do that.

Input format:

n –> stand for how many position you should cycle shift.(1 <= n <= 25)

plain text –>the string you should encrypt(1<= it’s length <= 30)

output format:

cipher text –> not ‘\n’ this time

For example

[Input]

2

ILoveYou

[Output]

KNQXGAQW
1.如果用getchar接收字符進行移位處理,那麼在用scanf接收變量n後,getchar接收字符前,需要用一個getchar函數接收變量n後面的’\n’字符.

2.如果用char數組接收字符串,再逐個處理。 那麼不需考慮1中的問題.

爲了更好的理解getchar與scanf的區別,本次建議使用第一種方式解題。

tips(題目可能有些超出目前學的知識,所以給出下面的提示):

1.輸入的字符串,僅包含A-Z,a-Z.末尾爲’\n’。不要考慮包含空格、\t等複雜情況

2.A-Z(ascii十進制:65-90)

a-z(ascii十進制97-122)

3.輸出字符的方式,例如:

char c = ‘A’;

printf(“%c”, c);

4.接收一串字符直到遇到換行符結束的代碼實現:

char c;

while ((ch = getchar()) != ‘\n’) {

   // ..... 對字符進行處理

}

5.輸入的結果後面不需要加換行符’\n’

  1. ‘Y’ 右移兩位是’A’,需要考慮這種特殊情況
#include <stdio.h>

int main() {
    char ch;
    int n;

    scanf("%d", &n);
    getchar();  //  該語句用於接收scanf輸入n後的回車
    while ((ch = getchar()) != '\n') {
        if (ch >= 'a' && ch <= 'z')  //  小寫轉換成大寫
            ch -= ('a' - 'A');

        ch += n;  // 移位操作
        if (ch > 'Z')  // 循環移位
            ch = ch - 'Z' - 1 + 'A';

        printf("%c", ch);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章