#include <stdio.h> int main() { bool logo = 1; char ch; while ((ch = getchar()) != EOF) { if (ch == '\"') { if (logo) printf("``"); else printf("''"); logo ^= 1; } else putchar(ch); } return 0; }第9~10行:我本来是用puts来输出``和‘‘,发现会自带换行符,所以用printf的%s输出,LRJ的代码是用了这技巧:q?"``":"‘‘"。
#include <stdio.h> char str[4][50] = {"`1234567890-=", "QWERTYUIOP[]\\", "ASDFGHJKL;'", "ZXCVBNM,./"}; int main() { int a[200], i, j; for (i = 0; i < 4; i++) for (j = 1; a[str[i][j]] = str[i][j-1]; j++); a[' '] = ' '; a['\t'] = '\t'; a['\n'] = '\n'; while ((i = getchar()) != EOF) putchar(a[i]); return 0; }第9行:用相当于“哈希映射”的方式,把每个输入字符(str),应该匹配的数值存到一个数组(a)。
#include <stdio.h> char str[200] = {"`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./ \t\t\n\n"}; int main() { int a[200], i; for (i = 1; a[str[i]] = str[i-1]; i++); while ((i = getchar()) != EOF) putchar(a[i]); return 0; }第2行:注意末尾的空格、制表符、回车三个字符各连写了两个,这样不用像上次那样多写了第10行。
#include <stdio.h> #include <string.h> char s[100], key1[] = "AEHIJLMOSTUVWXYZ12358", key2[] = "A3HILJMO2TUVMXY51SEZ8", *p; int ispal() { int i = 0, j = strlen(s) - 1; while (i < j) if (s[i++] != s[j--]) return 0; return 1; } int ismir() { int i = -1, j = strlen(s); while (++i <= --j) { p = strchr(key1, s[i]); if (p) { if (s[j] != key2[p-key1]) return 0;} else return 0; } return 1; } int main() { int t; while (scanf("%s", s) != EOF) { t = ismir(); printf("%s -- is ", s); if (ispal()) printf("%s", t?"a mirrored palindrome":"a regular palindrome"); else printf("%s", t?"a mirrored string":"not a palindrome"); printf(".\n\n"); } return 0; }关键是如何判断镜像性质:
#include <stdio.h> int y[100100]; int main() { for (int i = 0; i < 100000; i++) { int s = i, t = i; while (t) { s+=t%10; t/=10;} if (y[s] == 0) y[s] = i; } int T, pos; scanf("%d", &T); while (T--) { scanf("%d", &pos); printf("%d\n", y[pos]); } return 0; }
#include <stdio.h> #include <string.h> int main() { int T; scanf("%d", &T); while (T--) { char y[300]; scanf("%s", y); int len = strlen(y), i, min_p = 0; for (i = 0; i < len; i++) { y[i+len] = y[i]; y[i+len+1] = 0; if (strcmp(y+min_p,y+i+1) > 0) min_p = i+1; } y[min_p+len] = 0; printf("%s\n", y+min_p); } return 0; }
#include <stdio.h> int main() { int T; scanf("%d", &T); while (T--) { char s[100]; scanf("%s", s); int sum = 0, cnt = 0; for (int i = 0; s[i]; i++) { if (s[i]=='X') { sum += cnt*(cnt+1)/2; cnt = 0; } else cnt++; } printf("%d\n", sum+cnt*(cnt+1)/2); } return 0; }每遇到X做一次更新,否则cnt++,注意第19行最后还要一次更新。
#include <stdio.h> int main() { int T; scanf("%d", &T); while (T--) { char s[100]; scanf("%s", s); int sum = 0, cnt = 0; for (int i = 0; s[i]; i++) { if (s[i]=='X') cnt = 0; else { cnt++; sum += cnt; } } printf("%d\n", sum); } return 0; }
#include <stdio.h> #include <ctype.h> int main() { int T; scanf("%d", &T); while (T--) { char s[100]; scanf("%s", s); double sum = 0, w; for (int i = 0; s[i]; i++) { // 算出后两位所代表的数值 if (isdigit(s[i+1])) { if (isdigit(s[i+2])) w = 10*(s[i+1]-'0')+s[i+2]-'0'; else w = s[i+1] - '0'; } else w = 1; // 判断第一位 if (!isalpha(s[i])) continue; else if (s[i] == 'C') sum += 12.01*w; else if (s[i] == 'H') sum += 1.008*w; else if (s[i] == 'O') sum += 16.00*w; else sum += 14.01*w; } printf("%.3lf\n", sum); } return 0; }
#include <stdio.h> int main() { int T; scanf("%d", &T); while (T--) { int N, i, t, a[10] = {}; scanf("%d", &N); for (i = 1; i <= N; i++) { t = i; while (t) a[t%10]++, t/=10; } for (i = 0; i < 9; i++) printf("%d ", a[i]); printf("%d\n", a[9]); } return 0; }直接每组暴力都能过。如果会TLE,也可以建立二维数组a[10000][10],先暴力一轮求出数据。后面只要打表就行了。
#include <stdio.h> #include <string.h> int isTthePeriod(char s[], int T) { for (int i = 0; i < T; i++) { for (int j = i+T; j < strlen(s); j+=T) if (s[i] != s[j]) return 0; } return 1; } int main() { int T, kase, len, i; scanf("%d", &T); for (kase = 1; kase <= T; kase++) { char s[100]; while (scanf("%s", s), len = strlen(s), !len); for (i = 1; i <= len; i++) if (len%i == 0 && isTthePeriod(s,i)) break; if (kase != 1) putchar('\n'); printf("%d\n", i); } return 0; }
【基本框架】 |
将5*5网格定义为全局变量s(从1开始编号),当前空格所在行列为x,y |
output函数用来输出s的矩阵形式 |
ok用来操作一个命令为ch的移动,返回值1代表操作成功,返回0代表失败。成功的话,要更新相应的x、y和进行交换操作swap |
test用来完成每组测试的操作 |
#include <cstdio> #include <algorithm> using namespace std; int x, y; char s[6][6]; void output() { for (int i = 1; i <= 5; i++) { for (int j = 1; j < 5; j++) printf("%c ", s[i][j]); printf("%c\n", s[i][5]); } } int ok(char ch) { switch (ch) { case 'A': if (x == 1) return 0; else x--; swap(s[x][y], s[x+1][y]); break; case 'B': if (x == 5) return 0; else x++; swap(s[x][y], s[x-1][y]); break; case 'L': if (y == 1) return 0; else y--; swap(s[x][y], s[x][y+1]); break; case 'R': if (y == 5) return 0; else y++; swap(s[x][y], s[x][y-1]); break; } } int test() { char ch; int i, j; for (i = 1; i <= 5; i++) for (j = 1; j <= 5; j++) if (s[i][j] == ' ') x = i, y = j; while ((ch = getchar()) != '0') { if (ch == '\n') continue; if (!ok(ch)) { // 非法操作,仍要清掉字符'0'前的值 while (getchar() != '0'); return 0; } } return 1; } int main() { int kase, i, j; for (kase = 1; ; kase++) { gets(&s[1][1]); if (s[1][1] == 0) {kase--; continue;} // 注意此处读到空串,要忽略读入continue if (s[1][1] == 'Z' && s[1][2] == 0) break; if (kase != 1) putchar('\n'); printf("Puzzle #%d:\n", kase); for (i = 2; i <= 5; i++) gets(&s[i][1]); if (test()) output(); else printf("This puzzle has no final configuration.\n"); } return 0; }
#include <stdio.h> #include <string.h> int main() { int n, m, a[15][15], k, i, j; char s[15][15]; for (int kase = 1; ; kase++) { if (scanf("%d%d ", &n, &m) != 2) break; k = 0; memset(a, 0, sizeof(a)); memset(s, 0, sizeof(s)); for (i = 1; i <= n; i++) gets(&s[i][1]); for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { if (s[i][j] == '*') s[i][j] = 0; if (!(s[i-1][j]&&s[i][j-1]) && s[i][j]) a[i][j] = ++k; } if (kase != 1) putchar('\n'); printf("puzzle #%d:\nAcross\n", kase); for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) if (s[i][j] && !s[i][j-1]) printf("%3d.%s\n", a[i][j], s[i]+j); printf("Down\n"); for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { if (s[i][j] && !s[i-1][j]) { printf("%3d.", a[i][j]); for (k = i; s[k][j]; k++) putchar(s[k][j]); putchar('\n'); } } } return 0; }
#include <stdio.h> #include <string.h> inline int c2d(char c) { if (c == 'A') return 0; else if (c == 'C') return 1; else if (c == 'G') return 2; else return 3; } inline int d2c(int d) { if (d == 0) return 'A'; else if (d == 1) return 'C'; else if (d == 2) return 'G'; else return 'T'; } int main() { int T, m, n, i, j, a[1010][4], sum; char s[50][1010]; scanf("%d", &T); while (scanf("%d%d ", &m, &n) == 2) { for (i = 0; i < m; i++) gets(s[i]); memset(a, 0, sizeof(a)); for (i = 0; i < m; i++) for (j = 0; j < n; j++) a[j][ c2d(s[i][j]) ]++; for (sum = j = 0; j < n; j++) { int the_max = a[j][0], id = 0; for (i = 1; i < 4; i++) if (a[j][i]>the_max) id = i, the_max = a[j][i]; putchar(d2c(id)); sum += m - the_max; } printf("\n%d\n", sum); } return 0; }
#include <stdio.h> int A[2][3010], p, q; // 找i位之前是否有重复分子出现;有则返回周期,否则返回-1. inline int myfind() { for (p = 1; p < q; p++) { if (A[0][p] == A[0][q]) return q - p; } return -1; } int main() { int kase, a, b, i, len; for (kase = 1; scanf("%d%d", &a, &b) != EOF; kase++) { A[0][0] = a; A[1][0] = a/b; for (q = 1; ; q++) { A[0][q] = (A[0][q-1] - b*A[1][q-1])*10; A[1][q] = A[0][q]/b; len = myfind(); if (len > 0) break; } printf("%d/%d = %d.", a, b, A[1][0]); for (i = 1; i < p; i++) printf("%d", A[1][i]); putchar('('); for (i = p; i < q; i++) { if (i > 50) { printf("..."); break; } printf("%d", A[1][i]); } printf(")\n %d = number of digits in repeating cycle\n\n", len); } }
#include <string> #include <iostream> using namespace std; int fun(string& a, string& b) { int i, j = 0; for (i = 0; i < a.size(); i++, j++) { while (j < b.size() && a[i]!=b[j]) j++; if (j == b.size()) return 0; } return 1; } int main() { string a, b; while (cin >> a >> b) { if (fun(a,b)) cout << "Yes\n"; else cout << "No\n"; } }
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct Face{ int h, w; void make(int x, int y) { h = min(x,y); w = max(x,y); } bool operator < (const Face& b) const { if (h == b.h) return w < b.w; else return h < b.h; } bool operator == (const Face& b) const { return (h == b.h) && (w == b.w); } }; vector<Face> A(6); int test1() { for (int i = 0; i < 6; i += 2) if (!(A[i]==A[i+1])) return 0; return 1; } int test2() { if (A[0].h==A[2].h && A[0].w==A[4].h && A[2].w==A[4].w) return 1; else return 0; } int main() { int x, y; while (cin >> x >> y) { A[0].make(x, y); for (int i = 1; i < 6; i++) { cin >> x >> y; A[i].make(x, y); } sort(A.begin(), A.end()); if (test1() && test2()) cout << "POSSIBLE\n"; else cout << "IMPOSSIBLE\n"; } return 0; }
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int len1, len2; char s1[110], s2[110]; int test(int k, char s1[], char s2[]) { for (int i = 0; s1[k+i] && s2[i]; i++) if (s1[k+i]+s2[i]-2*'0' > 3) return 0; return 1; } int fun(char s1[], char s2[]) { int k = 0; while (!test(k,s1,s2)) k++; return max(strlen(s1), strlen(s2)+k); } int main() { while (scanf("%s%s", s1, s2) != EOF) { printf("%d\n", min(fun(s1,s2), fun(s2,s1))); } return 0; }
#include <math.h> #include <stdio.h> #include <string.h> double C[10][31]; void init() { int i, j, v; double f[10] = {0.5}, g[31], t; for (i = 1, t = 0.5; i < 10; i++) { f[i] = f[i-1] + t/2; t /= 2; } for (i = 0; i < 10; i++) f[i] = log10(f[i]); for (i = 1, v = 2; i <= 30; i++) { g[i] = (v - 1.0)*log10(2.0); v <<= 1; } for (i = 0; i < 10; i++) for (j = 1; j <= 30; j++) C[i][j] = f[i] + g[j]; } int main() { int i, j; char s[100], *p; double A, B; init(); while (scanf("%s", s), strcmp(s, "0e0")) { p = strchr(s, 'e'); *p = 0; // 将'e'所在位置变为'\0' sscanf(s, "%lf", &A); sscanf(p+1, "%lf", &B); int pi = 0, pj = 1; B = A = log10(A) + B; // 接下来A存储表达式值,B记录差值 for (i = 0; i < 10; i++) for (j = 1; j <= 30; j++) if (fabs(A-C[i][j]) < B) pi = i, pj = j, B = fabs(A-C[i][j]); printf("%d %d\n", pi, pj); } return 0; }
原文地址:http://blog.csdn.net/code4101/article/details/38303607