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:
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.
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’
- ‘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;
}