这个仅作为练习扩展使用,贴出代码,作为以后复习,不直接写结果。因为要自己推出
#include <stdio.h> int main(void) { int score; printf("pls input a score: "); scanf("%d", &score); if (score < 0 || score > 100) { printf("wrong. score from 0 to 100.\n"); } else if (60 < score && score <= 100) { if (score < 70) { printf("rank C!\n"); } else if (70 <= score && score < 85) { printf("rank B!\n"); } else printf("rank A!\n"); } else { if (score < 30) printf("rank E!\n"); else printf("rank D!\n"); } return 0; }
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h> int main(void) { int i, j; i = 1; while (i <= 9) { j = 1; while (j <= i) { printf("%d x %d = %2d ", i, j, i * j); j++; } i++; putchar('\n'); } printf("---------------------\n"); i = 1; do { j = 1; do { printf("%d x %d = %2d ", i, j, i * j); j++; } while (j <= i); putchar('\n'); i++; } while (i <= 9); printf("---------------------\n"); i = 1; labeli: j = 1; labelj: printf("%d x %d = %2d ", i, j, i * j); j++; if (j <= i) goto labelj; putchar('\n'); i++; if (i <= 9) goto labeli; return 0; }
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h> int main(void) { int i; double sum = 0; int flag = 1; for (i = 1; i <= 10; i++) { /* *if (i % 2 != 0) * sum += (double)1 / i; *else * sum -= 1 / (double)i; */ sum += flag * 1 / (double)i; flag *= -1; } printf("sum = %lf\n", sum); return 0; }
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h> int main(void) { unsigned n; printf("pls input n: "); scanf("%d", &n); int i; for (i = 31; i >= 0; i--) { if (n & (1 << i)) putchar('1'); else putchar('0'); if (i % 4 == 0) putchar(' '); } putchar('\n'); return 0; }
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h> int main(void) { int num; int divider; int flag; int count = 0; for (num = 2; num <= 1000; num++) { flag = 0; for (divider = 2; divider <= num / 2; divider++) { if (num % divider == 0) { flag = 1; break; } } if (!flag) { count++; printf("%d 是素数.\n", num); } } printf("2 - 1000 一共有%d个素数.\n", count); return 0; }
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h> int main(void) { int r; int guess; int ret; srand(time(NULL)); r = rand() % 100; while (1) { retry: printf("input a num, to guess the password: "); ret = scanf("%d", &guess); while (getchar() != '\n') ; if (ret == 0) { printf("姿势不对, 重新输入.\n"); goto retry; } if (guess > r) printf("你输入的数大了.\n"); else if (guess < r) printf("你输入的数小了.\n"); else { printf("恭喜你, 中奖了.\n"); break; } } return 0; }
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h> int main(void) { char ch8 = '0', ch7 = '0', ch6 = '0', ch5 = '0', ch4 = '0', ch3 = '0', ch2 = '0', ch1 = '0'; unsigned n; unsigned q, r; printf("pls input n: "); scanf("%u", &n); q = n; char ch; int cnt = 0; while (q > 0) { r = q % 16; q /= 16; if (r > 9) ch = (r - 10) + 'a'; else ch = r + '0'; switch (cnt) { case 0: ch1 = ch; break; case 1: ch2 = ch; break; case 2: ch3 = ch; break; case 3: ch4 = ch; break; case 4: ch5 = ch; break; case 5: ch6 = ch; break; case 6: ch7 = ch; break; case 7: ch8 = ch; break; default: break; } cnt++; } printf("transformed: "); printf("0x%c%c%c%c %c%c%c%c\n", ch8, ch7, ch6, ch5, ch4, ch3, ch2, ch1); return 0; }
这个几个中就转换二进制有点意思,一个判断位为1还是0,一个一个的来共32次,转16进制就是不停的用除数除16得到每个阶段的余数,从低到高