中M2021秋C、Java入门练习第I段——变量、表达式、分支、循环(1~20题)
7-1 重要的话说三遍 (5 分)
#include<stdio.h>
int main() {
int i;
for(i=0;i<3;i++)
printf("I'm gonna WIN!\n");
return 0;
}
7-2 I Love GPLT (5 分)
#include<stdio.h>
int main() {
printf("I\n \nL\no\nv\ne\n \nG\nP\nL\nT");
return 0;
}
7-3 输出带框文字 (5 分)
#include<stdio.h>
int main() {
printf("************\n");
printf(" Welcome\n");
printf("************\n");
return 0;
}
7-4 输出菱形图案 (5 分)
#include<stdio.h>
int main() {
printf(" A\n");
printf("A A\n");
printf(" A\n");
return 0;
}
7-5 输出倒三角图案 (5 分)
#include<stdio.h>
int main() {
printf("* * * *\n");
printf(" * * *\n");
printf(" * *\n");
printf(" *\n");
return 0;
}
7-6 厘米换算英尺英寸 (15 分)
#include <stdio.h>
int main() {
int f, i, cm;
double I, P;
scanf("%d", &cm);
I = cm / 30.48;
f = I / 1;
// printf("%d", f);
P = (I - f) * 12;
i = P / 1;
printf("%d %d", f, i);
return 0;
}
7-7 计算摄氏温度 (10 分)
int main() {
int F, C;
scanf("%d", &F);
C = 5 * (F - 32) / 9;
printf("Celsius = %d", C);
return 0;
}
7-8 是不是太胖了 (5 分)
#include <stdio.h>
int main() {
int H;
float W;
scanf("%d", &H);
W = (H - 100) * 0.9 * 2.0;
printf("%.1f", W);
return 0;
}
7-9 求整数均值 (10 分)
#include <stdio.h>
int main() {
int Sum, a, b, c, d ;
float Average ;
scanf("%d%d%d%d", &a, &b, &c, &d);
Sum = a + b + c + d;
Average = Sum / 4.0;
printf("Sum = %d; Average = %.1f", Sum, Average);
return 0;
}
7-10 算术入门之加减乘除 (10 分)
#include <stdio.h>
int main() {
int a, b;
scanf("%d%d", &a, &b);
printf("%d + %d = %d\n", a, b, a + b);
printf("%d - %d = %d\n", a, b, a - b);
printf("%d * %d = %d\n", a, b, a * b);
if (a % b == 0)
printf("%d / %d = %d\n", a, b, a / b);
else
printf("%d / %d = %.2f\n", a, b, a / (double)b);
return 0;
}
7-11 计算平均分 (5 分)
#include<stdio.h>
int main()
{
int m,e,c,ave;
m=87;
e=72;
c=93;
ave=(m+e+c)/3;
printf("math = %d, eng = %d, comp = %d, average = %d",m,e,c,ave);
return 0;
}
7-12 日期格式化 (5 分)
#include<stdio.h>
int main()
{
int mm,dd,yyyy;
scanf("%d-%d-%d",&mm,&dd,&yyyy);
printf("%04d-%02d-%02d",yyyy,mm,dd);
return 0;
}
7-13 后天 (5 分)
#include <stdio.h>
int main() {
int D;
scanf("%d", &D);
if (D <= 5)
printf("%d", D + 2);
else
printf("%d", D-5);
return 0;
}
7-14 然后是几点 (15 分)
#include<stdio.h>
int main()
{
int begin,later,pass,total,end;
scanf("%d%d",&begin,&pass);
total=begin/100*60+begin%100;
end=total+pass;
later=end/60*100+end%60;
if (end/60==0)
printf("%03d",later);
else
printf("%d",later);
return 0;
}
7-15 BCD解密 (10 分)
#include<stdio.h>;
int main()
{
int N,M;
scanf("%d",&N);
M=N/16*10+N%16;
printf("%d",M);
return 0;
}
7-16 计算符号函数的值 (10 分)
#include <stdio.h>
int main() {
int N;
scanf("%d", &N);
if (N < 0) {
printf("sign(%d) = -1", N);
}
if (N == 0) {
printf("sign(%d) = 0", N);
}
if (N > 0) {
printf("sign(%d) = 1", N);
}
return 0;
}
7-17 成绩转换 (15 分)
#include <stdio.h>
int main() {
int grade;
scanf("%d", &grade);
if (grade >= 90)
printf("A");
if (80 <= grade && grade < 90)
printf("B");
if (70 <= grade && grade < 80)
printf("C");
if (60 <= grade && grade < 70)
printf("D");
if (grade < 60)
printf("E");
return 0;
}
7-18 出租车计价 (15 分)
#include<stdio.h>
#include <math.h>
int main() {
float km, fare1, total;
int minute, pay,fare2;
scanf ("%f %d", &km, &minute);
if (km < 3)
fare1 = 10;
if (km > 3 && km <= 10)
fare1 = 10 + 2 * (km - 3);
if (km > 10)
fare1 = 24 + 3 * (km - 10);
fare2 = minute / 5 * 2;
total = fare1 + fare2;
pay = round(total);
printf("%d", pay);
return 0;
}
7-19 计算天数 (15 分)
#include <stdio.h>
int main() {
int yyyy = 0, mm = 0, dd = 0, days = 0, i = 0;
scanf("%d/%d/%d", &yyyy, &mm, &dd);
int a[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int b[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ((yyyy % 4 == 0 && yyyy % 100 != 0 || yyyy % 400 == 0)) {
for (i = 0; i < mm - 1; i++) {
days += b[i];
}
} else
for (i = 0; i < mm - 1; i++) {
days += a[i];
}
days = days + dd;
printf("%d", days);
return 0;
}
7-20 简单计算器 (20 分)
#include <stdio.h>
int main () {
int a, b, sum = 0 ;
char c;
scanf("%d", &a);
c = getchar();
while (c != '=') {
scanf("%d", &b);
if (c == '/' && b == 0) {
printf("ERROR\n");
return 0;
}
switch (c) {
case '+':
sum = a + b;
break;
case '-':
sum = a - b;
break;
case '*':
sum = a * b;
break;
case '/':
sum = a / b;
break;
default:
printf("ERROR\n");
return 0;
}
c = getchar();
a = sum;
}
printf("%d\n", a);
return 0;
}