USTCOJ 1361-1363题目比较简单,就不多说了。其中值得留意的是gets函数会读入'\r'(如果有的话),处理方法详见1362。
1361开灯问题:http://acm.ustc.edu.cn/ustcoj/problem.php?id=1361
#include <stdio.h> #include <string.h> void reverseFlag( char* flag ) { *flag = (*flag) ? 0 : 1; } main() { int n, k; while (scanf("%d%d", &n, &k) , n + k) { int i; char flag[1001]; memset(flag, 1, n + 1); for (i = 2; i <= k; i++) { int t = i; while (t <= n ) { reverseFlag(flag+t); t += i; } } printf("1"); for (i = 2; i <= n; i++) if (flag[i]) printf(" %d", i); printf("\n"); } return 0; }
1362 单词求值:http://acm.ustc.edu.cn/ustcoj/problem.php?id=1362
#include <stdio.h> #include <string.h> //由于在线评测系统的输入是重定向到文件的, //当输入文件中换行是"\r\n"时,gets函数会读入'\r' void strip(char *str) { char *p; if ((p = strchr(str, '\r')) != NULL) *p = '\0'; } main() { char str[260] = {0}; //使用gets函数读入带空格的字符串 while (gets(str+1), str[1] != '#') { strip(str); int i, sum = 0; for (i = 1; str[i]; i++) { if (str[i] == ' ') continue; else sum += i * (str[i] - 'A' + 1); } printf("%d\n", sum); } return 0; }
1363 幸福配对:http://acm.ustc.edu.cn/ustcoj/problem.php?id=1363
#include <stdio.h> main() { int T; scanf("%d", &T); while (T--) { int n, a, b, c; scanf("%d", &n); a = b = c = 0; while (n--) { int t; scanf("%d", &t); switch (t % 3) { case 0: a++; break; case 1: b++; break; case 2: c++; break; default: break; } } a /= 2; b = (b > c) ? c : b; printf("%d\n", a + b); } return 0; }