当前位置:   article > 正文

2023西工大NOJ (C语言版) 完结!!!

2023西工大noj

前言

代码已同步至 gitee:2023NOJ(C语言版)

题目过于垃圾和睿智,做这种题简直是浪费生命,甚至会让代码水平剧烈下降,也就81-90题值得做一下,有这功夫不如多打会儿游戏。
贴出 100 题中全部 AC 代码,并不保证代码正确性、可读性、简洁性、最佳性,只能保证AC;目前 4 道题 WA,还有 5 道题使用 CPP。具体情况如下:

  • 41-50:飞机起飞速度(WA);
  • 51-60:字符串替换(WA);
  • 61-70:GPS通信协议(CPP);
  • 71-80:卫星定位(WA),日出日落时间(WA),火箭发射模拟(CPP),晶体结构(CPP),原子计数(CPP);
  • 81-90:危险的组合(CPP)。

考试模板

作者考试时会携带的资料如下:

  1. // 字符串操作函数(常用)
  2. // 为避免内存泄漏和野指针问题, 通常只能对开在栈上字符串使用, 而不能对开在堆上的字符串使用
  3. # include <string.h>
  4. // 失败时均返回NULL
  5. void *memset(void *str, int c, size_t n); // 复制字符c到str的前n个字符.
  6. // 使用memset对数组初始化时, 字符c只能为0或-1或0x3f.
  7. void *memcpy(void *dest, const void *src, size_t n); // 从src复制n个字符到dest, 一般不用于src和dest内存重叠的情况.
  8. void *memmove(void *dest, const void *src, size_t n); // 从src复制n个字符到dest, src和dest内存重叠时推荐使用.
  9. size_t strlen(const char *str); // 返回str长度, 即头指针到'\0'的距离(但不包含'\0').
  10. char *strcat(char *dest, const char *src); // 将src追加到dest结尾.
  11. char *strchr(const char *str, int c); // 返回str中第一次出现字符c的位置.
  12. char *strstr(const char *haystack, const char *needle); // 返回在haystack中第一次出现needle的起始位置.
  13. size_t strspn(const char *str1, const char *str2); // str1中第一个不在str2出现的字符的下标
  14. char *strtok(char *str, const char *delim); // 以delim为分隔, 分解str.
  1. // 字符串操作函数(常用)
  2. // 为避免内存泄漏和野指针问题, 通常只能对开在栈上字符串使用, 而不能对开在堆上的字符串使用
  3. # include <string.h>
  4. // 失败时均返回NULL
  5. void *memset(void *str, int c, size_t n); // 复制字符c到str的前n个字符.
  6. // 使用memset对数组初始化时, 字符c只能为0或-1或0x3f.
  7. void *memcpy(void *dest, const void *src, size_t n); // 从src复制n个字符到dest, 一般不用于src和dest内存重叠的情况.
  8. void *memmove(void *dest, const void *src, size_t n); // 从src复制n个字符到dest, src和dest内存重叠时推荐使用.
  9. size_t strlen(const char *str); // 返回str长度, 即头指针到'\0'的距离(但不包含'\0').
  10. char *strcat(char *dest, const char *src); // 将src追加到dest结尾.
  11. char *strchr(const char *str, int c); // 返回str中第一次出现字符c的位置.
  12. char *strstr(const char *haystack, const char *needle); // 返回在haystack中第一次出现needle的起始位置.
  13. size_t strspn(const char *str1, const char *str2); // str1中第一个不在str2出现的字符的下标
  14. char *strtok(char *str, const char *delim); // 以delim为分隔, 分解str.
  1. // 指针数组模板
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. typedef struct {
  5. char id[64];
  6. int a;
  7. int b;
  8. } PointerArray;
  9. int main(){
  10. int n; scanf("%d", &n);
  11. PointerArray *arr[n];
  12. for (int i = 0; i < n; ++i) {
  13. arr[i] = (PointerArray*) calloc (sizeof(PointerArray), 1);
  14. scanf("%s %d %d", arr[i]->id, &arr[i]->a, &arr[i]->b);
  15. }
  16. for (int i = 0; i < n; ++i)
  17. printf("%s %d %d", arr[i]->id, arr[i]->a, arr[i]->b);
  18. return 0;
  19. }
  1. // 二维数组模板
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int** init(int n) {
  5. int** matrix = (int**)malloc(sizeof(int*) * (n + 1));
  6. for(int i = 0; i <= n; i++) {
  7. matrix[i] = (int*)malloc(sizeof(int) * (n + 1));
  8. }
  9. return matrix;
  10. }
  11. int op(int **matrix, int n);
  12. int main() {
  13. int n;
  14. scanf("%d", &n);
  15. int **matrix = init(n);
  16. for (int i = 1; i <= n; ++i) {
  17. for (int j = 1; j <= n; ++j) {
  18. scanf("%d", &matrix[i][j]);
  19. }
  20. }
  21. printf("%d", op(matrix, n));
  22. return 0;
  23. }
  1. // 快速排序模板: 从小到大
  2. void quickSort(int arr[], int left, int right) {
  3. if (left >= right) return;
  4. int flag = arr[(left + right) / 2];
  5. int head = left - 1, tail = right + 1;
  6. while (head < tail) {
  7. do head++; while (arr[head] < flag); // 若从大到小需修改本行
  8. do tail--; while (arr[tail] > flag); // 若从大到小需修改本行
  9. if (head < tail) {
  10. int tmp = arr[head];
  11. arr[head] = arr[tail], arr[tail] = tmp;
  12. }
  13. }
  14. quickSort(arr, left, tail);
  15. quickSort(arr, tail + 1, right);
  16. }
  1. // 冒泡排序模板: 从小到大
  2. #include <stdbool.h>
  3. void bubble(int arr[], int n) {
  4. for (int i = 0; i < n - 1; ++i) {
  5. bool flag = false;
  6. for (int j = 0; j < n - i - 1; ++j) {
  7. if (arr[j] > arr[j + 1]) { // 若从大到小需修改本行
  8. int tmp = arr[j];
  9. arr[j] = arr[j + 1], arr[j + 1] = tmp, flag = true;
  10. }
  11. }
  12. if (!flag) break;
  13. }
  14. }
  1. // 二分查找模板
  2. long long left = 1, right = 1e19, mid, ans;
  3. while(left <= right) {
  4. mid = ((right - left) >> 1) + left;
  5. if (isLeft(mid)) right = mid - 1, ans = mid;
  6. else left = mid + 1;
  7. }
  1. // 文件读写模板
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main(){
  5. int n; scanf("%d",&n);
  6. FILE *file = fopen("rr.dat", "w");
  7. for(int i=1;i<=n;i++)
  8. fprintf(file, "%d\n", i);
  9. fclose(file);
  10. file = fopen("rr.dat", "r");
  11. while (fscanf(file, "%d", &ans) == 1)
  12. printf("%d ", ans);
  13. fclose(file);
  14. }

1-10

Hello World

  1. #include<stdio.h>
  2. int main(){
  3. printf("Hello World");
  4. return 0;
  5. }

A+B

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

数据类型大小及范围

  1. #include <stdio.h>
  2. #include <limits.h>
  3. int main(){
  4. int id = 0;
  5. scanf("%d",&id);
  6. switch (id) {
  7. case 1: // char
  8. printf("%llu,%d,%d\n",sizeof(char),CHAR_MIN,CHAR_MAX);
  9. break;
  10. case 2: // unsigned char
  11. printf("%llu,%u,%u\n",sizeof(unsigned char),0,UCHAR_MAX);
  12. break;
  13. case 3: // short
  14. printf("%llu,%hd,%hd\n",sizeof(short),SHRT_MIN,SHRT_MAX);
  15. break;
  16. case 4: // unsigned short
  17. printf("%llu,%hu,%hu\n",sizeof(unsigned short),0,USHRT_MAX);
  18. break;
  19. case 5: // int
  20. printf("%llu,%d,%d\n",sizeof(int),INT_MIN,INT_MAX);
  21. break;
  22. case 6: // unsigned int
  23. printf("%llu,%u,%u\n",sizeof(unsigned int),0,UINT_MAX);
  24. break;
  25. case 7: //long
  26. printf("%llu,%ld,%ld\n",sizeof(int),LONG_MIN,LONG_MAX);
  27. break;
  28. case 8: // unsigned long
  29. printf("%llu,%u,%lu\n",sizeof(unsigned long),0,ULONG_MAX);
  30. break;
  31. case 9: // long long
  32. printf("%llu,%lld,%lld\n",sizeof(int),LLONG_MIN,LLONG_MAX);
  33. break;
  34. case 10: // unsigned long long
  35. printf("%llu,%u,%llu\n",sizeof(unsigned long long),0,ULLONG_MAX);
  36. break;
  37. }
  38. return 0;
  39. }

平均值

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

进制转换

  1. #include <stdio.h>
  2. int main(){
  3. unsigned int a = 0;
  4. scanf("%d",&a);
  5. printf("%X,%o",a,a);
  6. }

浮点数输出

  1. #include <stdio.h>
  2. int main(){
  3. double a = 0.0f;
  4. scanf("%lf",&a);
  5. printf("%.6lf,%.2lf,%.8lf",a,a,a);
  6. return 0;
  7. }

动态宽度输出

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

计算地球上两点之间的距离

  1. #include <stdio.h>
  2. #include <math.h>
  3. #define RADIUS 6371.000000
  4. #define PI 3.1415926
  5. int main(){
  6. double phi1, phi2, lambda1, lambda2, distance;
  7. scanf("%lf %lf",&phi1,&lambda1);
  8. scanf("%lf %lf",&phi2,&lambda2);
  9. phi1 = phi1*PI/180;
  10. phi2 = phi2*PI/180;
  11. lambda1 = lambda1*PI/180;
  12. lambda2 = lambda2*PI/180;
  13. double havRatio = (1-cos(phi2-phi1))/2+cos(phi1)*cos(phi2)*(1-cos(lambda2-lambda1))/2;
  14. distance = asin(sqrt(havRatio))*2*RADIUS;
  15. printf("%.4lfkm",distance);
  16. return 0;
  17. }

风寒指数

  1. #include <stdio.h>
  2. #include <math.h>
  3. int main(){
  4. double v,T;
  5. scanf("%lf %lf",&v,&T);
  6. int chill = 13.12f+0.6215f*T-11.37f*pow(v,0.16f)+0.3965f*T*pow(v,0.16f)+0.5f;
  7. printf("%d",chill);
  8. return 0;
  9. }

颜色模型转换

  1. #include <stdio.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. double findmax(double a, double b, double c) {
  5. return a >= b ? (a >= c ? a : c) : (b >= c ? b : c);
  6. }
  7. double findmin(double a, double b, double c) {
  8. return a <= b ? (a <= c ? a : c) : (b <= c ? b : c);
  9. }
  10. int main()
  11. {
  12. int R, G, B;
  13. double r, g, b;
  14. double max, min;
  15. double H, S, V;
  16. scanf("%d %d %d",&R,&G,&B);
  17. r = (double)((R > 0 ? R : 0) / 255.0f);
  18. g = (double)((G > 0 ? G : 0) / 255.0f);
  19. b = (double)((B > 0 ? B : 0) / 255.0f);
  20. max = findmax(r, g, b);
  21. min = findmin(r, g, b);
  22. V = max;
  23. if (max < 1e-9) {
  24. S = 0.0f;
  25. }
  26. else {
  27. S = (max - min) / max;
  28. }
  29. if (max - min < 1e-9) {
  30. H = 0.0f;
  31. }
  32. else {
  33. if (max == r) {
  34. H = 60.0f * (g - b) / (max - min);
  35. }
  36. else if (max == g) {
  37. H = 60.0f * (2.0f + (b - r) / (max - min));
  38. }
  39. else if (max == b) {
  40. H = 60.0f * (4.0f + (r - g) / (max - min));
  41. }
  42. else {
  43. return -1;
  44. }
  45. if (H < 1e-9) {
  46. H = H + 360.0f;
  47. }
  48. }
  49. printf("%.4lf,%.4lf%%,%.4lf%%", H, S * 100, V * 100);
  50. return 0;
  51. }

11-20

乘数模

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

方阵

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

分数的加、减、乘、除法

  1. #include<stdio.h>
  2. int gcd(int a,int b){
  3. if (b == 0) return a;
  4. else return gcd(b,a%b);
  5. }
  6. int main(){
  7. int aTop, aBot, bTop, bBot;
  8. scanf("%d",&aTop);
  9. getchar();
  10. scanf("%d",&aBot);
  11. getchar();
  12. scanf("%d",&bTop);
  13. getchar();
  14. scanf("%d",&bBot);
  15. getchar();
  16. int addTop, addBot, subTop, subBot, multTop, multBot, divTop, divBot;
  17. int addGcd, subGcd, multGcd, divGcd;
  18. addTop = aTop*bBot+aBot*bTop;
  19. addBot = aBot*bBot;
  20. addGcd = gcd(addTop,addBot);
  21. addTop /= addGcd;
  22. addBot /= addGcd;
  23. subTop = aTop*bBot-aBot*bTop;
  24. subBot = aBot*bBot;
  25. subGcd = gcd(subTop,subBot);
  26. subTop /= subGcd;
  27. subBot /= subGcd;
  28. multTop = aTop*bTop;
  29. multBot = aBot*bBot;
  30. multGcd = gcd(multTop,multBot);
  31. multTop /= multGcd;
  32. multBot /= multGcd;
  33. divTop = aTop*bBot;
  34. divBot = aBot*bTop;
  35. divGcd = gcd(divTop,divBot);
  36. divTop /= divGcd;
  37. divBot /= divGcd;
  38. printf("(%d/%d)+(%d/%d)=%d/%d\n",aTop,aBot,bTop,bBot,addTop,addBot);
  39. printf("(%d/%d)-(%d/%d)=%d/%d\n",aTop,aBot,bTop,bBot,subTop,subBot);
  40. printf("(%d/%d)*(%d/%d)=%d/%d\n",aTop,aBot,bTop,bBot,multTop,multBot);
  41. printf("(%d/%d)/(%d/%d)=%d/%d\n",aTop,aBot,bTop,bBot,divTop,divBot);
  42. return 0;
  43. }

操作数

  1. #include<stdio.h>
  2. int sumDig(int n){
  3. int sum = 0;
  4. while(n){
  5. sum += n%10;
  6. n /= 10;
  7. }
  8. return sum;
  9. }
  10. int main(){
  11. int n,cnt=0;
  12. scanf("%d",&n);
  13. while(n){
  14. n -= sumDig(n);
  15. ++cnt;
  16. }
  17. printf("%d",cnt);
  18. return 0;
  19. }

组合数

  1. #include<stdio.h>
  2. int main(){
  3. int cnt = 0, n, m;
  4. scanf("%d",&n);
  5. for(int a = 0; a <= 9; ++a){
  6. for(int b = 0; b <= 9; ++b){
  7. for(int c = 0; c <= 9; ++c){
  8. for(int d = 0; d <= 9; ++d){
  9. m = a+b+c+d;
  10. if (m == n){
  11. ++cnt;
  12. }
  13. }
  14. }
  15. }
  16. }
  17. printf("%d",cnt);
  18. return 0;
  19. }

比率

  1. #include<stdio.h>
  2. #include<math.h>
  3. int gcd(int a,int b){
  4. if (b == 0) return a;
  5. else return gcd(b,a%b);
  6. }
  7. int cntDig(double x){
  8. int cnt = 0;
  9. while(x != floor(x)){
  10. ++cnt;
  11. x *= 10;
  12. }
  13. return cnt;
  14. }
  15. int main(){
  16. double x;
  17. scanf("%lf",&x);
  18. int cnt = cntDig(x), n, m, g;
  19. m = pow(10,cnt);
  20. n = (int)(x*m);
  21. g = gcd(m,n);
  22. m /= g;
  23. n /= g;
  24. printf("%d/%d",n,m);
  25. return 0;
  26. }

级数和

  1. #include<stdio.h>
  2. #include<math.h>
  3. double decimal (int n){
  4. double m = (double)n+1;
  5. while(floor(m)){
  6. m /= 10;
  7. }
  8. return m+n;
  9. }
  10. int main(){
  11. int n;
  12. scanf("%d",&n);
  13. double sum = 0.0f, term;
  14. for (int i = 1; i < n; ++i) {
  15. term = decimal(i);
  16. sum += term;
  17. printf("%g+",term);
  18. }
  19. term = decimal(n);
  20. sum += term;
  21. printf("%g=%g",term,sum);
  22. return 0;
  23. }

对称数

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

幂数模

  1. #include<stdio.h>
  2. typedef unsigned long long uint64;
  3. int fastPowerMod (uint64 t, uint64 e, uint64 m){
  4. uint64 r = 1;
  5. while (e){
  6. if (e&1){
  7. r = (1LL*r*t)%m;
  8. }
  9. t = (1LL*t*t)%m;
  10. e >>= 1;
  11. }
  12. return r;
  13. }
  14. int main(){
  15. uint64 a,b,m,r;
  16. scanf("%llu %llu %llu",&a,&b,&m);
  17. r = fastPowerMod(a,b,m);
  18. printf("%llu",r);
  19. return 0;
  20. }

倍数和

  1. #include <stdio.h>
  2. int main(){
  3. unsigned int t = 0;
  4. unsigned int arrn[100000];
  5. scanf("%u", &t);
  6. for(unsigned int i = 0; i < t; i++){
  7. scanf("%u", &arrn[i]);
  8. }
  9. for(unsigned int j = 0; j < t; j++){
  10. unsigned int res = 0;
  11. for(unsigned int x = 1; x < arrn[j]; x++){
  12. if(x % 3 == 0 || x % 5 == 0){
  13. res += x;
  14. }
  15. }
  16. printf("%u\n", res);
  17. }
  18. return 0;
  19. }

21-30

余数和

  1. #include <stdio.h>
  2. int main() {
  3. unsigned int sum = 0, n, k;
  4. scanf("%u %u",&n,&k);
  5. for (unsigned int i = 1; i <= n; ++i) {
  6. sum += k%i;
  7. }
  8. printf("%u",sum);
  9. return 0;
  10. }

最大数字

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. bool isNonDecr(unsigned int n){
  4. unsigned int left;
  5. while(n){
  6. left = (n/10)%10;
  7. if (left > (n%10)){
  8. return false;
  9. }
  10. n /= 10;
  11. }
  12. return true;
  13. }
  14. int main() {
  15. unsigned int n, res;
  16. scanf("%u",&n);
  17. while(!isNonDecr(n)){
  18. --n;
  19. }
  20. printf("%u",n);
  21. return 0;
  22. }

倒水(原 AC 后 TE 现又 AC)

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define INF 0x3f3f3f3f
  4. const int maxSize = 10000;
  5. int distTable[10000][10000];
  6. int queue[2][10000] = {0};
  7. int bfs(int n, int m, int d){
  8. int head = 0, tail = 1;
  9. memset(distTable, 0x3f, sizeof(distTable));
  10. distTable[0][0] = 0;
  11. while(tail != head){
  12. int a = queue[0][head], b = queue[1][head];
  13. if (a == d || b == d) {
  14. return distTable[a][b];
  15. }
  16. ++head;
  17. if(head == maxSize-1) {
  18. head = 0;
  19. }
  20. // full
  21. if(distTable[a][m] == INF) {
  22. distTable[a][m] = distTable[a][b]+1;
  23. ++tail;
  24. queue[0][tail] = a;
  25. queue[1][tail] = m;
  26. }
  27. if(distTable[n][b] == INF) {
  28. distTable[n][b] = distTable[a][b]+1;
  29. ++tail;
  30. queue[0][tail] = n;
  31. queue[1][tail] = b;
  32. }
  33. // empty
  34. if(distTable[a][0] == INF) {
  35. distTable[a][0] = distTable[a][b]+1;
  36. ++tail;
  37. queue[0][tail] = a;
  38. queue[1][tail] = 0;
  39. }
  40. if(distTable[0][b] == INF) {
  41. distTable[0][b] = distTable[a][b]+1;
  42. ++tail;
  43. queue[0][tail] = 0;
  44. queue[1][tail] = b;
  45. }
  46. // pour
  47. if(a+b <= n && distTable[a+b][0] == INF){
  48. distTable[a+b][0] = distTable[a][b]+1;
  49. ++tail;
  50. queue[0][tail] = a+b;
  51. queue[1][tail] = 0;
  52. }
  53. if(a+b > n && distTable[n][a+b-n] == INF){
  54. distTable[n][a+b-n] = distTable[a][b]+1;
  55. ++tail;
  56. queue[0][tail] = n;
  57. queue[1][tail] = a+b-n;
  58. }
  59. if(a+b <= m && distTable[0][a+b] == INF){
  60. distTable[0][a+b] = distTable[a][b]+1;
  61. ++tail;
  62. queue[0][tail] = 0;
  63. queue[1][tail] = a+b;
  64. }
  65. if(a+b > m && distTable[a+b-m][m] == INF){
  66. distTable[a+b-m][m] = distTable[a][b]+1;
  67. ++tail;
  68. queue[0][tail] = a+b-m;
  69. queue[1][tail] = m;
  70. }
  71. if (tail == maxSize) {
  72. tail = 0;
  73. }
  74. }
  75. return -1;
  76. }
  77. int main(){
  78. int n,m,d;
  79. scanf("%d %d %d",&n,&m,&d);
  80. printf("%d", bfs(n,m,d));
  81. return 0;
  82. }

好数字

  1. #include <stdio.h>
  2. typedef unsigned long long uint64;
  3. const uint64 mod = 1e9+7;
  4. uint64 power(uint64 a, uint64 e){
  5. uint64 r = 1;
  6. while (e){
  7. if (e&1){
  8. r = (r*a)%mod;
  9. }
  10. a = (a*a)%mod;
  11. e >>= 1;
  12. }
  13. return r;
  14. }
  15. int main(){
  16. uint64 n, num;
  17. scanf("%llu",&n);
  18. num = power(4,n/2)*power(5,n-n/2)%mod;
  19. printf("%llu",num);
  20. return 0;
  21. }

毕达哥拉斯三元组

  1. #include <stdio.h>
  2. unsigned long long pythagoras(unsigned int sum){
  3. // a as short catheti, b as long catheti, c as hypotenuse
  4. unsigned a,b,c;
  5. for (a = 1; a <= sum/4; ++a){
  6. for (b = a+1; b <= sum/2; ++b){
  7. c = sum-a-b;
  8. if ((a+b > c) & (c-a < b) & (a*a+b*b == c*c)){
  9. return a*b*c;
  10. }
  11. }
  12. }
  13. }
  14. int main(){
  15. unsigned int n;
  16. scanf("%u",&n);
  17. printf("%d", pythagoras(n));
  18. return 0;
  19. }

竖式乘法

  1. #include <stdio.h>
  2. #include <math.h>
  3. typedef unsigned int uint;
  4. uint digit(uint x){
  5. uint cnt = 0;
  6. if (x == 0){
  7. return 1;
  8. }
  9. while(x){
  10. x /= 10;
  11. ++cnt;
  12. }
  13. return cnt;
  14. }
  15. uint getDigital(uint x, uint n){
  16. x /= (uint)pow(10,n-1);
  17. return x%10;
  18. }
  19. int main(){
  20. uint up, down;
  21. scanf("%u %u",&up,&down);
  22. uint ans = up*down, len = digit(ans)+1, downLen = digit(down);
  23. for (uint i = 0; i < len-digit(up); ++i) {
  24. printf(" ");
  25. }
  26. printf("%u\n",up);
  27. printf("x");
  28. for (uint i = 1; i <= len-downLen-1; ++i) {
  29. printf(" ");
  30. }
  31. printf("%u\n",down);
  32. for (uint i = 1; i <= len; ++i) {
  33. printf("-");
  34. }
  35. printf("\n");
  36. uint tmp;
  37. for (uint i = 1; i <= downLen; ++i) {
  38. tmp = getDigital(down,i)*up;
  39. if (i == downLen){
  40. printf("+");
  41. } else {
  42. for (uint j = 1; j <= len-digit(tmp)-i+1; ++j) {
  43. printf(" ");
  44. }
  45. }
  46. printf("%u\n",tmp);
  47. }
  48. for (uint i = 1; i <= len; ++i) {
  49. printf("-");
  50. }
  51. printf("\n");
  52. printf(" %u",ans);
  53. return 0;
  54. }

查找数列

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

俄罗斯农夫乘法

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

阶乘倍数

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. typedef unsigned long long int64;
  4. int64 primeFactNum = 0;
  5. int64 prime[20] = {0}, num[20] = {0};
  6. bool isMulti(int64 n){
  7. int64 primeNum, tmp;
  8. for (int64 i = 1; i <= primeFactNum; ++i) {
  9. primeNum = 0;
  10. tmp = n;
  11. while (tmp) {
  12. primeNum += tmp/prime[i];
  13. tmp /= prime[i];
  14. }
  15. if(primeNum < num[i]) {
  16. return false;
  17. }
  18. }
  19. return true;
  20. }
  21. void solveFact(int64 k){
  22. for (int64 i = 2; i*i <= k; ++i) {
  23. if(k%i == 0){
  24. ++primeFactNum;
  25. prime[primeFactNum] = i;
  26. while (k%i == 0){
  27. ++num[primeFactNum];
  28. k /= i;
  29. }
  30. }
  31. }
  32. if (k > 1){
  33. ++primeFactNum;
  34. prime[primeFactNum] = k;
  35. ++num[primeFactNum];
  36. }
  37. }
  38. int main(){
  39. int64 left = 1, right = 1e19, mid, n, k;
  40. scanf("%lld",&k);
  41. solveFact(k);
  42. while(left <= right){
  43. mid = ((right-left)>>1)+left;
  44. if (isMulti(mid)){
  45. right = mid-1;
  46. n = mid;
  47. } else {
  48. left = mid+1;
  49. }
  50. }
  51. printf("%lld",n);
  52. return 0;
  53. }

方案数

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

31-40

哈沙德数

  1. #include <stdio.h>
  2. int HarshadNumber(int n){
  3. int t = n, s = 0;
  4. while (t) {
  5. s += t%10;
  6. t /= 10;
  7. }
  8. if ((s == 0) || (n%s != 0)) return 0;
  9. if (s == 1) return 1;
  10. return n/s;
  11. }
  12. int main(){
  13. int cnt = 0, n;
  14. scanf("%d",&n);
  15. if (n == 1) cnt = 1;
  16. while ((n != 0) && (n != 1)) {
  17. n = HarshadNumber(n);
  18. if (n) ++cnt;
  19. }
  20. printf("%d",cnt);
  21. return 0;
  22. }

素数

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. typedef unsigned long long uint64;
  5. uint64 primeNum(uint64 a, uint64 b){
  6. bool isPrime[b+1];
  7. memset(isPrime,1,b+1);
  8. uint64 cnt = 0;
  9. for (uint64 i = 2; i <= b; ++i){
  10. if (isPrime[i]){
  11. for (uint64 j = 2; j*i <= b; ++j){
  12. isPrime[j*i] = false;
  13. }
  14. }
  15. }
  16. for (uint64 i = a; i <= b; ++i){
  17. cnt += isPrime[i];
  18. }
  19. return cnt;
  20. }
  21. int main(){
  22. uint64 a,b,num;
  23. scanf("%llu %llu",&a,&b);
  24. num = primeNum(a,b);
  25. printf("%llu",num);
  26. return 0;
  27. }

基思数

  1. #include <stdio.h>
  2. int arr[8] = {0};
  3. int init(int n){
  4. int cnt = 0;
  5. while (n) {
  6. arr[cnt++] = n%10;
  7. n /= 10;
  8. }
  9. return cnt;
  10. }
  11. void isKeith(int n, int len){
  12. int i = len - 1;
  13. while (arr[i] < n){
  14. int sum = 0;
  15. for (int j = 0; j < len; ++j) {
  16. sum += arr[(i-j+len)%len];
  17. }
  18. arr[i] = sum;
  19. i = (i-1+len)%len;
  20. }
  21. if (arr[i] == n) printf("Yes");
  22. else printf("No");
  23. }
  24. int main() {
  25. int n;
  26. scanf("%d",&n);
  27. isKeith(n,init(n));
  28. return 0;
  29. }

二进制表示

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. void binary(int a){
  4. bool flag = false;
  5. for (int i = 15; i >= 0 ; --i) {
  6. if ((a>>i)&1) {
  7. if (flag) printf("+");
  8. if (i >= 2){
  9. printf("2(");
  10. binary(i);
  11. printf(")");
  12. }
  13. if (i == 1) printf("2");
  14. if (i == 0) printf("2(0)");
  15. flag = true;
  16. }
  17. }
  18. }
  19. int main() {
  20. int a;
  21. scanf("%d",&a);
  22. binary(a);
  23. return 0;
  24. }

光线追踪

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

冰雹数列

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

佩尔数

  1. #include <stdio.h>
  2. int PA(int n){
  3. if (n == 0) return 0;
  4. else if (n == 1) return 1;
  5. return 2*PA(n-1)+PA(n-2);
  6. }
  7. int PB(int n){
  8. int p0 = 0, p1 = 1, pn;
  9. for (int i = 0; i <= n; ++i) {
  10. if (i == 0) pn = p0;
  11. else if (i == 1) pn = p1;
  12. else {
  13. pn = 2 * p1 + p0;
  14. p0 = p1;
  15. p1 = pn;
  16. }
  17. }
  18. return pn;
  19. }
  20. int main() {
  21. int n, p;
  22. scanf("%d",&n);
  23. if (n&1) p = PA(n);
  24. else p = PB(n);
  25. printf("%d",p);
  26. return 0;
  27. }

可变参数累加

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. int sum(int start,...){
  4. va_list vaList;
  5. int sum = 0;
  6. int curr = start;
  7. va_start(vaList,start);
  8. while (start){
  9. sum += start;
  10. start = va_arg(vaList,int);
  11. }
  12. va_end(vaList);
  13. return sum;
  14. }
  15. int main() {
  16. int a,b,c,d,e,f;
  17. scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f);
  18. int sumMinus = sum(a,b,0) - sum(c,d,e,f,0);
  19. printf("%d",sumMinus);
  20. return 0;
  21. }

运动会

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. int phiEuler(int n){
  4. int phi[n+1],prime[n+1];
  5. bool isSieved[n+1];
  6. int sum = 0,cnt = 1, comp;
  7. prime[0] = 1;
  8. phi[1] = 1;
  9. for (int i = 2; i < n; ++i){
  10. if (!isSieved[i]){
  11. prime[cnt++] = i;
  12. phi[i] = i-1;
  13. }
  14. for (int j = 1; i*prime[j] <= n; ++j){
  15. comp = i*prime[j];
  16. isSieved[comp] = true;
  17. if (i%prime[j] == 0){
  18. phi[comp] = prime[j]*phi[i];
  19. break;
  20. } else{
  21. phi[comp] = (prime[j]-1)*phi[i];
  22. }
  23. }
  24. }
  25. for (int i = 1; i <= n-1; ++i) {
  26. sum += phi[i];
  27. }
  28. return sum;
  29. }
  30. int main() {
  31. int n, num;
  32. scanf("%d",&n);
  33. num = n == 1 ? 0 : (2*phiEuler(n)+1);
  34. printf("%d",num);
  35. return 0;
  36. }

可变参数平均

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. double avg(int num,...){
  4. va_list vaList;
  5. double sum = 0.0f;
  6. va_start(vaList,num);
  7. for (int i = 0; i < num; ++i) {
  8. sum += va_arg(vaList,int);
  9. }
  10. va_end(vaList);
  11. return sum/num;
  12. }
  13. int main() {
  14. int a,b,c,d,e;
  15. scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
  16. double avgMinus = avg(2,a,b) - avg(3,c,d,e);
  17. printf("%.4f",avgMinus);
  18. return 0;
  19. }

41-50

航空旅行

  1. #include<stdio.h>
  2. #include<stdbool.h>
  3. void pass(int a, int b, int c, int d, int e){
  4. bool flag = false;
  5. if (a <= e && (b + c) <= d) flag = true;
  6. if (b <= e && (a + c) <= d) flag = true;
  7. if (c <= e && (a + b) <= d) flag = true;
  8. if (flag) printf("YES\n");
  9. else printf("NO\n");
  10. }
  11. int main(){
  12. int n, a, b, c, d, e;
  13. scanf("%d",&n);
  14. while (n--){
  15. scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
  16. pass(a, b, c, d, e);
  17. }
  18. return 0;
  19. }

蒙特卡罗法求积分

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. double func1(double x) {
  5. return pow(x, 4) * exp(-x);
  6. }
  7. double func2(double x) {
  8. return x * x + 1;
  9. }
  10. double func3(double x) {
  11. return cos(x);
  12. }
  13. double func4(double x) {
  14. return sqrt(x) * (x - 2);
  15. }
  16. double func5(double x) {
  17. return 2 * sin(x) - 5 * cos(x);
  18. }
  19. double func(int m, double x) {
  20. switch (m) {
  21. case 1: return func1(x);
  22. case 2: return func2(x);
  23. case 3: return func3(x);
  24. case 4: return func4(x);
  25. case 5: return func5(x);
  26. default: return 0;
  27. }
  28. }
  29. double mtk(int m, double a, double b, int n) {
  30. srand(RAND_MAX);
  31. double w = b - a, sum = 0;
  32. for (int i = 1; i < n; ++i) {
  33. double x = ((double)rand() / RAND_MAX) * w + a;
  34. sum += func(m, x);
  35. }
  36. sum *= w / n;
  37. return sum;
  38. }
  39. int main() {
  40. int m, n;
  41. double a, b;
  42. scanf("%d %lf %lf %d", &m, &a, &b, &n);
  43. printf("%.6lf", mtk(m, a, b, n));
  44. return 0;
  45. }

飞机起飞速度(WA)

稀疏矩阵

  1. #include <stdio.h>
  2. int main () {
  3. int raw, col, n, num = 0;
  4. scanf("%d %d", &raw, &col);
  5. for (int i = 0; i < raw; ++i) {
  6. for (int j = 0; j < col; ++j) {
  7. scanf("%d", &n);
  8. if (n) ++num;
  9. }
  10. }
  11. double ratio = (double)num / (raw * col);
  12. if (num == raw || num == col || (ratio - 0.05) <= 1e-9)
  13. printf("Yes\n");
  14. else printf("No\n");
  15. return 0;
  16. }

回文数之和

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. int dec[10] = {0}, kSys[32] = {0};
  4. bool isPalindrome(int arr[], int cnt){
  5. int head = 0, tail = cnt - 1;
  6. while (head < tail) {
  7. if (arr[head] != arr[tail]) return false;
  8. ++head, --tail;
  9. }
  10. return true;
  11. }
  12. bool isBiPalindrome(int n, int k){
  13. int tmp = n, cnt = 0;
  14. while (tmp) {
  15. dec[cnt++] = tmp % 10;
  16. tmp /= 10;
  17. }
  18. if (!isPalindrome(dec, cnt)) return false;
  19. tmp = n, cnt = 0;
  20. while (tmp) {
  21. kSys[cnt++] = tmp % k;
  22. tmp /= k;
  23. }
  24. if (!isPalindrome(kSys, cnt)) return false;
  25. return true;
  26. }
  27. int main() {
  28. int n, k, sum = 0;
  29. scanf("%d %d", &n, &k);
  30. for (int i = 1; i <= n; ++i) {
  31. if (isBiPalindrome(i, k)) sum += i;
  32. }
  33. printf("%d", sum);
  34. return 0;
  35. }

完美矩阵

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #define MAXSIZE 301
  4. int arr[MAXSIZE][MAXSIZE] = {0};
  5. int preSum[MAXSIZE][MAXSIZE] = {0};
  6. void prefix(int n, int m){
  7. for (int i = 1; i <= n; ++i) {
  8. for (int j = 1; j <= m; ++j) {
  9. preSum[i][j] = preSum[i - 1][j] + preSum[i][j - 1]
  10. - preSum[i - 1][j - 1] + arr[i][j];
  11. }
  12. }
  13. }
  14. int getSum(int x1, int x2, int y1, int y2) {
  15. return preSum[x2][y2] - preSum[x1 - 1][y2] - preSum[x2][y1 - 1]
  16. + preSum[x1 - 1][y1 - 1];
  17. }
  18. bool isPerfect(int x1, int x2, int y1, int y2) {
  19. int outer = getSum(x1, x2, y1, y2), inner;
  20. int len = 2 * (x2 - x1 + y2 - y1);
  21. if ((x2 - x1) == 1 || (y2 - y1) == 1) inner = 0;
  22. else inner = getSum(x1 + 1, x2 - 1, y1 + 1, y2 - 1);
  23. if (inner != 1 && inner != 0 && inner != -1) return false;
  24. if ((outer - inner) != len) return false;
  25. return true;
  26. }
  27. int perfectNum(int n, int m) {
  28. int cnt = 0;
  29. for (int i = 1; i <= n; ++i) {
  30. for (int j = 1; j <= m; ++j) {
  31. for (int k = 1; k + i <= n && k + j <= m; ++k) {
  32. if (arr[i][k + j] == 0 || arr[k + i][j] == 0) break;
  33. if (isPerfect(i, i + k, j, j + k)) {
  34. ++cnt;
  35. }
  36. }
  37. }
  38. }
  39. return cnt;
  40. }
  41. int main () {
  42. int n, m;
  43. scanf("%d %d", &n, &m);
  44. for (int i = 1; i <= n; ++i) {
  45. for (int j = 1; j <= m; ++j) {
  46. scanf("%d", &arr[i][j]);
  47. if (arr[i][j] == 0) arr[i][j] = -1;
  48. }
  49. }
  50. prefix(n ,m);
  51. printf("%d", perfectNum(n, m));
  52. return 0;
  53. }

波士顿房价预测

  1. #include <stdio.h>
  2. double nAvg(double arr[], int n) {
  3. double sum = 0;
  4. for (int i = 0; i < n; ++i) {
  5. sum += arr[i];
  6. }
  7. return sum / n;
  8. }
  9. int main() {
  10. int n;
  11. scanf("%d", &n);
  12. double x[n], y[n];
  13. for (int i = 0; i < n; ++i) {
  14. scanf("%lf %lf", &x[i], &y[i]);
  15. }
  16. double xBar = nAvg(x, n), yBar = nAvg(y, n);
  17. double sumUp = 0, sumDown = 0;
  18. for (int i = 0; i < n; ++i) {
  19. sumUp += (x[i] - xBar) * (y[i] - yBar);
  20. }
  21. for (int i = 0; i < n; ++i) {
  22. sumDown += (x[i] - xBar) * (x[i] - xBar);
  23. }
  24. double b = sumUp / sumDown;
  25. double a = yBar - b * xBar;
  26. printf("Y=%.4lf+%.4lf*X",a,b);
  27. return 0;
  28. }

行列式值

  1. #include <stdio.h>
  2. #define MAX_SIZE 10
  3. void swapRows(double matrix[MAX_SIZE][MAX_SIZE], int row1, int row2, int n) {
  4. for (int i = 0; i < n; i++) {
  5. double temp = matrix[row1][i];
  6. matrix[row1][i] = matrix[row2][i];
  7. matrix[row2][i] = temp;
  8. }
  9. }
  10. double calculateDeterminant(double matrix[MAX_SIZE][MAX_SIZE], int n) {
  11. int i, j, k;
  12. double determinant = 1.0;
  13. for (i = 0; i < n; i++) {
  14. if (matrix[i][i] == 0.0) {
  15. for (j = i + 1; j < n; j++) {
  16. if (matrix[j][i] != 0.0) {
  17. swapRows(matrix, i, j, n);
  18. determinant *= -1.0;
  19. break;
  20. }
  21. }
  22. }
  23. if (matrix[i][i] == 0.0) {
  24. return 0.0;
  25. }
  26. double pivot = matrix[i][i];
  27. determinant *= pivot;
  28. for (j = i; j < n; j++) {
  29. matrix[i][j] /= pivot;
  30. }
  31. for (j = i + 1; j < n; j++) {
  32. double factor = matrix[j][i];
  33. for (k = i; k < n; k++) {
  34. matrix[j][k] -= factor * matrix[i][k];
  35. }
  36. }
  37. }
  38. return determinant;
  39. }
  40. int main() {
  41. int n;
  42. scanf("%d", &n);
  43. double matrix[MAX_SIZE][MAX_SIZE];
  44. for (int i = 0; i < n; i++) {
  45. for (int j = 0; j < n; j++) {
  46. scanf("%lf", &matrix[i][j]);
  47. }
  48. }
  49. double determinant = calculateDeterminant(matrix, n);
  50. printf("%.0lf\n", determinant);
  51. return 0;
  52. }
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int** init(int n) {
  4. int** matrix = (int**)malloc(sizeof(int*) * (n + 1));
  5. for(int i = 0; i <= n; i++) {
  6. matrix[i] = (int*)malloc(sizeof(int) * (n + 1));
  7. }
  8. return matrix;
  9. }
  10. int det(int **matrix, int n) {
  11. if (n == 1) return matrix[1][1];
  12. int sum = 0;
  13. int **subMatrix = init(n - 1);
  14. for (int i = 1; i <= n; ++i) {
  15. for (int j = 1; j <= n - 1; ++j) {
  16. for (int k = 1; k <= n - 1; ++k) {
  17. if (k < i) subMatrix[j][k] = matrix[j + 1][k];
  18. else subMatrix[j][k] = matrix[j + 1][k + 1];
  19. }
  20. }
  21. int sgn = i % 2 == 0 ? -1 : 1;
  22. sum += sgn * matrix[1][i] * det(subMatrix, n - 1);
  23. }
  24. return sum;
  25. }
  26. int main() {
  27. int n;
  28. scanf("%d", &n);
  29. int **matrix = init(n);
  30. for (int i = 1; i <= n; ++i) {
  31. for (int j = 1; j <= n; ++j) {
  32. scanf("%d", &matrix[i][j]);
  33. }
  34. }
  35. printf("%d", det(matrix, n));
  36. return 0;
  37. }

货运优化

  1. #include <stdio.h>
  2. int main() {
  3. int l3s2[4] = {0, 5, 3, 1};
  4. int n, x1, x2, x3, x4, x5, x6, s2, s1;
  5. while (1) {
  6. scanf("%d %d %d %d %d %d", &x1, &x2, &x3, &x4, &x5, &x6);
  7. if ((x1 + x2 + x3 + x4 + x5 + x6) == 0) break;
  8. n = (x3 + 3) / 4 + x4 + x5 + x6;
  9. s2 = 5 * x4 + l3s2[x3 % 4];
  10. if (x2 > s2) n += (x2 - s2 + 8) / 9;
  11. s1 = 36 * n - 36 * x6 - 25 * x5 - 16 * x4 - 9 * x3 - 4 * x2;
  12. if (x1 > s1) n += (x1 - s1 + 35) / 36;
  13. printf("%d\n",n);
  14. }
  15. return 0;
  16. }

素数筛法

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #define NUM (int)1e7+1
  4. static bool isSieved[NUM];
  5. static int prime[NUM];
  6. int main() {
  7. int n, k = 0;
  8. scanf("%d", &n);
  9. isSieved[1] = true;
  10. for (int i = 2; i <= n; ++i) {
  11. if (!isSieved[i]) prime[++k] = i;
  12. for (int j = 1; prime[j] * i <= n; ++j) {
  13. isSieved[prime[j] * i] = true;
  14. if (i % prime[j] == 0) break;
  15. }
  16. }
  17. printf("%d", k);
  18. }

51-60

字符串替换(WA)

删除前后缀

  1. #include <stdio.h>
  2. #include <string.h>
  3. void strRemovePrefix(char *str, char *prefix) {
  4. int cnt = 0;
  5. char *pStr = str, *pPrefix = prefix;
  6. while (*pPrefix && *pStr && *pStr == *pPrefix) {
  7. while (*pPrefix && *pStr && *pStr == *pPrefix)
  8. ++pStr, ++pPrefix, ++cnt;
  9. pPrefix = prefix;
  10. }
  11. int len = strlen(prefix);
  12. int mov = (cnt / len) * len;
  13. if (mov) memmove(str - mov, str, strlen(str) + 1);
  14. }
  15. void strRemoveSuffix(char *str, char *suffix) {
  16. int len = strlen(suffix);
  17. char *pStr = str + strlen(str) - len, *pSuffix = suffix;
  18. while (*pSuffix && pStr >= str && *pStr == *pSuffix) {
  19. int cnt = 0;
  20. while (*pSuffix && pStr && *pStr == *pSuffix)
  21. ++pStr, ++pSuffix, ++cnt;
  22. if (cnt == len) {
  23. pSuffix = suffix, pStr = pStr - 2 * len;
  24. *(pStr + len) = '\0';
  25. } else break;
  26. }
  27. }
  28. int main() {
  29. char str1[1000] = "", fix[1000] = "", str2[1000] = "";
  30. scanf("%[^\n] %[^\n]", str1, fix);
  31. memcpy(str2, str1, strlen(str1) + 1);
  32. strRemovePrefix(str1, fix);
  33. puts(str1);
  34. strRemoveSuffix(str2, fix);
  35. puts(str2);
  36. return 0;
  37. }

大小写交换

  1. #include <stdio.h>
  2. void strSwapCase(char str[]) {
  3. for (int i = 0; str[i] != '\0'; ++i) {
  4. if ('a' <= str[i] && str[i] <= 'z')
  5. str[i] = (char) str[i] - 'a' + 'A';
  6. else if ('A' <= str[i] && str[i] <= 'Z')
  7. str[i] = (char) str[i] - 'A' + 'a';
  8. }
  9. }
  10. int main() {
  11. char input[1000] = "";
  12. scanf("%[^\n]",input);
  13. strSwapCase(input);
  14. puts(input);
  15. return 0;
  16. }

前后缀移除

  1. #include <stdio.h>
  2. #include <string.h>
  3. void strLeftStrip(char *str, char *chars) {
  4. int mov = 0;
  5. char *pStr = str;
  6. while (*pStr) {
  7. if (strchr(chars, *pStr)) ++mov;
  8. else break;
  9. ++pStr;
  10. }
  11. if (mov) memmove(str - mov, str, strlen(str) + 1);
  12. }
  13. void strRightStrip(char *str, char *chars) {
  14. int len = 0;
  15. char *pStr = str + strlen(str) - 1;
  16. while (pStr >= str) {
  17. if (strchr(chars, *pStr)) ++len;
  18. else break;
  19. --pStr;
  20. }
  21. *(str + strlen(str) - len) = '\0';
  22. }
  23. int main() {
  24. char str1[1000] = "", chars[1000] = "", str2[1000] = "";
  25. scanf("%[^\n] %[^\n]", str1, chars);
  26. memcpy(str2, str1, strlen(str1) + 1);
  27. strLeftStrip(str1, chars);
  28. puts(str1);
  29. strRightStrip(str2, chars);
  30. puts(str2);
  31. strRightStrip(str1, chars);
  32. puts(str1);
  33. return 0;
  34. }

字符串后缀

  1. #include <stdio.h>
  2. #include <string.h>
  3. void strEndsWith(char *str, char *suffix) {
  4. int len = strlen(suffix);
  5. char *pStr = str + strlen(str) - len, *pSuffix = suffix;
  6. while (*pSuffix && *pStr) {
  7. if (*pSuffix != *pStr) {
  8. printf("No\n");
  9. return;
  10. }
  11. else ++pSuffix, ++pStr;
  12. }
  13. printf("Yes\n");
  14. }
  15. int main() {
  16. char str[1000] = "", suffix[1000] = "";
  17. scanf("%[^\n] %[^\n]", str, suffix);
  18. strEndsWith(str, suffix);
  19. return 0;
  20. }

Atol转换

  1. #include <stdio.h>
  2. #include <limits.h>
  3. int atol(char *str) {
  4. char *pStr = str;
  5. int sgn = 1;
  6. long long tmp = 0;
  7. if (*pStr == '+') ++pStr;
  8. else if (*pStr == '-') sgn = -1, ++pStr;
  9. while (*pStr) {
  10. if (*pStr == ' ') ;
  11. else if ('0' <= *pStr && *pStr <= '9') {
  12. tmp = (*pStr - '0') + tmp * 10;
  13. if ((tmp * sgn) >= INT_MAX) return INT_MAX;
  14. else if ((tmp * sgn) <= INT_MIN) return INT_MIN;
  15. }
  16. else break;
  17. ++pStr;
  18. }
  19. return tmp * sgn;
  20. }
  21. int main() {
  22. char str[1000] = "";
  23. scanf("%[^\n]", str);
  24. printf("%d", atol(str));
  25. return 0;
  26. }

元宇宙A+B

  1. #include <stdio.h>
  2. #include <string.h>
  3. const static char decToMeta[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  4. static char c[100] = "", a[100] = "", b[100] = "";
  5. static int C[100] = {0}, A[100] = {0}, B[100] = {0};
  6. int metaToDec(char m) {
  7. if ('0' <= m && m <= '9') return m - '0';
  8. return m - 'A' + 10;
  9. }
  10. void add(void) {
  11. int lenA = strlen(a), lenB = strlen(b);
  12. for (int i = 0; i < lenA; ++i) A[i] = metaToDec(a[lenA - i - 1]);
  13. for (int i = 0; i < lenB; ++i) B[i] = metaToDec(b[lenB - i - 1]);
  14. int carry = 0;
  15. int lenC = lenA > lenB ? lenA : lenB;
  16. for (int i = 0; i < lenC; ++i) {
  17. C[i] = A[i] + B[i] + carry;
  18. carry = C[i] / 36;
  19. C[i] %= 36;
  20. }
  21. if (carry != 0) {
  22. C[lenC] = carry;
  23. ++lenC;
  24. }
  25. for (int i = lenC - 1; i >= 0; --i) c[i] = decToMeta[C[lenC - i - 1]];
  26. c[lenC] = '\0';
  27. }
  28. int main() {
  29. scanf("%s %s", a, b);
  30. add();
  31. puts(c);
  32. return 0;
  33. }

字符串切片

  1. #include <stdio.h>
  2. #include <string.h>
  3. int t, n, len;
  4. char str[1000];
  5. void strSlice(int begin, int end, int step) {
  6. char slice[1000] = "";
  7. int pos = 0;
  8. if (begin < 0) begin += len;
  9. if (end < 0) end += len;
  10. if (end >= begin && step > 0) {
  11. for (int i = begin; i < end; i += step) {
  12. slice[pos] = str[i];
  13. ++pos;
  14. }
  15. } else if (end < begin && step < 0) {
  16. for (int i = begin; i > end; i += step) {
  17. slice[pos] = str[i];
  18. ++pos;
  19. }
  20. }
  21. puts(slice);
  22. }
  23. void mode(void) {
  24. len = strlen(str);
  25. int begin, end, step;
  26. switch (n) {
  27. case 3: {
  28. scanf("%d %d %d", &begin, &end, &step);
  29. break;
  30. }
  31. case 2: {
  32. scanf("%d %d", &begin, &end);
  33. step = 1;
  34. break;
  35. }
  36. case 1: {
  37. scanf("%d", &begin);
  38. end = len, step = 1;
  39. break;
  40. }
  41. }
  42. strSlice(begin, end, step);
  43. }
  44. int main() {
  45. scanf("%[^\n] %d", str, &t);
  46. for (int i = 0; i < t; ++i) {
  47. scanf("%d", &n);
  48. mode();
  49. }
  50. return 0;
  51. }

分离字符串

  1. #include <stdio.h>
  2. #include <string.h>
  3. void split(char *str, char *sep) {
  4. while (*str) {
  5. char *flag = strstr(str, sep);
  6. if (flag == NULL) break;
  7. char sub[101] = "";
  8. int len = flag - str;
  9. memcpy(sub, str, len);
  10. puts(sub);
  11. memmove(str - len - strlen(sep), str, strlen(str) + 1);
  12. }
  13. puts(str);
  14. }
  15. int main() {
  16. char str[2000] = "", sep[2000] = "";
  17. scanf("%[^\n] %[^\n]", str, sep);
  18. split(str, sep);
  19. return 0;
  20. }

Kids A+B

  1. #include <stdio.h>
  2. #include <string.h>
  3. char ans[30] = "";
  4. int strToNum(char *str) {
  5. if(strstr(str, "zero")) return 0;
  6. if(strstr(str, "ten")) return 10;
  7. if(strstr(str, "eleven")) return 11;
  8. if(strstr(str, "twelve")) return 12;
  9. if(strstr(str, "thirteen")) return 13;
  10. if(strstr(str, "fourteen")) return 14;
  11. if(strstr(str, "fifteen")) return 15;
  12. if(strstr(str, "sixteen")) return 16;
  13. if(strstr(str, "seventeen")) return 17;
  14. if(strstr(str, "eighteen")) return 18;
  15. if(strstr(str, "nineteen")) return 19;
  16. int unit = 0, decade = 0;
  17. if(strstr(str, "one")) unit = 1;
  18. if(strstr(str, "two")) unit = 2;
  19. if(strstr(str, "three")) unit = 3;
  20. if(strstr(str, "four")) unit = 4;
  21. if(strstr(str, "five")) unit = 5;
  22. if(strstr(str, "six")) unit = 6;
  23. if(strstr(str, "seven")) unit = 7;
  24. if(strstr(str, "eight")) unit = 8;
  25. if(strstr(str, "nine")) unit = 9;
  26. if(strstr(str, "twenty")) decade = 20;
  27. if(strstr(str, "thirty")) decade = 30;
  28. if(strstr(str, "forty")) decade = 40;
  29. if(strstr(str, "fifty")) decade = 50;
  30. if(strstr(str, "sixty")) decade = 60;
  31. if(strstr(str, "seventy")) decade = 70;
  32. if(strstr(str, "eighty")) decade = 80;
  33. if(strstr(str, "ninety")) decade = 90;
  34. return unit + decade;
  35. }
  36. void numToStr(int n) {
  37. switch (n) {
  38. case 0: {
  39. strcpy(ans, "zero");
  40. char *p = ans;
  41. return;
  42. }
  43. case 11: {
  44. strcpy(ans, "eleven");
  45. char *p = ans;
  46. return;
  47. }
  48. case 12: {
  49. strcpy(ans, "twelve");
  50. char *p = ans;
  51. return;
  52. }
  53. case 13: {
  54. strcpy(ans, "thirteen");
  55. char *p = ans;
  56. return;
  57. }
  58. case 14: {
  59. strcpy(ans, "fourteen");
  60. char *p = ans;
  61. return;
  62. }
  63. case 15: {
  64. strcpy(ans, "fifteen");
  65. char *p = ans;
  66. return;
  67. }
  68. case 16: {
  69. strcpy(ans, "sixteen");
  70. char *p = ans;
  71. return;
  72. }
  73. case 17: {
  74. strcpy(ans, "seventeen");
  75. char *p = ans;
  76. return;
  77. }
  78. case 18: {
  79. strcpy(ans, "eighteen");
  80. char *p = ans;
  81. return;
  82. }
  83. case 19: {
  84. strcpy(ans, "nineteen");
  85. char *p = ans;
  86. return;
  87. }
  88. default:
  89. break;
  90. }
  91. int decade = (n / 10) % 10, unit = n % 10;
  92. switch (decade) {
  93. case 2: {
  94. strcpy(ans, "twenty");
  95. break;
  96. }
  97. case 3: {
  98. strcpy(ans, "thirty");
  99. break;
  100. }
  101. case 4: {
  102. strcpy(ans, "forty");
  103. break;
  104. }
  105. case 5: {
  106. strcpy(ans, "fifty");
  107. break;
  108. }
  109. case 6: {
  110. strcpy(ans, "sixty");
  111. break;
  112. }
  113. case 7: {
  114. strcpy(ans, "seventy");
  115. break;
  116. }
  117. case 8: {
  118. strcpy(ans, "eighty");
  119. break;
  120. }
  121. case 9: {
  122. strcpy(ans, "ninety");
  123. break;
  124. }
  125. default: {
  126. break;
  127. }
  128. }
  129. if (decade && unit) strcat(ans, "-");
  130. switch (unit) {
  131. case 1: {
  132. strcat(ans, "one");
  133. break;
  134. }
  135. case 2: {
  136. strcat(ans, "two");
  137. break;
  138. }
  139. case 3: {
  140. strcat(ans, "three");
  141. break;
  142. }
  143. case 4: {
  144. strcat(ans, "four");
  145. break;
  146. }
  147. case 5: {
  148. strcat(ans, "five");
  149. break;
  150. }
  151. case 6: {
  152. strcat(ans, "six");
  153. break;
  154. }
  155. case 7: {
  156. strcat(ans, "seven");
  157. break;
  158. }
  159. case 8: {
  160. strcat(ans, "eight");
  161. break;
  162. }
  163. case 9: {
  164. strcat(ans, "nine");
  165. break;
  166. }
  167. default: {
  168. break;
  169. }
  170. }
  171. }
  172. int main() {
  173. char a[30] = "", b[30] = "";
  174. scanf("%s %s", a, b);
  175. numToStr(strToNum(a) + strToNum(b));
  176. puts(ans);
  177. return 0;
  178. }

61-70

时钟A-B

  1. #include <stdio.h>
  2. #include <time.h>
  3. int main(){
  4. struct tm begin = {0}, end = {0};
  5. scanf("%d %d %d", &begin.tm_year, &begin.tm_mon, &begin.tm_mday);
  6. scanf("%d %d %d", &end.tm_year, &end.tm_mon, &end.tm_mday);
  7. begin.tm_year -= 1900, begin.tm_mon -= 1;
  8. end.tm_year -= 1900, end.tm_mon -= 1;
  9. time_t tmBegin = mktime(&begin);
  10. time_t tmEnd = mktime(&end);
  11. printf("%.6lf", difftime(tmBegin, tmEnd));
  12. return 0;
  13. }

加密字串

  1. #include <stdio.h>
  2. static int freq[26] = {0};
  3. int main() {
  4. char plain[8000] = "";
  5. int x;
  6. scanf("%s %d", plain, &x);
  7. for (int i = 0; plain[i]; ++i) ++freq[plain[i] - 'a'];
  8. char cipher[8000] = "";
  9. for (int i = 0; plain[i]; ++i) {
  10. if (freq[plain[i] - 'a'] & 1)
  11. cipher[i] = (char) (((plain[i] - 'a' - x) % 26 + 26) % 26 + 'a');
  12. else
  13. cipher[i] = (char) ((plain[i] - 'a' + x) % 26 + 'a');
  14. }
  15. puts(cipher);
  16. return 0;
  17. }

Arduino显示

  1. #include <stdio.h>
  2. static const int digit[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  3. int getUnit(int num) {
  4. int cnt = 0;
  5. do {
  6. cnt += digit[num % 10];
  7. num /= 10;
  8. } while (num);
  9. return cnt;
  10. }
  11. int main() {
  12. int n;
  13. scanf("%d", &n);
  14. n -= 4;
  15. if (n <= 0) printf("0");
  16. else {
  17. int cnt = 0;
  18. for (int i = 0; i <= 1111; ++i) {
  19. for (int j = 0; j <= 1111; ++j) {
  20. if (getUnit(i) + getUnit(j) + getUnit(i + j) == n) ++cnt;
  21. }
  22. }
  23. printf("%d", cnt);
  24. }
  25. return 0;
  26. }

有效表达式

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

长安

  1. #include <stdio.h>
  2. int bx, by, px, py, cnt;
  3. void dfs(int x, int y) {
  4. if ((x == px && y == py) || x > bx || y > by) return;
  5. if (x == bx && y == by) {
  6. ++cnt;
  7. return;
  8. }
  9. dfs(x + 1, y);
  10. dfs(x, y + 1);
  11. }
  12. int main() {
  13. while (1) {
  14. fflush(stdin);
  15. scanf("%d %d %d %d", &bx, &by, &px, &py);
  16. if (bx <= 0 || by <= 0 || px <= 0 || py <= 0) break;
  17. cnt = 0;
  18. dfs(1, 1);
  19. printf("%d\n", cnt);
  20. }
  21. return 0;
  22. }

GPS通信协议(CPP)

感谢 Sekiro_2 提供的代码~

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. string out[100];
  4. int k=0;
  5. int check(string str){
  6. int i,result;
  7. for(result=str[1],i=2;str[i]!='*';i++)
  8. {
  9. result^=str[i];
  10. }
  11. return result;
  12. }
  13. int convert(string str){
  14. int res=0;
  15. res=stoi(str,0,16);
  16. return res;
  17. }
  18. void convert_BeingTime(string utcTime){
  19. int hour=stoi(utcTime.substr(0,2));
  20. int B_hour=(hour+8)%24;
  21. if(B_hour/10==0)
  22. out[k++]="0"+to_string(B_hour)+":"+utcTime.substr(2,2)+":"+utcTime.substr(4,2);
  23. else
  24. out[k++]=to_string(B_hour)+":"+utcTime.substr(2,2)+":"+utcTime.substr(4,2);
  25. }
  26. int main(){
  27. string str;
  28. while(cin>>str){
  29. if(str=="END") break;
  30. if(str.compare(0,6,"$GPRMC")==0){
  31. size_t asteriskPos = str.find('*');
  32. if(asteriskPos!=string::npos){
  33. int checksum=check(str);
  34. int senchecksum=convert(str.substr(asteriskPos + 1, 2));
  35. if(checksum!=senchecksum) {
  36. out[k++]="error";
  37. }
  38. else{
  39. string utcTime = str.substr(7, 6);
  40. convert_BeingTime(utcTime);
  41. }
  42. }
  43. }
  44. }
  45. for(int i=0;i<k;i++){
  46. cout<<out[i]<<endl;
  47. }
  48. }

三元搜索

  1. #include <stdio.h>
  2. int terSearch(int arr[], int n, int k) {
  3. int left = 0, right = n - 1, mid1 = (n - 1) / 3, mid2 = n - mid1;
  4. while(mid1 != mid2) {
  5. if (k > arr[right] || k < arr[left]) return -1;
  6. if (k == arr[mid1]) return mid1;
  7. if (k == arr[mid2]) return mid2;
  8. if (mid1 == mid2) break;
  9. if (k < arr[mid1]) right = mid1 - 1;
  10. else if (k > arr[mid2]) left = mid2 + 1;
  11. else left = mid1 + 1, right = mid2 - 1;
  12. mid1 = left + (right - left) / 3, mid2 = right - (right - left) / 3;
  13. }
  14. return -1;
  15. }
  16. int main() {
  17. int n, k;
  18. scanf("%d", &n);
  19. int arr[n];
  20. for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);
  21. scanf("%d", &k);
  22. printf("%d in [%d]", k, terSearch(arr, n, k));
  23. return 0;
  24. }

DNA双螺旋结构

  1. #include <stdio.h>
  2. void putsDna1(){
  3. printf(" AT \n");
  4. printf(" T--A \n");
  5. printf(" A----T \n");
  6. printf("T------A\n");
  7. printf("T------A\n");
  8. printf(" G----C \n");
  9. printf(" T--A \n");
  10. printf(" GC \n");
  11. }
  12. void putsDna2(){
  13. printf(" CG \n");
  14. printf(" C--G \n");
  15. printf(" A----T \n");
  16. printf("A------T\n");
  17. printf("T------A\n");
  18. printf(" A----T \n");
  19. printf(" A--T \n");
  20. printf(" GC \n");
  21. }
  22. void putsDna3(){
  23. printf(" AT \n");
  24. printf(" C--G \n");
  25. printf(" T----A \n");
  26. printf("C------G\n");
  27. printf("C------G\n");
  28. printf(" T----A \n");
  29. printf(" G--C \n");
  30. printf(" AT \n");
  31. }
  32. int main() {
  33. int n;
  34. scanf("%d", &n);
  35. for (int i = 1; i <= n/2; ++i) {
  36. if (i % 3 == 1) putsDna1();
  37. else if (i % 3 == 2) putsDna2();
  38. else putsDna3();
  39. }
  40. return 0;
  41. }

PID控制

  1. #include <stdio.h>
  2. typedef struct PIDController {
  3. double Kp, Ki, Kd;
  4. double preError, integral;
  5. } PIDData;
  6. double PIDCalculate(PIDData *pid, double setPoint, double measuredValue) {
  7. double error = setPoint - measuredValue;
  8. pid->integral += error;
  9. double differential = error - pid->preError;
  10. double output = pid->Kp * error + pid->Ki * pid->integral + pid->Kd * differential;
  11. pid->preError = error;
  12. return output;
  13. }
  14. int main() {
  15. double setPoint, measuredValue;
  16. int time;
  17. PIDData pid = {0};
  18. scanf("%lf %lf %lf", &pid.Kp, &pid.Ki, &pid.Kd);
  19. scanf("%lf %lf %d", &setPoint, &measuredValue, &time);
  20. for (int i = 1; i <= time; ++i) {
  21. double output = PIDCalculate(&pid, setPoint, measuredValue);
  22. measuredValue += output;
  23. printf("%d %.6lf\n", i, measuredValue);
  24. }
  25. return 0;
  26. }

循环排序

  1. #include <stdio.h>
  2. void swap(int *a, int *b) {
  3. int tmp = *a;
  4. *a = *b, *b = tmp;
  5. }
  6. void cycleSort(int arr[], int n) {
  7. for (int i = 0; i < n - 1; ++i) {
  8. int item = arr[i], pos = i;
  9. for (int j = i + 1; j < n; ++j) if (arr[j] < item) ++pos;
  10. if (pos == i) continue;
  11. swap(&arr[pos], &item);
  12. while(pos != i) {
  13. pos = i;
  14. for (int j = i + 1; j < n; ++j) if (arr[j] < item) ++pos;
  15. while (item == arr[pos]) ++pos;
  16. swap(&arr[pos], &item);
  17. }
  18. }
  19. }
  20. int main() {
  21. int n;
  22. scanf("%d", &n);
  23. int arr[n];
  24. for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);
  25. cycleSort(arr, n);
  26. for (int i = 0; i < n; ++i) printf("%d ", arr[i]);
  27. return 0;
  28. }

71-80

卫星定位(WA)

热能计算

  1. #include <stdio.h>
  2. int main() {
  3. int Ti, Tf, cL, cV;
  4. double mL, mV;
  5. scanf("%d %d %lf %d %lf %d", &Ti, &Tf, &mL, &cL, &mV, &cV);
  6. double QL = cL * mL * (Tf - Ti);
  7. double QV = cV * mV * (Tf - Ti);
  8. double Q = QL + QV;
  9. printf("%.2lfkJ,%.2lf%%,%.2lf%%\n", Q / 1000, QV / Q, QL / Q);
  10. return 0;
  11. }
  12. /*
  13. 20 80
  14. 0.250 4186
  15. 0.500 900
  16. */

几何约束

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. int maxi(int a, int b) {
  4. return a > b ? a : b;
  5. }
  6. int mini(int a, int b) {
  7. return a < b ? a : b;
  8. }
  9. int cross(int x1, int y1, int x2, int y2) {
  10. return x1 * y2 - y1 * x2;
  11. }
  12. bool insert(int line1[4], int line2[4]) {
  13. if (maxi(line2[0], line2[2]) < mini(line1[0], line1[2]) ||
  14. maxi(line1[0],line1[2]) < mini(line2[0], line2[2]) ||
  15. maxi(line2[1], line2[3]) < mini(line1[1], line1[3]) ||
  16. maxi(line1[1], line2[3]) < mini(line2[1],line2[3])) return false;
  17. if (cross(line1[2] - line1[0], line1[3] - line1[1], line2[0] - line1[0], line2[1] - line1[1]) *
  18. cross(line1[2] - line1[0], line1[3] - line1[1], line2[2] - line1[0], line2[3] - line1[1]) > 0 ||
  19. cross(line2[2] - line2[0], line2[3] - line2[1], line1[0] - line2[0], line1[1] - line2[1]) *
  20. cross(line2[2] - line2[0], line2[3] - line2[1], line1[2] - line2[0], line1[3] - line2[1]) > 0) return false;
  21. return true;
  22. }
  23. int main() {
  24. int n;
  25. scanf("%d", &n);
  26. int line[n][4];
  27. for (int i = 0; i < n; ++i)
  28. for (int j = 0; j < 4; ++j) scanf("%d", &line[i][j]);
  29. int cnt = 0;
  30. for (int i = 0; i < n; ++i) {
  31. for (int j = i + 1; j < n; ++j) {
  32. if (insert(line[i], line[j])) {
  33. printf("X: #%d #%d\n", i + 1, j + 1);
  34. ++cnt;
  35. }
  36. }
  37. }
  38. printf("n=%d\n", cnt);
  39. return 0;
  40. }
  41. /*
  42. 5
  43. 1 5 4 5
  44. 2 5 10 1
  45. 3 2 10 3
  46. 6 4 9 4
  47. 7 1 8 1
  48. */

日出日落时间(WA)

中位数

  1. #include <stdio.h>
  2. int arr[1000];
  3. double mid(int n) {
  4. if (n & 1) return arr[n / 2];
  5. return (arr[n / 2] + arr[n / 2 - 1]) / 2.0f;
  6. }
  7. int main() {
  8. int flag, cnt = 0;
  9. while(1) {
  10. scanf("%d", &flag);
  11. if (flag == -1) break;
  12. while (1) {
  13. if (flag == 0) {
  14. for (int i = 0; i < cnt; ++i) printf("%d ", arr[i]);
  15. printf("%.6lf\n", mid(cnt));
  16. break;
  17. }
  18. arr[cnt++] = flag;
  19. scanf("%d", &flag);
  20. }
  21. }
  22. return 0;
  23. }

原子计数(CPP)

感谢 Cubeist 提供的代码~

  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4. string s;
  5. int getnum(int x)
  6. {
  7. int res = 0;
  8. for (; s[x]>='0' && s[x]<='9' && s[x]; x ++)
  9. res = res * 10 + s[x] - '0';
  10. return res + !res;
  11. }
  12. int main()
  13. {
  14. getline(cin, s);
  15. map<string, int> mp;
  16. for (int i = 0; s[i]; i ++)
  17. {
  18. if (!(s[i]>='A' && s[i]<='Z')) continue;
  19. string ele = "";
  20. ele += s[i];
  21. if (s[i+1]>='a' && s[i+1]<='z')
  22. {
  23. ele += s[i+1];
  24. mp[ele] += getnum(i+2);
  25. }
  26. else
  27. mp[ele] += getnum(i+1);
  28. }
  29. for (auto& p : mp)
  30. cout << p.first << " " << p.second << endl;
  31. return 0;
  32. }

成绩单

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. typedef struct tag {
  5. long long id;
  6. char name[31];
  7. int score;
  8. } ST;
  9. void load(ST* arr[], int n) {
  10. for (int i = 0; i < n; ++i) {
  11. arr[i] = (ST*) malloc(sizeof(ST));
  12. scanf("%lld %s %d", &arr[i]->id, arr[i]->name, &arr[i]->score);
  13. }
  14. }
  15. void sort(ST* arr[], int n) {
  16. ST* tmp = NULL;
  17. for (int i = 0; i < n; ++i) {
  18. bool isSwapped = false;
  19. for (int j = 0; j < n - 1 - i; ++j)
  20. if ((arr[j]->score < arr[j + 1]->score) ||
  21. (arr[j]->score == arr[j + 1]->score && arr[j]->id > arr[j + 1]->id))
  22. tmp = arr[j], arr[j] = arr[j + 1], arr[j + 1] = tmp, isSwapped = true;
  23. if (!isSwapped) break;
  24. }
  25. }
  26. void traverse(ST* arr[], int n) {
  27. for (int i = 0; i < n; ++i)
  28. printf("%lld %s %d\n", arr[i]->id, arr[i]->name, arr[i]->score);
  29. }
  30. int main() {
  31. int n;
  32. scanf("%d", &n);
  33. ST* grade[n];
  34. load(grade, n);
  35. sort(grade, n);
  36. traverse(grade, n);
  37. return 0;
  38. }
  39. /*
  40. 6
  41. 2001900001 Jerry 88
  42. 2001900005 Tom 92
  43. 2001900006 Milla 85
  44. 2001900002 Alice 80
  45. 2001900003 Mickey 85
  46. 2001900004 Aladdin 83
  47. */

水下声学定位

  1. #include<stdio.h>
  2. #include<math.h>
  3. int main()
  4. {
  5. const double pi = 3.1415926f;
  6. double a, b, c, d, diag;
  7. scanf("%lf %lf %lf %lf %lf", &a, &b, &c, &d, &diag);
  8. double p = (a + b + diag) / 2, q = (c + d + diag);
  9. double s = sqrt(p * (p - a) * (p - b) * (p - diag)) + sqrt(q * (q - c) * (q - d) * (q - diag));
  10. double angle = atan((4 * s) / (pow(b, 2) + pow(d, 2) - pow(a, 2) - pow(c, 2)));
  11. angle *= 180 / pi;
  12. printf("%.6lf %.1lf", s, angle);
  13. }

火箭发射模拟(CPP)

感谢 De1ta_Zer0 提供的代码~

  1. #include <iostream>
  2. #include <iomanip>
  3. const double timeStep = 0.1;
  4. int main()
  5. {
  6. double totalMass, rocketMass, burnTime, cE, g;
  7. std::cin >> totalMass >> rocketMass >> burnTime >> cE >> g;
  8. double propellantMass = totalMass - rocketMass;
  9. double massFlow = propellantMass / burnTime;
  10. double T = massFlow * cE;
  11. double altitude = 0, V = 0;
  12. double timeLeft = burnTime + timeStep;
  13. while(timeLeft >= 0)
  14. {
  15. double a = T / totalMass;
  16. double deltaV = a * timeStep;
  17. double deltaAltitude = V * timeStep;
  18. double deltaM = massFlow * timeStep;
  19. V += deltaV;
  20. altitude += deltaAltitude;
  21. totalMass -= deltaM;
  22. timeLeft -= timeStep;
  23. }
  24. std::cout << std::fixed << std::setprecision(3) << altitude / 1000 << "km" << std::endl;
  25. return 0;
  26. }

晶体结构(CPP)

感谢 De1ta_Zer0 提供的代码~

  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <cmath>
  5. struct Atom
  6. {
  7. std::string name;
  8. double mass;
  9. double APF;
  10. double r;
  11. };
  12. Atom elemList[] =
  13. {
  14. { "Po", 208.998, 0.52360, 1.68 },
  15. { "Li", 6.941, 0.68017, 1.52 },
  16. { "Na", 22.989770, 0.68017, 1.86 },
  17. { "Cr", 51.9961, 0.68017, 1.28 },
  18. { "Mn", 54.938049, 0.68017, 1.27 },
  19. { "Fe", 55.845, 0.68017, 1.26 },
  20. { "Mo", 95.94, 0.68017, 1.39 },
  21. { "Ta", 180.9749, 0.68017, 1.46 },
  22. { "Al", 26.981538, 0.74048, 1.43 },
  23. { "Ca", 40.078, 0.74048, 1.97 },
  24. { "Ni", 58.6934, 0.74048, 1.24 },
  25. { "Cu", 63.546, 0.74048, 1.28 },
  26. { "Ge", 72.64, 0.74048, 1.22 },
  27. { "Ag", 107.8682, 0.74048, 1.44 },
  28. { "Pt", 195.078, 0.74048, 1.39 },
  29. { "Au", 196.96655, 0.74048, 1.44 },
  30. { "Pb", 207.2, 0.74048, 1.75 }
  31. };
  32. template <int N> double pow(double a) { return a * pow<N - 1>(a); }
  33. template <> double pow<0>(double a) { return 1; }
  34. int main()
  35. {
  36. int n;
  37. std::cin >> n;
  38. for (int i = 0; i < n; ++i)
  39. {
  40. std::string in;
  41. std::cin >> in;
  42. for(const auto& e : elemList)
  43. {
  44. if(e.name == in)
  45. {
  46. double V = 4.0 * M_PI * pow<3>(e.r) / 3.0;
  47. std::cout << std::fixed << std::setprecision(2) << 10.0 * e.mass * e.APF / 6.022 / V << std::endl;
  48. break;
  49. }
  50. }
  51. }
  52. return 0;
  53. }

81-90

上楼梯

  1. #include <stdio.h>
  2. #include <string.h>
  3. int main() {
  4. int n, m;
  5. scanf("%d %d", &n, &m);
  6. long long dp[n + 1];
  7. memset(dp, -1, (n + 1) * sizeof(long long));
  8. for (int i = 0; i < m; ++i) {
  9. int tmp;
  10. scanf("%d", &tmp);
  11. dp[tmp] = 0;
  12. }
  13. dp[1] = dp[1] ? 1 : 0;
  14. dp[2] = dp[2] ? (dp[1] ? 2 : 1) : 0;
  15. for (int i = 3; i <= n; ++i)
  16. if(dp[i]) dp[i] = (dp[i - 1] + dp [i - 2]) % (long long) (1e9 + 7);
  17. printf("%lld\n", dp[n]);
  18. return 0;
  19. }

绝对差

  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. void quickSort(int arr[], int left, int right) {
  6. if (left >= right) return;
  7. srand(time(NULL));
  8. int idx = rand() % (left - right) + left;
  9. int flag = arr[idx], head = left - 1, tail = right + 1;
  10. while (head < tail) {
  11. do head++; while(arr[head] < flag);
  12. do tail--; while(arr[tail] > flag);
  13. if (head < tail) {
  14. int tmp = arr[head];
  15. arr[head] = arr[tail];
  16. arr[tail] = tmp;
  17. }
  18. }
  19. quickSort(arr, left, tail);
  20. quickSort(arr, tail + 1, right);
  21. }
  22. int minAbsSub(int arr[], int n) {
  23. int min = INT_MAX;
  24. for (int i = 0; i < n - 1; ++i) {
  25. int tmp = arr[i + 1] - arr[i];
  26. if (tmp < min) min = tmp;
  27. }
  28. return min;
  29. }
  30. int main() {
  31. int n;
  32. scanf("%d", &n);
  33. int arr[n];
  34. for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);
  35. quickSort(arr, 0, n - 1);
  36. printf("%d", minAbsSub(arr, n));
  37. return 0;
  38. }

挑选

  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. void quickSort(long long arr[], long long left, long long right) {
  6. if (left >= right) return;
  7. srand(time(NULL));
  8. long long idx = rand() % (left - right) + left;
  9. long long flag = arr[idx], head = left - 1, tail = right + 1;
  10. while (head < tail) {
  11. do head++; while(arr[head] < flag);
  12. do tail--; while(arr[tail] > flag);
  13. if (head < tail) {
  14. long long tmp = arr[head];
  15. arr[head] = arr[tail];
  16. arr[tail] = tmp;
  17. }
  18. }
  19. quickSort(arr, left, tail);
  20. quickSort(arr, tail + 1, right);
  21. }
  22. long long maxi(long long arr[], long long n) {
  23. long long max = LLONG_MIN;
  24. for (long long i = 0; i < n; ++i)
  25. if (max < arr[i]) max = arr[i];
  26. return max;
  27. }
  28. long long sumMax(long long arr[], long long n) {
  29. long long dp[n];
  30. dp[0] = arr[0];
  31. for (long long i = 1; i < n; ++i) {
  32. if (arr[i] == arr[i - 1])
  33. dp[i] = dp[i - 1] > (arr[i] + dp[i - 1]) ? dp[i - 1] : (arr[i] + dp[i - 1]);
  34. else {
  35. long long j = i - 1;
  36. while (j >= 0 && arr[j] >= arr[i] - 1) --j;
  37. dp[i] = arr[i] + (j >= 0 ? dp[j] : 0);
  38. }
  39. }
  40. return maxi(dp, n);
  41. }
  42. int main() {
  43. long long n;
  44. scanf("%lld", &n);
  45. long long arr[n];
  46. for (long long i = 0; i < n; ++i) scanf("%lld", &arr[i]);
  47. quickSort(arr, 0, n - 1);
  48. printf("%lld\n", sumMax(arr, n));
  49. return 0;
  50. }

三角形

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. void quickSort(int arr[], int left, int right) {
  5. if (left >= right) return;
  6. srand(time(NULL));
  7. int idx = rand() % (left - right) + left;
  8. int flag = arr[idx], head = left - 1, tail = right + 1;
  9. while (head < tail) {
  10. do head++; while(arr[head] < flag);
  11. do tail--; while(arr[tail] > flag);
  12. if (head < tail) {
  13. int tmp = arr[head];
  14. arr[head] = arr[tail];
  15. arr[tail] = tmp;
  16. }
  17. }
  18. quickSort(arr, left, tail);
  19. quickSort(arr, tail + 1, right);
  20. }
  21. void findTri(int arr[], int n) {
  22. for (int i = n - 1; i > 1; --i)
  23. if (arr[i] < arr[i - 1] + arr[i - 2]) {
  24. printf("%d %d %d\n", arr[i - 2], arr[i - 1], arr[i]);
  25. return;
  26. }
  27. printf("-1\n");
  28. }
  29. int main() {
  30. int n;
  31. scanf("%d", &n);
  32. int arr[n];
  33. for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);
  34. quickSort(arr, 0, n - 1);
  35. findTri(arr, n);
  36. return 0;
  37. }

子数组最大和

  1. #include <stdio.h>
  2. #include <string.h>
  3. int maxSum(int arr[], int n) {
  4. int dp[n];
  5. memset(dp, 0, n * sizeof(int));
  6. int maxi = arr[0];
  7. dp[0] = arr[0];
  8. for (int i = 1; i < n; ++i) {
  9. dp[i] = arr[i] > (dp[i - 1] + arr[i]) ? arr[i] : (dp[i - 1] + arr[i]);
  10. maxi = dp[i] > maxi ? dp[i] : maxi;
  11. }
  12. return maxi;
  13. }
  14. int main() {
  15. int n;
  16. scanf("%d", &n);
  17. int arr[n];
  18. for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);
  19. printf("%d\n", maxSum(arr, n));
  20. return 0;
  21. }

和字符串

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. long long substrToNum(char str[], int pos, int len) {
  5. long long num = 0;
  6. for (int i = 0; i < len; ++i)
  7. num = num * 10 + str[pos + i] - '0';
  8. return num;
  9. }
  10. long long getLen(long long n) {
  11. int cnt = 0;
  12. do ++cnt, n /= 10; while(n);
  13. return cnt;
  14. }
  15. bool backTracking(char str[], int strLen, int begin, int len1, int len2) {
  16. if (begin + len1 + len2 >= strLen) return true;
  17. long long num1 = substrToNum(str, begin, len1);
  18. long long num2 = substrToNum(str, begin + len1, len2);
  19. long long num3 = substrToNum(str, begin + len1 + len2, getLen(num1 + num2));
  20. if (num1 + num2 == num3) return backTracking(str, strLen, begin + getLen(num1), getLen(num2), getLen(num3));
  21. return false;
  22. }
  23. void partition(char str[]) {
  24. int strLen = strlen(str);
  25. for (int i = 1; i <= strLen / 2; ++i) {
  26. if (backTracking (str, strLen, 0, i, i)) {
  27. printf("true\n");
  28. return;
  29. }
  30. }
  31. printf("false\n");
  32. }
  33. int main() {
  34. char str[1000] = "";
  35. scanf("%s", str);
  36. partition(str);
  37. return 0;
  38. }

汤包

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. typedef struct {
  5. int guest;
  6. int end;
  7. } Tag;
  8. void quickSort(Tag* arr[], int left, int right) {
  9. if (left >= right) return;
  10. srand(time(NULL));
  11. int idx = rand() % (left - right) + left;
  12. int flag = arr[idx]->end, head = left - 1, tail = right + 1;
  13. while (head < tail) {
  14. do head++; while(arr[head]->end < flag);
  15. do tail--; while(arr[tail]->end > flag);
  16. if (head < tail) {
  17. Tag *tmp = arr[head];
  18. arr[head] = arr[tail];
  19. arr[tail] = tmp;
  20. }
  21. }
  22. quickSort(arr, left, tail);
  23. quickSort(arr, tail + 1, right);
  24. }
  25. void traverse(Tag *arr[], int n) {
  26. for (int i = 0; i < n; ++i)
  27. printf("%d ", arr[i]->guest);
  28. }
  29. int main() {
  30. int n;
  31. scanf("%d", &n);
  32. Tag *arr[n];
  33. for (int i = 0; i < n; ++i) {
  34. arr[i] = (Tag*)malloc(sizeof(Tag));
  35. arr[i]->guest = i + 1;
  36. int begin, duration;
  37. scanf("%d %d", &begin, &duration);
  38. arr[i]->end = begin + duration;
  39. }
  40. quickSort(arr, 0, n -1);
  41. traverse(arr, n);
  42. return 0;
  43. }

打字机

  1. #include <stdio.h>
  2. #include <string.h>
  3. long long dp[100];
  4. long long seg[100] = {0};
  5. long long method(char str[]) {
  6. memset(dp, -1, 100 * sizeof(long long));
  7. char *iter = str;
  8. int part = 0, ans = 1;
  9. seg[part] = 1;
  10. while (*iter) {
  11. if (*iter == 'm' || *iter == 'w') return 0;
  12. if (*iter == 'n' && *(iter + 1) == 'n') {
  13. int cnt = 1;
  14. dp[0] = 1, dp[1] = 2, iter += 2;
  15. while(*iter == 'n') {
  16. ++cnt, ++iter;
  17. dp[cnt] = dp[cnt - 1] + dp[cnt - 2];
  18. }
  19. seg[++part] = dp[cnt];
  20. }
  21. else if (*iter == 'u' && *(iter + 1) == 'u') {
  22. int cnt = 1;
  23. dp[0] = 1, dp[1] = 2, iter += 2;
  24. while(*iter == 'u') {
  25. ++cnt, ++iter;
  26. dp[cnt] = dp[cnt - 1] + dp[cnt - 2];
  27. }
  28. seg[++part] = dp[cnt];
  29. }
  30. else ++iter;
  31. }
  32. for (int i = 0; seg[i] ; ++i) ans *= seg[i];
  33. return ans;
  34. }
  35. int main() {
  36. char str[1000];
  37. scanf("%s", str);
  38. printf("%lld", method(str));
  39. return 0;
  40. }

游乐园

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. int dist[12][12];
  5. int n, m;
  6. bool pass[12];
  7. int ans = -1;
  8. bool check(int v, int u) {
  9. return !pass[u] && dist[v][u] != 0x3f3f3f3f;
  10. }
  11. bool noway(int v) {
  12. for (int i = 0; i < n; ++i)
  13. if (check(v, i)) return false;
  14. return true;
  15. }
  16. void backTracking(int v, int l) {
  17. if (noway(v)) {
  18. ans = ans > l ? ans : l;
  19. return;
  20. }
  21. for (int i = 0; i < n; ++i) {
  22. if (check(v, i) && i != v) {
  23. pass[i] = true;
  24. backTracking(i, l + dist[v][i]);
  25. pass[i] = false;
  26. }
  27. }
  28. }
  29. int main() {
  30. scanf("%d %d", &n, &m);
  31. memset(dist, 0x3f, sizeof(dist));
  32. for (int i = 0; i < n; ++i) dist[i][i] = 0;
  33. while (m) {
  34. int v, u, l;
  35. scanf("%d %d %d", &v, &u, &l);
  36. v -= 1, u -= 1;
  37. dist[v][u] = dist[v][u] < l ? dist[v][u] : l;
  38. dist[u][v] = dist[v][u], --m;
  39. }
  40. for (int i = 0; i < n; ++i) {
  41. memset(pass, 0, sizeof(pass));
  42. pass[i] = true;
  43. backTracking(i, 0);
  44. }
  45. printf("%d\n", ans);
  46. return 0;
  47. }

危险的组合(CPP)

感谢 Cubeist 提供的代码~

  1. #include <iostream>
  2. using namespace std;
  3. long long w[] = {0, 0, 0, 1, 3, 8, 20, 47, 107, 238, 520, 1121,
  4. 2391, 5056, 10616, 22159, 46023, 95182, 196132, 402873, 825259,
  5. 1686408, 3438828, 6999071, 14221459, 28853662, 58462800,
  6. 118315137, 239186031, 483072832, 974791728};
  7. int main()
  8. {
  9. int n;
  10. while (cin >> n, n)
  11. {
  12. if (n <= 0) exit(0);
  13. cout << w[n] << endl;
  14. }
  15. return 0;
  16. }

91-100 考试模拟

【循环】圆周率 $\pi$

  1. #include <stdio.h>
  2. double pi(int n) {
  3. double sum = 3.0f;
  4. for (int i = 2; i <= n; ++i) {
  5. double sgn = i % 2 ? -1.0f : 1.0;
  6. sum += sgn * 4 / (2 * i * (2 * i - 1) * (2 * i - 2));
  7. }
  8. return sum;
  9. }
  10. int main() {
  11. int n;
  12. scanf("%d", &n);
  13. printf("%.7lf\n", pi(n));
  14. return 0;
  15. }

【选择】马赫数

  1. #include <stdio.h>
  2. #include <math.h>
  3. void mach(double v, double T) {
  4. double m = (v / 3.6f) / (331.3f * sqrt(1 + T / 273.15f));
  5. printf("%.3lf ", m);
  6. if (m - 0.8f < 1e-6) printf("subsonic\n");
  7. else if (m - 1.2f < 1e-6) printf("transonic\n");
  8. else if (m - 5.0f < 1e-6) printf("supersonic\n");
  9. else printf("hypersonic\n");
  10. }
  11. int main() {
  12. double v, T;
  13. scanf("%lf %lf", &v, &T);
  14. mach(v, T);
  15. return 0;
  16. }

【IO】气体扩散

  1. #include <stdio.h>
  2. #include <math.h>
  3. double rate(double m1, double m2) {
  4. return sqrt(m2 / m1);
  5. }
  6. int main() {
  7. double m1, m2;
  8. scanf("%lf %lf", &m1, &m2);
  9. printf("%.4lf\n", rate(m1, m2));
  10. return 0;
  11. }

【字符串】左右操作

  1. #include <stdio.h>
  2. #include <string.h>
  3. void quickSort(char str[], int left, int right) {
  4. if (left >= right) return;
  5. char flag = str[(left + right) / 2];
  6. int head = left - 1, tail = right + 1;
  7. while (head < tail) {
  8. do head++; while (str[head] > flag);
  9. do tail--; while (str[tail] < flag);
  10. if (head < tail) {
  11. char tmp = str[head];
  12. str[head] = str[tail], str[tail] = tmp;
  13. }
  14. }
  15. quickSort(str, left, tail);
  16. quickSort(str, tail + 1, right);
  17. }
  18. void reverse(char str[], int begin, int end) {
  19. int head = begin, tail = end;
  20. while (head <= tail) {
  21. char tmp = str[head];
  22. str[head] = str[tail], str[tail] = tmp;
  23. ++head, --tail;
  24. }
  25. }
  26. int main() {
  27. char str[1005] = "";
  28. scanf("%s", str);
  29. int len = strlen(str);
  30. quickSort(str, 0, len / 2 - 1);
  31. int mid = (len & 1) ? (len /2 + 1) : (len / 2);
  32. reverse(str, mid, len - 1);
  33. printf("%s", str);
  34. return 0;
  35. }

【结构体】空中交通管制

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. typedef struct {
  5. char id[64];
  6. int x;
  7. int y;
  8. } Plane;
  9. int main() {
  10. int n;
  11. scanf("%d", &n);
  12. Plane *plane[n];
  13. for (int i = 0; i < n; ++i) {
  14. plane[i] = (Plane *) malloc(sizeof(Plane));
  15. scanf("%s %d %d", plane[i]->id, &plane[i]->x, &plane[i]->y);
  16. }
  17. double minDist = 1e9;
  18. int idx1, idx2;
  19. for (int i = 0; i < n - 1; ++i) {
  20. for (int j = i + 1; j < n; ++j) {
  21. double dist = sqrt(pow(plane[i]->x - plane[j]->x, 2) +
  22. pow(plane[i]->y - plane[j]->y, 2));
  23. if (dist < minDist) idx1 = i, idx2 = j, minDist = dist;
  24. }
  25. }
  26. printf("%s-%s %.4lf", plane[idx1]->id, plane[idx2]->id, minDist);
  27. return 0;
  28. }
  29. /*
  30. 6
  31. UA057 2 3
  32. AA044 12 30
  33. BA1534 40 50
  34. DL262 5 1
  35. AF001 12 10
  36. SK837 3 4
  37. */

【数组】重复元素

  1. #include <stdio.h>
  2. int arr[1005] = {0};
  3. int main() {
  4. int n, cnt = 0;
  5. scanf("%d", &n);
  6. for (int i = 0; i < n; ++i) {
  7. scanf("%d", &arr[i]);
  8. for (int j = 0; j < i; ++j)
  9. if (arr[i] == arr[j]) {
  10. ++cnt;
  11. break;
  12. }
  13. }
  14. printf("%d\n", cnt);
  15. }
  16. //10 1 10 20 1 25 1 10 30 25 1

【文件】平方根

  1. #include <stdio.h>
  2. #include <math.h>
  3. int main() {
  4. int n;
  5. scanf("%d", &n);
  6. FILE *fp1 = fopen("rr.dat", "w");
  7. for (int i = 1; i <= n; ++i)
  8. fprintf(fp1, "%.6lf ", sqrt(i));
  9. fclose(fp1);
  10. FILE *fp2 = fopen("rr.dat", "r");
  11. for (int i = 1; i <= n; ++i) {
  12. double output;
  13. fscanf(fp2, "%lf", &output);
  14. printf("%.6lf ", output);
  15. }
  16. fclose(fp2);
  17. return 0;
  18. }

 【算法】零钞

  1. #include <stdio.h>
  2. int main() {
  3. int s;
  4. scanf("%d", &s);
  5. int cnt[4], r = s;
  6. cnt[0] = r / 10, r -= cnt[0] * 10;
  7. cnt[1] = r / 5, r -= cnt[1] * 5;
  8. cnt[2] = r / 2, cnt[3] = r - cnt[2] * 2;
  9. if (cnt[3]) printf("1=%d\n", cnt[3]);
  10. if (cnt[2]) printf("2=%d\n", cnt[2]);
  11. if (cnt[1]) printf("5=%d\n", cnt[1]);
  12. if (cnt[0]) printf("10=%d\n", cnt[0]);
  13. return 0;
  14. }

【枚举】机场翻牌显示

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. int cnt(char src, char dest) {
  4. if (src == dest) return 0;
  5. if (isupper(src))
  6. return (src < dest) ? (dest - src) : ('Z' - src + dest - 'A' + 1);
  7. if ('0' <= src && src <= '9') {
  8. /* if (src == '0') src = '9' + 1;
  9. if (dest == '0') dest = '9' + 1;
  10. return (src > dest) ? (src - dest) : (src - '0' + '9' + 1 - dest + 1);*/
  11. return (src < dest) ? (dest - src) : ('9' - src + dest - '0' + 1);
  12. }
  13. return -1;
  14. }
  15. int main() {
  16. char id1[10] = "", id2[10] = "";
  17. scanf("%s %s", id1, id2);
  18. int num = 0;
  19. for (int i = 0; id1[i] && id2[i] ; ++i) num += cnt(id1[i], id2[i]);
  20. printf("%d\n", num);
  21. return 0;
  22. }

【递归】阿克曼数

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/271774
推荐阅读
相关标签
  

闽ICP备14008679号