#include<stdio.h> char op[] = {'+', '-', '*', '/'}; typedefint(*expr)(int, int); intplus(int a, int b) {return a + b;} intminus(int a, int b) {return a - b;} intmult(int a, int b) {return a * b;} intdiv(int a, int b) {return a / b;} expr funcs[] = {plus, minus, mult, div}; intmain() { 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)); } return0; }
#include<stdio.h> int t[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; intgetDay(int y, int m) { if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))) { return29; } return t[m]; } intmain() { 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); return0; }
7-6 计算工资
1 2 3 4 5 6 7 8 9
#include<stdio.h> #define max(a, b) (a > b ? a : b) intmain() { 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)); return0; }
#include<stdio.h> intmain() { 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"); } } return0; }
7-4 求分数序列前N项和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<stdio.h> intmain() { 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); return0; }
7-5 求给定精度的简单交错序列部分和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<stdio.h> #define ll long long intmain() { 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); return0; }
7-6 求cosx的近似值
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<stdio.h> intmain() { 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); return0; }
#include<stdio.h> char s[1024]; intmain() { 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'); } } return0; }
7-10 约分最简分式
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<stdio.h> intgcd(int a, int b) { if (b) { return gcd(b, a % b); } return a; } intmain() { int a, b; scanf("%d/%d", &a , &b); int x = gcd(a, b); printf("%d/%d", a / x, b / x); return0; }
7-11 猴子吃桃问题
1 2 3 4 5 6 7 8 9 10
#include<stdio.h> intmain() { int n, t = 1; scanf("%d", &n); while (--n) { t = (t + 1) * 2; } printf("%d", t); return0; }
7-12 特殊a串数列求和
1 2 3 4 5 6 7 8 9 10 11 12
#include<stdio.h> intmain() { 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); return0; }
7-13 穷举法搬运砖块问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<stdio.h> intmain() { 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!"); } return0; }
7-14 数字金字塔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<stdio.h> intmain() { 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'); } } return0; }
#include<stdio.h> intmain() { 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]); } return0; }
7-2 评委打分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<stdio.h> intmain() { 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]); } return0; }
7-3 组合数的和
1 2 3 4 5 6 7 8 9 10 11
#include<stdio.h> intmain() { 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); return0; }
#include<stdio.h> int m[5][5]; int x[5][5]; intmain() { 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; } elseif (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'); } return0; }
7-10 二进制数据转换成十进制数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<stdio.h> #include<string.h> intmain() { 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); return0; }
#include<stdio.h> constint weights[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; constchar checksum[] = "1 0 X 9 8 7 6 5 4 3 2 "; intmain() { 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("全部正确!"); } return0; }
7-14 将整数按三位分节
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<stdio.h> #include<string.h> intmain() { 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]); } return0; }
#include<stdio.h> constint ds[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; intmain() { 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"); } return0; }
C理论A_编程练习5-函数与指针
6-1 求实数和的函数
1 2 3 4 5 6 7
floatsum(float data[], int n) { float s = 0; for (int i = 0;i < n;i++) { s += data[i]; } return s; }
doubleAvg( int N, int data[] ) { double s = 0; for (int i = 0;i < N;i++) { s += data[i]; } return s / N; } doubleStdDev( 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); } returnsqrt(s / N); }
6-4 计算Fibonacci数列每一项时所需的递归调用次数
1 2 3 4 5 6 7 8 9
int count;
longFib(int a) { count ++; if (a == 1 || a == 2) { return1; } return Fib(a - 1) + Fib(a - 2); }
voidShowDate(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("***********************************"); }
#include<stdio.h> structStudent { int gender; int paired; char name[10]; } stu[100]; intmain() { 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; } } } } return0; }
7-3 计算职工工资
1 2 3 4 5 6 7 8 9 10 11 12
#include<stdio.h> intmain() { 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); } return0; }
实验1 顺序结构
7-1 逆序的三位数
1 2 3 4 5 6 7 8 9 10 11
#include<stdio.h> intmain() { int a, t = 0; scanf("%d", &a); while (a) { t = t * 10 + a % 10; a /= 10; } printf("%d", t); return0; }
7-2 求整数均值
1 2 3 4 5 6 7 8 9 10 11 12
#include<stdio.h> #define ll long long intmain() { 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); return0; }
7-3 日期格式化
1 2 3 4 5 6 7
#include<stdio.h> intmain() { int m, d, y; scanf("%d-%d-%d", &m, &d, &y); printf("%04d-%02d-%02d", y, m ,d); return0; }
#include<stdio.h> intmain() { 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); return0; }
7-6 计算存款利息
1 2 3 4 5 6 7 8 9
#include<stdio.h> #include<math.h> intmain() { double m, y, r; scanf("%lf%lf%lf", &m, &y, &r); double v = m * pow(1 + r, y) - m; printf("interest = %.2lf", v); return0; }
7-7 苹果装盘
1 2 3 4 5 6 7
#include<stdio.h> intmain() { int a; scanf("%d", &a); printf("%d", a / 3 + (a % 3 != 0)); return0; }