C理论A_编程练习1-顺序结构

主要是一些基础语法,输入输出,和对类型的理解。

7-1 计算整数各位数字之和

这里直接用循环实现了。

1
2
3
4
5
6
7
8
9
#include<stdio.h>
int main() {
int a, b = 0;
scanf("%d", &a);
while (a)
b += a % 10, a /= 10;
printf("%d", b);
return 0;
}

7-2 计算代数表达式

使用内置的数学函数解决

1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
#include<math.h>
#define PI 3.14159
#define to(x) (x * PI / 180)
int main() {
double x;
scanf("%lf", &x);
printf("%.2lf", sqrt((sin(to(60)) + 1) * (sin(to(30)) + 1) / cos(to(x))));
return 0;
}

7-3 A乘以B

1
2
3
4
5
6
7
#include<stdio.h>
int main() {
int a, b;
scanf("%d%d", &a, &b);
printf("%d", a * b);
return 0;
}

7-4 是不是太胖了

1
2
3
4
5
6
7
#include<stdio.h>
int main() {
int h;
scanf("%d", &h);
printf("%.1f", (h - 100) * 0.9f * 2);
return 0;
}

7-5 整数四则运算

不会函数和指针者慎重参考。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
char op[] = {'+', '-', '*', '/'};
typedef int (*expr)(int, int);
int plus(int a, int b) {return a + b;}
int minus(int a, int b) {return a - b;}
int mult(int a, int b) {return a * b;}
int div(int a, int b) {return a / b;}
expr funcs[] = {plus, minus, mult, div};
int main() {
int a, b;
scanf("%d%d", &a, &b);
for (int i = 0;i < 4;i++) {
printf("%d %c %d = %d\n", a, op[i], b, funcs[i](a, b));
}
return 0;
}

7-6 杨辉三角

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>
#define N 5
int a[2][N + 10];
int main() {
for (int i = 1;i <= N;i++) {
for (int j = 1;j <= i;j++) {
if (j == 1 || j == i) {
a[i % 2][j] = 1;
} else {
a[i % 2][j] = a[(i-1) % 2][j - 1] + a[(i-1) % 2][j];
}
printf("%3d", a[i % 2][j]);
}
putchar('\n');
}
return 0;
}

7-7 计算物体自由下落的距离

1
2
3
4
5
6
7
#include<stdio.h>
int main() {
int t;
scanf("%d", &t);
printf("height = %.2f", 0.5f * 9.8f * t * t);
return 0;
}

7-8 厘米换算英尺英寸

主要考察算数运算和类型强制转换

1
2
3
4
5
6
7
8
#include<stdio.h>
int main() {
int cm;
scanf("%d", &cm);
float tot = cm / 100.0f / 0.3048f;
printf("%d %d", (int)tot, (int)((tot - (int)tot) * 12));
return 0;
}

C理论A_编程练习2-选择结构

7-1 A除以B

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
int main() {
int a, b;
scanf("%d%d", &a, &b);
if (b > 0) {
printf("%d/%d=%.2f", a, b, a*1.0f/b);
} else if (b < 0) {
printf("%d/(%d)=%.2f", a, b, a*1.0f/b);
} else {
printf("%d/%d=Error", a, b);
}
return 0;
}

7-2 出租车计价

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
int main() {
float d, fee;
int t;
scanf("%f%d", &d, &t);
if (d <= 3) {
fee = 10;
} else if (d <= 10) {
fee = 10 + (d - 3) * 2;
} else {
fee = 10 + (d - 3) * 2 + (d - 10) * 1;
}
fee += t / 5 * 2;
printf("%.0f", fee);
return 0;
}

7-3 输出英文字母的前驱和后继字母

考虑ascii码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
int main() {
char s[10];
scanf("%s", s);
char c = s[0];
char prv = c - 1, nxt = c + 1;
if (c >= 'a' && c <= 'z') {
if (prv < 'a') {
prv = 'z';
}
if (nxt > 'z') {
nxt = 'a';
}
} else {
if (prv < 'A') {
prv = 'Z';
}
if (nxt > 'Z') {
nxt = 'A';
}
}
printf("%c %d\n%c %d", prv, (int)prv, nxt, (int)nxt);
return 0;
}

7-4 后天

1
2
3
4
5
6
7
#include<stdio.h>
int main() {
int d;
scanf("%d", &d);
printf("%d", (d + 2) % 8 + (d + 2) / 8);
return 0;
}

7-5 后天日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
int t[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int getDay(int y, int m) {
if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))) {
return 29;
}
return t[m];
}
int main() {
int y, m, d;
scanf("%d-%d-%d", &y, &m, &d);
d += 2;
int tot = getDay(y ,m);
if (d > tot) {
d -= tot;
m ++;
}
if (m > 12) {
m -= 12;
y ++;
}
printf("%d.%02d.%02d", y, m, d);
return 0;
}

7-6 计算工资

1
2
3
4
5
6
7
8
9
#include<stdio.h>
#define max(a, b) (a > b ? a : b)
int main() {
int y, h;
scanf("%d%d", &y, &h);
int b = y >= 5 ? 50 : 30;
printf("%.2f", (h * b + b * max(0, h - 40) * 0.5f));
return 0;
}

7-7 能买手机吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>
#include<math.h>
int main() {
int p;
scanf("%d", &p);
if (p > 40 * 20 * 4) {
float l = (p - 40 * 20 * 4) / 40.0f;
if (l > 4 * 10) {
printf("需加班%.0f小时,买不起", ceil(l));
} else {
printf("需加班%.0f小时,可购买", ceil(l));
}
} else {
puts("不需加班,可购买");
}
return 0;
}

7-8 谁是赢家

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
int main() {
int pa, pb, c, b = 0;
scanf("%d%d", &pa, &pb);
for (int i = 0;i < 3;i++) {
scanf("%d", &c);
b += c;
}
if ((pb > pa && b > 0) || (pb < pa && b == 3)) {
printf("The winner is b: %d + %d", pb, b);
} else {
printf("The winner is a: %d + %d", pa, 3 - b);
}
return 0;
}

7-9 比较大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int main() {
int a[10];
for (int i = 0;i < 3;i++) {
scanf("%d", &a[i]);
}
for (int i = 0;i < 3;i++) {
for (int j = 0;j < 2 - i;j++) {
if (a[j] > a[j + 1]) {
a[j] ^= a[j + 1];
a[j + 1] ^= a[j];
a[j] ^= a[j + 1];
}
}
}
for (int i = 0;i < 3;i++) {
printf("->%d" + (i == 0) * 2, a[i]);
}
return 0;
}

C理论A_编程练习3-循环结构

7-1 求平方根序列前N项和

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#include<math.h>
int main() {
int n;
scanf("%d", &n);
double s = 0;
for (int i = 1;i <= n;i++) {
s += sqrt(i);
}
printf("sum = %.2lf", s);
return 0;
}

7-2 统计整数的位数

为什么写成do-while?因为0的特判。

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
int main() {
int n, l = 0;
scanf("%d", &n);
do {
n /= 10;
l ++;
} while (n);
printf("It contains %d digits.", l);
return 0;
}

7-3 输出闰年

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int main() {
int n;
scanf("%d", &n);
if (n <= 2000 || n > 2100) {
puts("Invalid year!");
} else {
int f = 1;
for (int i = 2001;i <= n;i++) {
if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) {
printf("%d\n", i);
f = 0;
}
}
if (f) {
puts("None");
}
}
return 0;
}

7-4 求分数序列前N项和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
int main() {
int n;
scanf("%d", &n);
double s = 0;
double x = 2, y = 1;
while (n--) {
s += x / y;
double t = x;
x = x + y;
y = t;
}
printf("%.2lf", s);
return 0;
}

7-5 求给定精度的简单交错序列部分和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
#define ll long long
int main() {
double eps;
scanf("%lf", &eps);
double s = 0, c;
ll i = 0;
do {
c = 1.0 / (1 + 3 * i);
s += (i&1) ? -c : c;
i++;
} while (c > eps);
printf("sum = %.6lf", s);
return 0;
}

7-6 求cosx的近似值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
int main() {
double x;
int n;
scanf("%lf%d", &x, &n);
double s = 0, m = 1, y = 1;
for (int i = 0;i <= n;i++) {
s += y / m * ((i&1) ? -1 : 1);
y *= x * x;
m *= (2 * i + 1) * (2 * i + 2);
}
printf("cos(%.6lf)=%.6lf", x, s);
return 0;
}

7-7 统计学生成绩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int l[] = {90, 80, 70, 60, 0};
int cs[5];
int main() {
int n, c;
scanf("%d", &n);
for (int i = 0;i < n;i++) {
scanf("%d", &c);
for (int j = 0;j < 5;j++) {
if (c >= l[j]) {
cs[j]++;
break;
}
}
}
for (int i = 0;i < 5;i++) {
printf(" %d" + !i, cs[i]);
}
return 0;
}

7-8 最佳情侣身高差

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
int main() {
int n;
scanf("%d", &n);
double c;
char s[10];
while (n--) {
scanf("%s%lf", s, &c);
printf("%.2lf\n", s[0] == 'M' ? c / 1.09 : c * 1.09);
}
return 0;
}

7-9 简写转全称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
char s[1024];
int main() {
scanf("%s", s);
int i = 0;
while (s[i] != '\0') {
switch(s[i++]) {
case 'c':case 'C':
puts("BEIJING OLYMPIC GAMES");
break;
case 'j':case 'J':
puts("JAPAN WORLD CUP");
break;
case 'k':case 'K':
puts("KOREA WORLD CUP");
break;
default:
putchar(s[i - 1]);
putchar('\n');
}
}
return 0;
}

7-10 约分最简分式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
int gcd(int a, int b) {
if (b) {
return gcd(b, a % b);
}
return a;
}
int main() {
int a, b;
scanf("%d/%d", &a , &b);
int x = gcd(a, b);
printf("%d/%d", a / x, b / x);
return 0;
}

7-11 猴子吃桃问题

1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
int main() {
int n, t = 1;
scanf("%d", &n);
while (--n) {
t = (t + 1) * 2;
}
printf("%d", t);
return 0;
}

7-12 特殊a串数列求和

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
int main() {
int a, n;
scanf("%d%d", &a, &n);
int s = 0, d = a;
while (n--) {
s += d;
d = d * 10 + a;
}
printf("s = %d", s);
return 0;
}

7-13 穷举法搬运砖块问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
int main() {
int n, f = 1;
scanf("%d", &n);
for (int i = 0;i <= n / 3;i++) {
int r = n - 3 * i;
for (int j = 0;j <= r / 2 && i + j <= n;j++) {
int rr = r - 2 * j;
if (i + j + rr*2 == n) {
printf("men=%d women=%d child=%d\n", i, j, rr*2);
f = 0;
}
}
}
if (f) {
puts("No solution!");
}
return 0;
}

7-14 数字金字塔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
int main() {
int t, n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 1;i <= n;i++) {
for (int j = 0;j < (n - i) * 2;j++) {
putchar(' ');
}
for (int j = 0;j < 1 + (i - 1) * 2;j++) {
printf("%d ", i);
}
putchar('\n');
}
}
return 0;
}

7-15 输出N以内的所有素数

使用素数筛法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<stdio.h>
short p[600];
int main() {
int t;
scanf("%d", &t);
p[1] = 1;
for (int i = 2;i <= t;i++) {
if (!p[i]) {
for (int j = i * 2;j <= t;j += i) {
p[j] = 1;
}
}
}
int cnt = 0;
for (int i = 1;i <= t;i++) {
if (!p[i]) {
printf("%5d", i);
cnt ++;
if (cnt % 8 == 0) {
putchar('\n');
}
}
}
return 0;
}

C理论A_编程练习4-数组

7-1 冒泡法排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
int main() {
int a[110], n, k;
scanf("%d%d", &n, &k);
for (int i = 0;i < n;i++) {
scanf("%d", &a[i]);
}
for (int i = 0;i < k;i++) {
for (int j = 0;j < n - 1 - i;j++) {
if (a[j] > a[j + 1]) {
a[j] ^= a[j + 1];
a[j + 1] ^= a[j];
a[j] ^= a[j + 1];
}
}
}
for (int i = 0;i < n;i++) {
printf(" %d" + !i, a[i]);
}
return 0;
}

7-2 评委打分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
int main() {
int n, m[11] = {};
for (int i = 1;i <= 10;i++) {
scanf("%d", &m[i]);
}
scanf("%d", &n);
while (n--) {
scanf("%d", &m[0]);
m[m[0]] += 10;
}
for (int i = 1;i <= 10;i++) {
printf(" %d" + (i == 1), m[i]);
}
return 0;
}

7-3 组合数的和

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
int main() {
int n, s = 0, c;
scanf("%d", &n);
for (int i = 0;i < n;i++) {
scanf("%d", &c);
s += c * 11 * (n - 1);
}
printf("%d", s);
return 0;
}

7-4 找不同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
struct {
int cnt;
int time;
} d[100100];
int main() {
int n, c, max = 0;
scanf("%d", &n);
for (int i = 1;i <= n;i++) {
scanf("%d", &c);
if (c > max) {
max = c;
}
d[c].cnt ++;
if (d[c].cnt == 1) {
d[c].time = i;
}
}
int m = -1, ts = 11451419;
for (int i = 1;i <= max;i++) {
if (d[i].cnt == 1) {
if (m == -1 || d[i].time < ts) {
m = i;
ts = d[i].time;
}
}
}
if (m == -1) {
puts("None");
} else {
printf("%d", m);
}
return 0;
}

7-5 利用二分查找搜寻所有待查找数据

有点申必。次数对不上,后来改成这b样踩过

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<stdio.h>
int a[50], n, cnt;
int binarySearch(int x) {
cnt = 0;
int l = 0, r = n, lastmid = -1;
while (l < r) {
cnt ++;
int mid = (l + r) / 2;
if (mid == lastmid) {
return -1;
}
if (a[mid] > x) {
r = mid;
} else if (a[mid] < x) {
l = mid;
} else {
return mid;
}
lastmid = mid;
}
if (a[l] == x) {
return l;
}
return -1;
}
int main() {
int x;
scanf("%d", &n);
for (int i = 0;i < n;i++) {
scanf("%d", &a[i]);
}
scanf("%d", &x);
int left = binarySearch(x);
if (left == -1) {
puts("not found");
} else {
printf("查找次数%d\n", cnt);
int right = left + 1;
while (left > 0 && a[left - 1] == x) {
left --;
}
while (right < n && a[right] == x) {
right ++;
}
for (int i = left;i < right;i++) {
printf("位置:%d\n", i);
}
}
return 0;
}

7-6 判断上三角矩阵

根本用不上数组,笑死

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
int main() {
int t, n, c;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
int flag = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &c);
if (j < i) {
flag = flag && !c;
}
}
}
puts(flag ? "YES" : "NO");
}
return 0;
}

7-7 方阵循环右移

算算公式就好,不用真的右移

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
int main() {
int m, n, a[20];
scanf("%d%d", &m, &n);
for (int i = 0;i < n;i++) {
for (int j = 0;j < n;j++) {
scanf("%d", &a[j]);
}
for (int j = 0;j < n;j++) {
printf("%d ", a[(n * (m / n + 1) + j - m) % n]);
}
putchar('\n');
}
return 0;
}

7-8 TicTacToe游戏–胜负判定

代码重用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
char m[10][10];
int check(char x) {
if (m[0][0] == x && m[1][1] == x && m[2][2] == x) {
return 1;
}
if (m[0][2] == x && m[1][1] == x && m[2][0] == x) {
return 1;
}
for (int i = 0;i < 3;i++) {
int x1 = 1, x2 = 1;
for (int j = 0;j < 3;j++) {
x1 = x1 && m[i][j] == x;
x2 = x2 && m[j][i] == x;
}
if (x1 | x2) {
return 1;
}
}
return 0;
}
int main() {
for (int i = 0;i < 3;i++) {
scanf("%s", m[i]);
}
if (check('O')) {
puts("O win!");
} else if (check('X')) {
puts("X win!");
} else {
puts("No one win!");
}
return 0;
}

7-9 2048游戏模拟(2)–向下移位合并

错了几次确实是我忘了2048怎么玩的。。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<stdio.h>
int m[5][5];
int x[5][5];
int main() {
for (int i = 0;i < 16;i++) {
scanf("%d", &m[i / 4][i % 4]);
}
for (int i = 0;i < 4;i++) {
for (int j = 2;j >= 0;j--) {
if (!m[j][i]) {
continue;
}
int k = j;
while (k < 3 && !m[k + 1][i]) k++;
if (k < 3 && m[k + 1][i] == m[j][i] && !x[k + 1][i]) {
m[k + 1][i] *= 2;
m[j][i] = 0;
x[k + 1][i] = 1;
} else if (k != j){
m[k][i] = m[j][i];
m[j][i] = 0;
}
}
}
for (int i = 0;i < 4;i++) {
for (int j = 0;j < 4;j++) {
printf(" %d" + !j, m[i][j]);
}
putchar('\n');
}
return 0;
}

7-10 二进制数据转换成十进制数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
#include<string.h>
int main() {
char str[32];
int x = 0, d = 1;
scanf("%s", str);
int len = strlen(str);
for (int i = len - 1;i >= 0;i--) {
x += (str[i] - '0') * d;
d *= 2;
}
printf("%d", x);
return 0;
}

7-11 英文单词排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<stdio.h>
#include<string.h>
struct S {
char text[20];
int len;
} s[25];
int pos[25];
int cmp(int i, int j) {
int r = s[pos[i]].len - s[pos[j]].len;
if (r == 0) {
r = pos[i] - pos[j];
}
return r;
}
int main() {
int n = 0;
while (1) {
scanf("%s", s[n].text);
if (!strcmp(s[n].text, "#")) {
break;
}
s[n].len = strlen(s[n].text);
pos[n] = n;
n ++;
}
for (int i = 0;i < n;i++) {
for (int j = 0;j < n - i - 1;j++) {
if (cmp(j, j + 1) > 0) {
pos[j] ^= pos[j + 1];
pos[j + 1] ^= pos[j];
pos[j] ^= pos[j + 1];
}
}
}
for (int i = 0;i < n;i++) {
printf("%s ", s[pos[i]].text);
}
return 0;
}

7-12 统计单词的长度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
#include<string.h>
char buf[1024];
int main() {
int f = 1;
while (scanf("%s", buf) != -1) {
printf("%d ", strlen(buf));
f = 0;
}
if (f) {
printf("0 ");
}
return 0;
}

7-13 来验证我们的身份证吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
const int weights[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
const char checksum[] = "1 0 X 9 8 7 6 5 4 3 2 ";
int main() {
int n, f = 1;
char str[20];
scanf("%d", &n);
while (n--) {
scanf("%s", str);
int sum = 0;
for (int i = 0;i < 17;i++) {
sum += (str[i] - '0') * weights[i];
sum %= 11;
}
if (checksum[sum * 2] != str[17]) {
puts(str);
f = 0;
}
}
if (f) {
puts("全部正确!");
}
return 0;
}

7-14 将整数按三位分节

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
#include<string.h>
int main() {
char str[32];
scanf("%s", str);
int len = strlen(str);
int skip = len % 3;
for (int i = 0;i < len;i++) {
if (i >= skip && i != 0 && (i - skip) % 3 == 0) {
putchar(',');
}
putchar(str[i]);
}
return 0;
}

7-15 求完全对称日

顺便喷一句,一开始测试数据完全没有考虑闰年。我的内心是操蛋的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>
const int ds[] = {
0, 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31
};
int main() {
int m, n;
scanf("%d%d", &m, &n);
int flag = 1;
for (int i = m;i <= n;i++) {
int y = i;
int m = y % 100;
m = (m % 10) * 10 + m / 10;
int d = y / 100;
d = (d % 10) * 10 + d / 10;
if (m >= 1 && m <= 12 && d > 0) {
int d2 = (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)) ? 29 : ds[m];
if (d <= d2) {
printf("%04d.%02d.%02d\n", y, m, d);
flag = 0;
}
}
}
if (flag) {
puts("None");
}
return 0;
}

C理论A_编程练习5-函数与指针

6-1 求实数和的函数

1
2
3
4
5
6
7
float sum(float data[], int n) {
float s = 0;
for (int i = 0;i < n;i++) {
s += data[i];
}
return s;
}

6-2 求解一元二次方程实根的函数

1
2
3
4
5
6
7
8
9
10
11
12
int rootOfEquation(double a, double b, double c, double *x1, double *x2) {
double delta = b * b - 4 * a * c;
if (delta < 0) {
return 0;
} else {
double s1 = (-b + sqrt(delta)) / (2 * a);
double s2 = (-b - sqrt(delta)) / (2 * a);
*x1 = s1 > s2 ? s1 : s2;
*x2 = s1 > s2 ? s2 : s1;
return delta == 0 ? 1 : 2;
}
}

6-3 求集合数据的均方差

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double Avg ( int N, int  data[] ) {
double s = 0;
for (int i = 0;i < N;i++) {
s += data[i];
}
return s / N;
}
double StdDev( int N, int data[] ) {
double avg = Avg(N, data);
double s = 0;
for (int i = 0;i < N;i++) {
s += (data[i] - avg) * (data[i] - avg);
}
return sqrt(s / N);
}

6-4 计算Fibonacci数列每一项时所需的递归调用次数

1
2
3
4
5
6
7
8
9
int count;

long Fib(int a) {
count ++;
if (a == 1 || a == 2) {
return 1;
}
return Fib(a - 1) + Fib(a - 2);
}

6-5 字符串加密

1
2
3
4
5
6
7
8
9
10
11
12
13
void cryptograp(char ch[],int n) {
for (int i = 0;i < n;i++) {
char salted = ch[i] + 5;
if (ch[i] >= 'a' && ch[i] <= 'z' && salted > 'z') {
salted = 'a' + salted - 'z' - 1;
} else if (ch[i] >= 'A' && ch[i] <= 'Z' && salted > 'Z') {
salted = 'A' + salted - 'Z' - 1;
} else if(!((ch[i] >= 'A' && ch[i] <= 'Z') || (ch[i] >= 'a' && ch[i] <= 'z'))){
continue;
}
ch[i] = salted;
}
}

6-6 万年历显示函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void ShowDate(int y, int m) {
puts("***********************************");
puts(" Sun Mon Tue Wen The Fri Sta");
int days = GetDaysofMonth(y, m);
int loc = GetFirstDayInTable(y, m);
for (int i = 1;i <= days;i++) {
if (i == 1) {
for (int j = 0;j < loc;j++) {
printf(" ");
}
}
printf("%5d", i);
if ((loc + i) % 7 == 0) {
putchar('\n');
}
}
if ((loc + days) % 7 != 0) {
putchar('\n');
}
puts("***********************************");
}

C理论A_编程练习6-结构体与共用体

6-1 为通讯录排序

注:本题为函数题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct Records {
char name[16], birth[10], tele[25];
};
#include<string.h>
int cmp(struct Records *a, struct Records *b) {
return strcmp(a->name, b->name);
}
char buf[50];
void strswp(char *a, char *b) {
strcpy(buf, a);
strcpy(a, b);
strcpy(b, buf);
}
void sswap(struct Records *a, struct Records *b) {
strswp(a->name, b->name);
strswp(a->birth, b->birth);
strswp(a->tele, b->tele);
}
void my_sort(struct Records *addr, int N) {
for (int i = 0;i < N;i++) {
for (int j = 0;j < N - i - 1;j++) {
if (cmp(addr + j, addr + j + 1) > 0) {
sswap(addr + j + 1, addr + j);
}
}
}
}

7-1 查找书籍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
struct Book {
double price;
char name[50];
} books[50];
int main() {
int n;
scanf("%d", &n);
int highIdx = 0, lowIdx = 0;
for (int i = 0;i < n;i++) {
while (getchar() != '\n');
gets(books[i].name);
scanf("%lf", &books[i].price);
if (books[i].price <= books[lowIdx].price) {
lowIdx = i;
}
if (books[i].price >= books[highIdx].price) {
highIdx = i;
}
}
printf("%.2lf, %s\n%.2lf, %s", books[highIdx].price, books[highIdx].name, books[lowIdx].price,
books[lowIdx].name);
return 0;
}

7-2 一帮一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
struct Student {
int gender;
int paired;
char name[10];
} stu[100];
int main() {
int n;
scanf("%d", &n);
for (int i = 0;i < n;i++) {
scanf("%d%s", &stu[i].gender, stu[i].name);
}
for (int i = 0;i < n;i++) {
if (!stu[i].paired) {
stu[i].paired = 1;
for (int j = n - 1;j >= 0;j--) {
if (!stu[j].paired && stu[j].gender != stu[i].gender) {
stu[j].paired = 1;
printf("%s %s\n", stu[i].name, stu[j].name);
break;
}
}
}
}
return 0;
}

7-3 计算职工工资

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
char name[256];
double a, b, c;
for (int i = 0;i < n;i++) {
scanf("%s%lf%lf%lf", name, &a, &b, &c);
printf("%s %.2lf\n", name, a + b - c);
}
return 0;
}

实验1 顺序结构

7-1 逆序的三位数

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
int main() {
int a, t = 0;
scanf("%d", &a);
while (a) {
t = t * 10 + a % 10;
a /= 10;
}
printf("%d", t);
return 0;
}

7-2 求整数均值

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#define ll long long
int main() {
int a;
ll sum = 0;
for (int i = 0;i < 4;i++) {
scanf("%d", &a);
sum += a;
}
printf("Sum = %lld; Average = %.1lf", sum, sum / 4.0);
return 0;
}

7-3 日期格式化

1
2
3
4
5
6
7
#include<stdio.h>
int main() {
int m, d, y;
scanf("%d-%d-%d", &m, &d, &y);
printf("%04d-%02d-%02d", y, m ,d);
return 0;
}

7-4 混合类型数据格式化输入

1
2
3
4
5
6
7
8
9
#include<stdio.h>
int main() {
float v1, v4;
int v2;
char v3;
scanf("%f %d %c %f", &v1, &v2, &v3, &v4);
printf("%c %d %.2f %.2f", v3, v2, v1, v4);
return 0;
}

7-5 然后是几点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
int main() {
int a,b;
scanf("%d%d", &a, &b);
int m = (a / 100) * 60 + a % 100;
m += b;
while (m < 0) {
m += 60 * 24;
}
int h = m / 60;
m %= 60;
h %= 24;
printf("%d%02d", h, m);
return 0;
}

7-6 计算存款利息

1
2
3
4
5
6
7
8
9
#include<stdio.h>
#include<math.h>
int main() {
double m, y, r;
scanf("%lf%lf%lf", &m, &y, &r);
double v = m * pow(1 + r, y) - m;
printf("interest = %.2lf", v);
return 0;
}

7-7 苹果装盘

1
2
3
4
5
6
7
#include<stdio.h>
int main() {
int a;
scanf("%d", &a);
printf("%d", a / 3 + (a % 3 != 0));
return 0;
}

7-8 计算并联电阻的阻值

1
2
3
4
5
6
7
#include<stdio.h>
int main() {
double r1, r2;
scanf("%lf%lf", &r1, &r2);
printf("%.2lf", 1.0 / (1.0/r1 + 1.0/r2));
return 0;
}