仅作研究参考,本文为使用C语言编写的代码,如需其他版本,先留言
一、语言及算法基础篇
基础(一)c语言
第一章
1000:入门测试题目
【题目描述】
求两个整数的和。
【输入】
一行,两个用空格隔开的整数。
【输入样例】
2 3
【输出样例】
5
#include <stdio.h> int main() { int a, b; scanf("%d%d", &a, &b); printf("%d", a + b); }
讯享网
2060:【例1.1】计算机输出
【输入样例】
(无)
【输出样例】
Hello World!
讯享网#include <stdio.h> int main() { printf("Hello World!"); }
2061:【例1.2】梯形面积

【输入样例】
(无)
【输出样例】
(无)
#include <stdio.h> int main() { float area_of_triangle = 150.0; float base_of_triangle = 15.0; float height_of_triangle = (2 * area_of_triangle) / base_of_triangle; float top_base_of_trapezoid = 15.0; float bottom_base_of_trapezoid = 25.0; float area_of_trapezoid = (top_base_of_trapezoid + bottom_base_of_trapezoid) * height_of_triangle / 2; printf("%.2f", area_of_trapezoid); return 0; }
2062:【例1.3】电影票
【输入样例】
2
【输出样例】
2 20
讯享网#include <stdio.h> int main() { int a, b=10; scanf("%d", &a); printf("%d %d", a,a * b); return 0; }
2063:【例1.4】牛吃牧草
【输入样例】
(无)
【输出样例】
(无)
#include <stdio.h> int main() { int c1 = 20 * 15; int c2 = 10 * 20; int daily_growth = (c1 - c2) / (20 - 10); printf("%d", daily_growth); return 0; }
1001:Hello,World!
提示:“Hello,World!”中间没空格。
【输入样例】
(无)
【输出样例】
Hello,World!
#include <stdio.h> int main() { printf("Hello,World!"); }
1002:输出第二个整数
【输入样例】
123 456 789
【输出样例】
456
#include <stdio.h> int main() { int a, b, c; scanf("%d%d%d", &a, &b, &c); printf("%d", b); return 0; }
1003:对齐输出
【输入样例】
0 -1
【输出样例】
0 -1
#include <stdio.h> int main() { int a, b, c; scanf("%d%d%d", &a, &b, &c); printf("%8d %8d %8d",a, b, c); return 0; }
1004:字符三角形
【输入样例】
*
【输出样例】
*
*
*
#include <stdio.h> int main() { char c; scanf("%c", &c); printf(" %c\n", c); printf(" %c%c%c\n", c, c, c); printf("%c%c%c%c%c\n", c, c, c, c, c); return 0; }
1005:地球人口承载力估计
为了能够实现可持续发展,避免资源枯竭,地球最多能够养活多少亿人?
【输入样例】
110 90 90 210
【输出样例】
75.00
#include <stdio.h> int main() { int x, a, y, b; double G, Z; scanf("%d %d %d %d", &x, &a, &y, &b); G = (double)(x * a - y * b) / (a - b); Z = G; printf("%.2f", Z); return 0; }
第二章
第一节 运算符和表达式
2064:【例2.1】交换值
【输入样例】
2 3
【输出样例】
3 2
#include <stdio.h> int main() { int a, b, temp; scanf("%d %d", &a, &b); temp = a; a = b; b = temp; printf("%d %d", a, b); return 0; }
2065:【例2.2】整数的和
输入a、b、c这3个整数,求它们的和。
【输入样例】
1 2 3
【输出样例】
6
#include <stdio.h> int main() { int a, b, c, sum; scanf("%d %d %d", &a, &b, &c); sum = a + b + c; printf("%d\n", sum); return 0; }
2066:【例2.3】买图书
【输入样例】
100 100
【输出样例】
20.00
#include <stdio.h> int main() { float n, m, remaining; scanf("%f %f", &n, &m); remaining = n - (m * 0.8); printf("%.2f", remaining); return 0; }
1006:A+B问题
A+B问题的题目描述如下:给定两个整数A和B,输出A+B的值。保证A、B及结果均在整型范围内。现在请你解决这一问题。
【输入样例】
1 2
【输出样例】
3
#include <stdio.h> int main() { int A, B; scanf("%d %d", &A, &B); printf("%d\n", A + B); return 0; }
1007:计算(a+b)×c的值
【输入】
输入仅一行,包括三个整数a、b、c, 数与数之间以一个空格分开。(-10,000<a,b,c<10,000)
【输出】
输出一行,即表达式的值。
【输入样例】
2 3 5
【输出样例】
25
#include <stdio.h> int main() { int a, b, c, result; scanf("%d %d %d", &a, &b, &c); result = (a + b) * c; printf("%d\n", result); return 0; }
1008:计算(a+b)/c的值
【输入样例】
1 1 3
【输出样例】
0
#include <stdio.h> int main() { int a, b, c, result; scanf("%d %d %d", &a, &b, &c); if (c == 0) { printf("Error: division by zero\n"); return 1; } result = (a + b) / c; printf("%d\n", result); return 0; }
1009:带余除法
【输入样例】
10 3
【输出样例】
3 1
#include <stdio.h> int main() { int dividend, divisor, quotient, remainder; scanf("%d %d", ÷nd, &divisor); quotient = dividend / divisor; remainder = dividend % divisor; printf("%d %d\n", quotient, remainder); return 0; }
1010:计算分数的浮点数值
【题目描述】
两个整数a
和b
分别作为分子和分母,既分数ab
,求它的浮点数值(双精度浮点数,保留小数点后9
位)。
【输入】
输入仅一行,包括两个整数a
和b
。
【输出】
输出也仅一行,分数ab
的浮点数值(双精度浮点数,保留小数点后9
位)。
【输入样例】
5 7
【输出样例】
0.
#include <stdio.h> int main() { int a, b; double result; scanf("%d %d", &a, &b); if (b == 0) { printf("Error: division by zero\n"); return 1; } result = (double)a / b; printf("%.9lf\n", result); return 0; }
第二节 常量和变量
2067:【例2.5】圆
【输入样例】
1.0
【输出样例】
2.0000 6.2832 3.1416
#include <stdio.h> #include <math.h> int main() { double radius; scanf("%lf", &radius); double diameter = 2.0 * radius; double circumference = 2.0 * M_PI * radius; double area = M_PI * radius * radius; printf("%.4lf %.4lf %.4lf\n", diameter, circumference, area); return 0; }
2068:【例2.6】鸡兔同笼
【输入样例】
30 90
【输出样例】
15 15
#include <stdio.h> int main() { int x, y; scanf("%d %d", &x, &y); int t = (y - 2 * x) / 2; int j = x - t; printf("%d %d\n", j, t); return 0; }
1011:甲流疫情死亡率
【输入样例】
10433 60
【输出样例】
0.575%
#include <stdio.h> int main() { int confirmed, deaths; scanf("%d %d", &confirmed, &deaths); double mortalityRate = (double)deaths / confirmed * 100; printf("%.3lf%%\n", mortalityRate); return 0; }
1012:计算多项式的值
【题目描述】
对于多项式f(x)=ax3+bx2+cx+d
和给定的a,b,c,d,x
,计算f(x)
的值,保留到小数点后7
位。
【输入】
输入仅一行,包含5
个实数,分别是x
,及参数a、b、c、d
的值,每个数都是绝对值不超过100
的双精度浮点数。数与数之间以一个空格分开。
【输出】
输出一个实数,即f(x)
的值,保留到小数点后7
位。
【输入样例】
2.31 1.2 2 2 3
【输出样例】
33.0
#include <stdio.h> int main() { double x, a, b, c, d; scanf("%lf %lf %lf %lf %lf", &x, &a, &b, &c, &d); double fx = a * x * x * x + b * x * x + c * x + d; printf("%.7lf\n", fx); return 0; }
1013:温度表达转化
【题目描述】
利用公式 C=5×(F−32)÷9
(其中C
表示摄氏温度,F
表示华氏温度)进行计算转化,输入华氏温度F
,输出摄氏温度C
,要求精确到小数点后5
位。
【输入】
输入一行,包含一个实数F
,表示华氏温度。(F≥−459.67)
【输出】
输出一行,包含一个实数,表示对应的摄氏温度,要求精确到小数点后5
位。
【输入样例】
41
【输出样例】
5.00000
#include <stdio.h> int main() { double F, C; scanf("%lf", &F); C = 5 * (F - 32) / 9; printf("%.5lf\n", C); return 0; }
1014:与圆相关的计算
【题目描述】
给出圆的半径,求圆的直径、周长和面积。输入圆的半径实数r,输出圆的直径、周长、面积,每个数保留小数点后4
位。圆周率取值为3.14159
。
【输入】
输入包含一个实数r(0<r≤10,000)
,表示圆的半径。
【输出】
输出一行,包含三个数,分别表示圆的直径、周长、面积,数与数之间以一个空格分开,每个数保留小数点后4
位。
【输入样例】
3.0
【输出样例】
6.0000 18.8495 28.2743
#include <stdio.h> int main() { double r; const double PI = 3.14159; scanf("%lf", &r); double diameter = 2 * r; double circumference = 2 * PI * r; double area = PI * r * r; printf("%.4lf %.4lf %.4lf\n", diameter, circumference, area); return 0; }
1015:计算并联电阻的阻值
【题目描述】
对于阻值为 r1
和 r2
的电阻,其并联电阻阻值公式计算如下:R=1/(1/r1+1/r2)
。输入两个电阻阻抗大小,浮点型。输出并联之后的阻抗大小,结果保留小数点后2
位。
【输出】
并联之后的阻抗大小,结果保留小数点后2
位。
【输入样例】
1 2
【输出样例】
0.67
#include <stdio.h> int main() { double r1, r2, R; scanf("%lf %lf", &r1, &r2); R = 1 / (1 / r1 + 1 / r2); printf("%.2lf\n", R); return 0; }
第三节 标准数据类型
1414:【17NOIP普及组】成绩
总成绩 = 作业成绩 × 20% + 小测成绩 × 30% + 期末考试成绩 × 50%
牛牛想知道,这门课程自己最终能得到多少分。
【输入样例】
100 100 80
【输出样例】
90
【提示】
【输入输出样例 1 说明】 牛牛的作业成绩是 100 分,小测成绩是 100 分,期末考试成绩是 80 分,总成 绩是 100 × 20% + 100 × 30% + 80 × 50% = 20 + 30 + 40 = 90。
【输入输出样例 2】
输入:
【数据说明】
对于 30% 的数据,A = B = 0。
对于另外 30% 的数据,A = B = 100。
对于 100% 的数据, 0 ≤ A、B、C ≤ 100 且 A、B、C 都是 10 的整数倍。
#include <stdio.h> int main() { int A, B, C; scanf("%d %d %d", &A, &B, &C); int totalScore = A * 20 / 100 + B * 30 / 100 + C * 50 / 100; printf("%d", totalScore); return 0; }
1016:整型数据类型存储空间大小
【输入样例】
(无)
【输出样例】
(无)
#include <stdio.h> int main() { printf("%lu %lu\n", sizeof(int), sizeof(short)); return 0; }
1017:浮点型数据类型存储空间大小
【输入样例】
(无)
【输出样例】
(无)
#include <stdio.h> int main() { printf("%lu %lu\n", sizeof(float), sizeof(double)); return 0; }
1018:其他数据类型存储空间大小
【输入样例】
(无)
【输出样例】
(无)
#include <stdio.h> #include <stdbool.h> int main() { printf("%lu %lu\n", sizeof(bool), sizeof(char)); return 0; }
1019:浮点数向零舍入
【输入样例】
2.3
【输出样例】
2
#include <stdio.h> int main() { float num; scanf("%f", &num); int rounded = (int)num; printf("%d\n", rounded); return 0; }
1020:打印ASCII码
【输入样例】
A
【输出样例】
65
#include <stdio.h> int main() { char ch; scanf("%c", &ch); printf("%d\n", (int)ch); return 0; }
1021:打印字符
【输入样例】
65
【输出样例】
A
#include <stdio.h> int main() { int asciiCode; char character; scanf("%d", &asciiCode); character = (char)asciiCode; printf("%c\n", character); return 0; }
1022:整型与布尔型的转换(本题为C++)
【输入样例】
3
【输出样例】
1
#include <iostream> int main() { int intValue; std::cin >> intValue; bool boolValue = intValue; int result = boolValue; std::cout << result; return 0; }
1023:Hello,World!的大小
【输入样例】
(无)
【输出样例】
(无)
#include <stdio.h> int main() { printf("%zu\n", sizeof("Hello, World!")); return 0; }
第四节 数据输入输出
2069:【例2.12 】糖果游戏
【输入样例】
8 9 10 11 12
【输出样例】
11 7 9 11 6
#include <stdio.h> int main() { int candy[5]; int t[5]; for (int i = 0; i < 5; i++) { scanf("%d", &candy[i]); } for (int i = 0; i < 5; i++) { candy[i] -= candy[i] % 3; t[i] = candy[i] / 3; candy[i] -= t[i] * 2; candy[(i + 1) % 5] += t[i]; candy[(i - 1 + 5) % 5] += t[i]; } for (int i = 0; i < 5; i++) { printf("%5d", candy[i]); } return 0; }
1024:保留3位小数的浮点数
【输入样例】
12.34521
【输出样例】
12.345
#include <stdio.h> int main() { float number; scanf("%f", &number); printf("%.3f\n", number); return 0; }
1025:保留12位小数的浮点数
【输入样例】
3.98932
【输出样例】
3.0
#include <stdio.h> int main() { double number; scanf("%lf", &number); printf("%.12lf\n", number); return 0; }
1026:空格分隔输出
第二行是一个整数;
第三行是一个单精度浮点数;
第四行是一个双精度浮点数。
【输入样例】
a
12
2.3
3.2
【输出样例】
a 12 2. 3.
#include <stdio.h> int main() { char ch; int integer; float singleFloat; double doubleFloat; scanf("%c", &ch); scanf("%d", &integer); scanf("%f", &singleFloat); scanf("%lf", &doubleFloat); printf("%c %d %.6f %.6lf\n", ch, integer, singleFloat, doubleFloat); return 0; }
1027:输出浮点数
第二行是按“%f”保留5位小数输出的双精度浮点数;
第三行是按“%e”输出的双精度浮点数;
第四行是按“%g”输出的双精度浮点数。
【输入样例】
12.
【输出样例】
12.
12.34568
1.e+001
12.3457
#include <stdio.h> int main() { double number; scanf("%lf", &number); printf("%f\n", number); printf("%.5f\n", number); printf("%e\n", number); printf("%g\n", number); return 0; }
1028:字符菱形
【输入样例】
*
【输出样例】
*
*
*
*
*
#include <stdio.h> int main() { char ch; int i, j, space; scanf("%c", &ch); for (i = 1; i <= 3; i++) { for (space = 3 - i; space > 0; space--) { printf(" "); } for (j = 0; j < 2 * i - 1; j++) { printf("%c", ch); } printf("\n"); } for (i = 2; i >= 1; i--) { for (space = 3 - i; space > 0; space--) { printf(" "); } for (j = 0; j < 2 * i - 1; j++) { printf("%c", ch); } printf("\n"); } return 0; }
第五节 顺序结构实例
2070:【例2.13】数字对调
【输入样例】
123
【输出样例】
321
#include <stdio.h> int main() { int num, swappedNum; int hundreds, tens, ones; scanf("%d", &num); hundreds = num / 100; tens = (num / 10) % 10; ones = num % 10; swappedNum = ones * 100 + tens * 10 + hundreds; printf("%d\n", swappedNum); return 0; }
2071:【例2.14】平均分
【输入样例】
2 3
【输出样例】
85.8000
#include <stdio.h> int main() { int x, y; double totalScore, averageScore; scanf("%d %d", &x, &y); totalScore = x * 87.0 + y * 85.0; averageScore = totalScore / (x + y); printf("%.4f\n", averageScore); return 0; }
2072:【例2.15】歌手大奖赛
【输入样例】
(无)
【输出样例】
(无)
#include <stdio.h> int main() { double totalScore = 9.6 * 6; double highestScore = totalScore - 9.4 * 5; double lowestScore = totalScore - 9.8 * 5; double totalScoreWithoutMaxMin = totalScore - highestScore - lowestScore; double averageWithoutMaxMin = totalScoreWithoutMaxMin / 4; printf("%5.2f\n", averageWithoutMaxMin); return 0; }
2073:【例2.16 】三角形面积
提示:海伦公式
s=p(p−a)(p−b)(p−c)−−−−−−−−−−−−−−−−−√
,其中p=a+b+c2
。
【输入样例】
2.1 3.1 4.1
【输出样例】
3.179
#include <stdio.h> #include <math.h> int main() { double a, b, c, p, area; scanf("%lf %lf %lf", &a, &b, &c); p = (a + b + c) / 2; area = sqrt(p * (p - a) * (p - b) * (p - c)); printf("%.3f\n", area); return 0; }
1029:计算浮点数相除的余
【题目描述】
计算两个双精度浮点数a
和b
的相除的余数,a
和b
都是双精度浮点数。这里余数(r)
的定义是:a=k×b+r
,其中k
是整数,0≤r<b
。
【输入】
输入仅一行,包括两个双精度浮点数a
和b
。
【输出】
输出也仅一行,a÷b
的余数。
【输入样例】
73.263 0.9973
【输出样例】
0.4601
#include <stdio.h> int main() { double a, b; scanf("%lf %lf", &a, &b); int k = a / b; printf("%g", a-k*b); return 0; }
1030:计算球的体积
【题目描述】
对于半径为 r
的球,其体积的计算公式为V=43πr3
,这里取 π=3.14
。现给定 r
,即球半径,类型为double,求球的体积V
,保留到小数点后2
位。
【输入】
输入为一个不超过 100
的非负实数,即球半径,类型为double。
【输出】
输出一个实数,即球的体积,保留到小数点后 2
位。
【输入样例】
4
【输出样例】
267.95
#include <stdio.h> int main() { double r, volume; const double pi = 3.14; scanf("%lf", &r); volume = (4.0 / 3.0) * pi * r * r * r; printf("%.2f\n", volume); return 0; }
1031:反向输出一个三位数
【输入样例】
100
【输出样例】
001
#include <stdio.h> int main() { int n, reversedNumber; int hundreds, tens, ones; scanf("%d", &n); hundreds = n / 100; tens = (n / 10) % 10; ones = n % 10; reversedNumber = ones * 100 + tens * 10 + hundreds; printf("%03d\n", reversedNumber); return 0; }
1032:大象喝水查
【输入样例】
23 11
【输出样例】
3
#include <stdio.h> #include <math.h> int main() { int h, r; double volumePerBucket; int buckets; scanf("%d %d", &h, &r); volumePerBucket = M_PI * r * r * h / 1000.0; buckets = 20.0 / volumePerBucket; if (20.0 > volumePerBucket * buckets) { buckets++; } printf("%d\n", buckets); return 0; }
1033:计算线段长度
【题目描述】
已知线段的两个端点的坐标A(Xa,Ya)
,B(Xb,Yb)
,求线段AB
的长度,保留到小数点后3
位。
【输入】
第一行是两个实数Xa,Ya
,即A
的坐标。
第二行是两个实数Xb,Yb
,即B
的坐标。

【输出】
一个实数,即线段AB
的长度,保留到小数点后3
位。
【输入样例】
1 1
2 2
【输出样例】
1.414
#include <stdio.h> #include <math.h> int main() { double Xa, Ya, Xb, Yb, length; scanf("%lf %lf", &Xa, &Ya); scanf("%lf %lf", &Xb, &Yb); length = sqrt(pow(Xb - Xa, 2) + pow(Yb - Ya, 2)); printf("%.3f\n", length); return 0; }
1034:计算三角形面积
【题目描述】
平面上有一个三角形,它的三个顶点坐标分别为(x1,y1),(x2,y2),(x3,y3)
,那么请问这个三角形的面积是多少,精确到小数点后两位。
【输入】
输入仅一行,包括6
个双精度浮点数,分别对应x1,y1,x2,y2,x3,y3
。
【输入样例】
0 0 4 0 0 3
【输出样例】
6.00
#include <stdio.h> #include <math.h> int main() { double x1, y1, x2, y2, x3, y3, area; scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3); area = fabs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0; printf("%.2f\n", area); return 0; }
1035:等差数列末项计算
【题目描述】
给出一个等差数列的前两项a1,a2
,求第n
项是多少。
【输入】
一行,包含三个整数a1,a2,n
。−100≤a1,a2≤100,0<n≤1000
。
【输出】
一个整数,即第n
项的值。
【输入样例】
1 4 100
【输出样例】
298
#include <stdio.h> int main() { int a1, a2, n, d, an; scanf("%d %d %d", &a1, &a2, &n); d = a2 - a1; an = a1 + (n - 1) * d; printf("%d\n", an); return 0; }
1036:A×B问题
【题目描述】
输入两个正整数A
和B
,求A×B
的值。注意乘积的范围和数据类型的选择。
【输入】
一行,包含两个正整数A
和B
,中间用单个空格隔开。1≤A,B≤50000
。
【输出】
一个整数,即 A×B
的值。
【输入样例】
3 4
【输出样例】
12
#include <stdio.h> int main() { long long A, B, product; scanf("%lld %lld", &A, &B); product = A * B; printf("%lld\n", product); return 0; }
1037:计算2的幂
【题目描述】
给定非负整数n
,求2n
的值,即2
的n
次方。
【输入】
一个整数n
。0≤n<31
。
【输出】
一个整数,即2
的n
次方。
【输入样例】
3
【输出样例】
8
#include <stdio.h> int main() { int n, result; scanf("%d", &n); result = 1 << n; printf("%d\n", result); return 0; }
1038:苹果和虫子
【输入样例】
10 4 9
【输出样例】
7
#include <stdio.h> int main() { int n, x, y, eaten, remaining; scanf("%d %d %d", &n, &x, &y); eaten = y / x; if (y % x > 0) { eaten++; } remaining = n - eaten; if (remaining < 0) { remaining = 0; } printf("%d\n", remaining); return 0; }
第三章
第一节 if选择结构
2051:【例3.1】偶数
【题目描述】
读入一个正整数a
,如果a
为偶数输出yes。
【输入】
一个正整数a
。
【输入样例】
12
【输出样例】
yes
#include <stdio.h> int main() { int a; scanf("%d", &a); if (a % 2 == 0) { printf("yes\n"); } return 0; }
2052:【例3.2】范围判断
【题目描述】
读入一个整数,若这个数大于1并且小于100
,则输出yes。
【输入样例】
76
【输出样例】
yes
#include <stdio.h> int main() { int a; scanf("%d", &a); if (a > 1 && a < 100) { printf("yes\n"); } return 0; }
2053:【例3.3】三个数
【输入样例】
3 2 1
【输出样例】
3 2 1
#include <stdio.h> int main() { int a, b, c, temp; scanf("%d %d %d", &a, &b, &c); if (a < b) { temp = a; a = b; b = temp; } if (a < c) { temp = a; a = c; c = temp; } if (b < c) { temp = b; b = c; c = temp; } printf("%d %d %d\n", a, b, c); return 0; }
2054:【例3.4】适合晨练
【题目描述】
输入温度t
的值,判断是否适合晨练。(25≤t≤30
,则适合晨练ok!,否则不适合no!)
【输入】
输入温度t
的值。
【输入样例】
26
【输出样例】
ok!
#include <stdio.h> int main() { int t; scanf("%d", &t); if (t >= 25 && t <= 30) { printf("ok!\n"); } else { printf("no!\n"); } return 0; }
2055:【例3.5】收费
【输入样例】
20.00
【输出样例】
33.60
#include <stdio.h> int main() { double weight, fee; scanf("%lf", &weight); if (weight <= 20) { fee = weight * 1.68; } else { fee = weight * 1.98; } printf("%.2f\n", fee); return 0; }
2056:【例3.7】最大的数
【输入样例】
1.01 2 3.22
【输出样例】
3.22
#include <stdio.h> int main() { double num1, num2, num3, max; scanf("%lf %lf %lf", &num1, &num2, &num3); max = num1; if (num2 > max) { max = num2; } if (num3 > max) { max = num3; } printf("%.2f\n", max); return 0; }
1039:判断数正负
【题目描述】
给定一个整数N
,判断其正负。如果N>0
,输出positive;如果N=0
,输出zero;如果N<0
,输出negative。
【输入】
一个整数N(−109≤N≤109)
。
【输出】
如果N>0
, 输出positive;
【输入样例】
1
【输出样例】
positive
#include <stdio.h> int main() { int N; scanf("%d", &N); if (N > 0) { printf("positive\n"); } else if (N == 0) { printf("zero\n"); } else { printf("negative\n"); } return 0; }
1040:输出绝对值
【输入样例】
-3.14
【输出样例】
3.14
#include <stdio.h> #include <math.h> int main() { double num; scanf("%lf", &num); printf("%.2f\n", fabs(num)); return 0; }
1041:奇偶数判断
【输入样例】
5
【输出样例】
odd
#include <stdio.h> int main() { int n; scanf("%d", &n); if (n % 2 == 0) { printf("even\n"); } else { printf("odd\n"); } return 0; }
1042:奇偶ASCII值判断
【题目描述】
任意输入一个字符,判断其ASCII
是否是奇数,若是,输出YES
,否则,输出NO
。例如,字符A
的ASCII
值是65
,则输出YES
,若输入字符B
(ASCII
值是66
),则输出NO
。
【输出】
如果其ASCII
值为奇数,则输出YES
,否则,输出NO
。
【输入样例】
A
【输出样例】
YES
#include <stdio.h> int main() { char ch; scanf("%c", &ch); if ((int)ch % 2 != 0) { printf("YES\n"); } else { printf("NO\n"); } return 0; }
1043:整数大小比较
【题目描述】
输入两个整数,比较它们的大小。若x>y
,输出>
;若x=y
,输出=
;若x<y
,输出<
。
【输入】
一行,包含两个整数x
和y
,中间用单个空格隔开。0≤x<232
, −231≤y<231
。
【输出】
一个字符。若x>y
,输出 >
;若x=y
,输出 =
;若x<y
,输出 <
;
【输入样例】
1000 100
【输出样例】
>
#include <stdio.h> int main() { int x, y; scanf("%d %d", &x, &y); if (x > y) { printf(">\n"); } else if (x == y) { printf("=\n"); } else { printf("<\n"); } return 0; }
1044:判断是否为两位数
【题目描述】
判断一个正整数是否是两位数(即大于等于10
且小于等于99
)。若该正整数是两位数,输出1
,否则输出0
。
【输入】
一个正整数,不超过1000
。
【输出】
一行。若该正整数是两位数,输出1
,否则输出0
。
【输入样例】
54
【输出样例】
1
#include <stdio.h> int main() { int num; scanf("%d", &num); if (num >= 10 && num <= 99) { printf("1\n"); } else { printf("0\n"); } return 0; }
1045:收集瓶盖赢大奖
【输入样例】
11 19
【输出样例】
1
#include <stdio.h> int main() { int luckyCaps, encourageCaps; scanf("%d %d", &luckyCaps, &encourageCaps); if (luckyCaps >= 10 || encourageCaps >= 20) { printf("1\n"); } else { printf("0\n"); } return 0; }
1046:判断一个数能否同时被3和5整除
【输入样例】
15
【输出样例】
YES
#include <stdio.h> int main() { int n; scanf("%d", &n); if (n % 15 == 0) { printf("YES\n"); } else { printf("NO\n"); } return 0; }
1047:判断能否被3,5,7整除
【题目描述】
给定一个整数,判断它能否被3
,5
,7
整除,并输出以下信息:
1、能同时被3
,5
,7
整除(直接输出3 5 7,每个数中间一个空格);
2、只能被其中两个数整除(输出两个数,小的在前,大的在后。例如:3 5或者3 7或者5 7,中间用空格分隔);
3、只能被其中一个数整除(输出这个除数);
4、不能被任何数整除,输出小写字符‘n’,不包括单引号。
【输出】
输出一行,按照描述要求给出整数被3
,5
,7
整除的情况。
【输入样例】
105
【输出样例】
3 5 7
#include <stdio.h> int main() { int n; scanf("%d", &n); int div3 = (n % 3 == 0); int div5 = (n % 5 == 0); int div7 = (n % 7 == 0); if (div3 && div5 && div7) { printf("3 5 7\n"); } else if (div3 && div5) { printf("3 5\n"); } else if (div3 && div7) { printf("3 7\n"); } else if (div5 && div7) { printf("5 7\n"); } else if (div3) { printf("3\n"); } else if (div5) { printf("5\n"); } else if (div7) { printf("7\n"); } else { printf("n\n"); } return 0; }
1048:有一门课不及格的学生
【输入样例】
50 80
【输出样例】
1
#include <stdio.h> int main() { int chinese, math; scanf("%d %d", &chinese, &math); if ((chinese < 60 && math >= 60) || (chinese >= 60 && math < 60)) { printf("1\n"); } else { printf("0\n"); } return 0; }
第二节 switch语句
2057:【例3.9 】星期几
1、星期一:Monday
2、星期二:Tuesday
3、星期三:Wednesday
4、星期四:Thursday
5、星期五:Friday
6、星期六:Saturday
7、星期日:Sunday
错误输入请输出:input error!
【输入样例】
1
【输出样例】
Monday
#include <stdio.h> int main() { int day; scanf("%d", &day); switch(day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday\n"); break; default: printf("input error!\n"); break; } return 0; }
2058:【例3.10】简单计算器
(1)如果出现除数为0的情况,则输出:Divided by zero!
(2)如果出现无效的操作符(即不为+,-,*,/之一),则输出:Invalid operator!
【输入样例】
34 56 +
【输出样例】
90
【提示】
【样例2】
输入:
0.8
#include <stdio.h> int main() { float a, b; char c; scanf("%f %f %c", &a, &b, &c); switch (c) { case '+': printf("%g", a + b); break; case '-': printf("%g", a - b); break; case '*': printf("%g", a * b); break; case '/': if (b == 0) { printf("Divided by zero!"); } else { printf("%g", a / b); } break; default: printf("Invalid operator!"); break; } return 0; }
2059:【例3.11】买笔
【题目描述】
期末来临了,班长小Q决定将剩余班费x
元钱,用于购买若干支钢笔奖励给一些学习好、表现好的同学。已知商店里有三种钢笔,它们的单价为6
元、5
元和4
元。小Q想买尽量多的笔(鼓励尽量多的同学),同时他又不想有剩余钱。请您编一程序,帮小Q制订出一种买笔的方案。
【输入】
一个正整数x
(剩余班费)。
【输入样例】
10
【输出样例】
1 0 1
#include <stdio.h> int main() { int a, b, c; int x, y; scanf("%d", &x); c = x / 4; y = x % 4; switch (y) { case 0: { a = 0; b = 0; break; } case 1: { a = 0; b = 1; c--; break; } case 2: a = 1; b = 0; c--; break; case 3: a = 1; b = 1; c-= 2; } printf("%d %d %d", a, b, c); return 0; }
1049:晶晶赴约会
【输入样例】
2
【输出样例】
YES
#include <stdio.h> int main() { int day; scanf("%d", &day); if (day == 1 || day == 3 || day == 5) { printf("NO\n"); } else { printf("YES\n"); } return 0; }
1050:骑车与走路
【输入样例】
120
【输出样例】
Bike
#include <stdio.h> int main() { int distance; scanf("%d", &distance); double walkTime = distance / 1.2; double bikeTime = 50 + (double)distance / 3.0; if (walkTime < bikeTime) { printf("Walk\n"); } else if (bikeTime < walkTime) { printf("Bike\n"); } else { printf("All\n"); } return 0; }
1051:分段函数
【题目描述】
编写程序,计算下列分段函数y=f(x)
的值。结果保留到小数点后三位。
y=−x+2.5;0≤x<5
y=2−1.5(x−3)(x−3);5≤x<10
y=x2−1.5;10≤x<20
【输入】
一个浮点数N(0≤N<20)
。
【输出】
输出 N
对应的分段函数值:f(N)
。结果保留到小数点后三位。
【输入样例】
1.0
【输出样例】
1.500
#include <stdio.h> int main() { float a, b; scanf("%f", &a); if (a >= 0 && a < 5) { b = -a + 2.5; } else if (a >= 5 && a < 10) { b = 2 - 1.5 * (a - 3) * (a - 3); } else { b = a / 2 - 1.5; } printf("%.3f", b); return 0; }
1052:计算邮资
【输入样例】
1200 y
【输出样例】
17
#include <stdio.h> int main() { int weight; char isUrgent; int fee = 8; scanf("%d %c", &weight, &isUrgent); if (weight > 1000) { int extraWeight = weight - 1000; int extraFee = ((extraWeight - 1) / 500 + 1) * 4; fee += extraFee; } if (isUrgent == 'y') { fee += 5; } printf("%d", fee); return 0; }
1053:最大数输出
【输入样例】
10 20 56
【输出样例】
56
#include <stdio.h> int main() { int a, b, c, max; scanf("%d %d %d", &a, &b, &c); max = a; if (b > max) max = b; if (c > max) max = c; printf("%d", max); return 0; }
1054:三角形判断
【输入样例】
3 4 5
【输出样例】
yes
#include <stdio.h> int main() { int a, b, c; scanf("%d %d %d", &a, &b, &c); if (a + b > c && a + c > b && b + c > a) { printf("yes"); } else { printf("no"); } return 0; }
1055:判断闰年
【输入样例】
2006
【输出样例】
N
#include <stdio.h> int main() { int year; scanf("%d", &year); if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { printf("Y"); } else { printf("N"); } return 0; }
1056:点和正方形的关系
【输入样例】
1 1
【输出样例】
yes
#include <stdio.h> int main() { int x, y; scanf("%d %d", &x, &y); if (x >= -1 && x <= 1 && y >= -1 && y <= 1) { printf("yes"); } else { printf("no"); } return 0; }
1057:简单计算器
1. 如果出现除数为0的情况,则输出:Divided by zero!
2. 如果出现无效的操作符(即不为 +, -, *, / 之一),则输出:Invalid operator!
1.如果出现除数为0的情况,则输出:Divided by zero!
2.如果出现无效的操作符(即不为 +, -, *, / 之一),则输出:Invalid operator!
【输入样例】
1 2 +
【输出样例】
3
#include <stdio.h> int main() { int a, b; char op; scanf("%d %d %c", &a, &b, &op); switch (op) { case '+': printf("%d", a + b); break; case '-': printf("%d", a - b); break; case '*': printf("%d", a * b); break; case '/': if (b == 0) { printf("Divided by zero!"); } else { printf("%d", a / b); } break; default: printf("Invalid operator!"); break; } return 0; }
1058:求一元二次方程
【题目描述】
利用公式x1=−b+b2−4ac√2a,x2=−b−b2−4ac√2a
,求一元二次方程ax2+bx+c=0
的根,其中a
不等于0
。结果要求精确到小数点后5
位。
【输入】
输入一行,包含三个浮点数a,b,c
(它们之间以一个空格分开),分别表示方程ax2+bx+c=0
的系数。
若无实根输出“No answer!”。
所有输出部分要求精确到小数点后5位,数字、符号之间没有空格。
【输入样例】
-15.97 19.69 12.02
【输出样例】
x1=-0.44781;x2=1.68075
#include <stdio.h> #include <math.h> int main() { double a, b, c, discriminant, x1, x2; scanf("%lf %lf %lf", &a, &b, &c); discriminant = b*b - 4*a*c; if (discriminant < 0) { printf("No answer!"); } else { x1 = (-b + sqrt(discriminant)) / (2*a); x2 = (-b - sqrt(discriminant)) / (2*a); if (fabs(x1 - x2) < 1e-5) { printf("x1=x2=%.5lf", x1); } else { if (x1 > x2) { printf("x1=%.5lf;x2=%.5lf", x2, x1); } else { printf("x1=%.5lf;x2=%.5lf", x1, x2); } } } return 0; }
第四章
第一节 for循环
2016:【例4.1】for循环求和
【题目描述】
利用for循环。计算输出1+2+3+...+n
的和。
【输入】
输入n
。
【输入样例】
10
【输出样例】
55
【提示】
【数据规模及约定】
#include <stdio.h> int main() { int n, sum = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { sum += i; } printf("%d", sum); return 0; }
2017:【例4.2】输出偶数
【题目描述】
按照由小到大的顺序,输出1∼n
之间的所有偶数。
【输入】
输入n
。
【输入样例】
10
【输出样例】
2 4 6 8 10
【提示】
【数据规模及约定】
#include <stdio.h> int main() { int n; scanf("%d", &n); for (int i = 2; i <= n; i += 2) { printf("%d", i); if (i < n - 1) printf(" "); } return 0; }
2018:【例4.3】输出奇偶数之和
【题目描述】
利用for循环,分别输出1∼n
之间的所有奇数的和、偶数的和。
【输入】
输入n
。
【输入样例】
10
【输出样例】
30 25
【提示】
【数据规模及约定】
#include <stdio.h> int main() { int n, evenSum = 0, oddSum = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { if (i % 2 == 0) { evenSum += i; } else { oddSum += i; } } printf("%d %d", evenSum, oddSum); return 0; }
2019:【例4.4】求阶乘
【题目描述】
利用for循环求n!
的值。
【输入】
输入一个正整数n
。
【输出】
输出n!
的值。
【输入样例】
4
【输出样例】
24
【提示】
【数据规模及约定】
#include <stdio.h> int main() { int n; long long factorial = 1; scanf("%d", &n); for (int i = 1; i <= n; ++i) { factorial *= i; } printf("%lld", factorial); return 0; }
1059:求平均年龄
【输入】
第一行有一个整数n
(1≤n≤100
),表示学生的人数。其后n
行每行有1
个整数,表示每个学生的年龄,取值为15
到25
。
【输入样例】
2
18
17
【输出样例】
17.50
#include <stdio.h> int main() { int n, age, sum = 0; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &age); sum += age; } printf("%.2f", sum / (double)n); return 0; }
1060:均值
【输入样例】
2
1.0 3.0
【输出样例】
2.0000
#include <stdio.h> int main() { int n; double sum = 0.0, value; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%lf", &value); sum += value; } printf("%.4f", sum / n); return 0; }
1061:求整数的和与均值
【题目描述】 读入n(1≤n≤10000)个整数,求它们的和与均值。
【输入】 输入第一行是一个整数n,表示有n个整数。 第2~n+1行每行包含1个整数。每个整数的绝对值均不超过10000。
【输出】 输出一行,先输出和,再输出平均值(保留到小数点后5位),两个数间用单个空格分隔。
【输入样例】 4 344 222 343 222
【输出样例】 1131 282.75000
#include <stdio.h> int main() { int n, i, num; long long sum = 0; double average; scanf("%d", &n); for(i = 0; i < n; i++) { scanf("%d", &num); sum += num; } average = (double)sum / n; printf("%lld %.5f", sum, average); return 0; }
1062:最高的分数
【输入样例】
5
85 78 90 99 60
【输出样例】
99
#include <stdio.h> int main() { int n, score, maxScore = 0; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &score); if(score > maxScore) { maxScore = score; } } printf("%d", maxScore); return 0; }
1063:最大跨度值
【输入样例】
6
3 0 8 7 5 9
【输出样例】
9
#include <stdio.h> int main() { int n, num, max = 0, min = 1000; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &num); if(num > max) { max = num; } if(num < min) { min = num; } } printf("%d", max - min); return 0; }
1064:奥运奖牌计数
【输入样例】
3
1 0 3
3 1 0
0 3 0
【输出样例】
4 4 3 11
#include <stdio.h> int main() { int n, gold, silver, bronze; int totalGold = 0, totalSilver = 0, totalBronze = 0, totalMedals = 0; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d %d %d", &gold, &silver, &bronze); totalGold += gold; totalSilver += silver; totalBronze += bronze; } totalMedals = totalGold + totalSilver + totalBronze; printf("%d %d %d %d", totalGold, totalSilver, totalBronze, totalMedals); return 0; }
1065:奇数求和
【题目描述】
计算非负整数m
到n
(包括m
和n
)之间的所有奇数的和,其中,m
不大于 n
,且n
不大于300
。例如 m=3,n=12
, 其和则为:3+5+7+9+11=35
。
【输入】
两个数 m
和 n
,两个数以一个空格分开,其中 0≤m≤n≤300
。
【输出】
输出一行,包含一个整数,表示m
到 n
(包括m
和 n
)之间的所有奇数的和
【输入样例】
7 15
【输出样例】
55
#include <stdio.h> int main() { int m, n, sum = 0; scanf("%d %d", &m, &n); for(int i = m; i <= n; i++) { if(i % 2 != 0) { sum += i; } } printf("%d", sum); return 0; }
1066:满足条件的数累加
【题目描述】
将正整数m
和n
之间(包括m
和n
)能被17
整除的数累加,其中,0<m<n<1000
。
【输入】
一行,包含两个整数m
和n
,其间,以一个空格间隔。
【输入样例】
50 85
【输出样例】
204
#include <stdio.h> int main() { int m, n, sum = 0; scanf("%d %d", &m, &n); for(int i = m; i <= n; i++) { if(i % 17 == 0) { sum += i; } } printf("%d", sum); return 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/133246.html