#include <stdio.h> #include <stdlib.h> #include <string.h> int cmp(const void* a,const void* b) { return *(int *)a - *(int *)b; } int main() { char s1[110], s2[110]; while (scanf("%s%s", s1, s2) != EOF) { int a[26] = {}, b[26] = {}, i; for (i = 0; s1[i]; i++) a[s1[i]-'A']++; for (i = 0; s2[i]; i++) b[s2[i]-'A']++; qsort(a, 26, sizeof(int), cmp); qsort(b, 26, sizeof(int), cmp); printf("%s\n", memcmp(a, b, sizeof(a))?"NO":"YES"); } return 0; }
#include <stdio.h> int main() { int t, i, c; char s[1000]; bool vis[26], has[26]; while (scanf("%d ", &t), t != -1) { for (i = 0; i < 26; i++) vis[i] = has[i] = 0; scanf("%s", s); for (i = 0; s[i]; i++) has[s[i]-'a'] = 1; for (i = 0; i < 26; i++) vis[i] = has[i]; scanf("%s", s); for (c = i = 0; s[i]; i++) { if (has[s[i]-'a']) vis[s[i]-'a'] = 0; else c++; if (c == 7) break; } for (i = 0; i < 26; i++) if (vis[i]) break; printf("Round %d\n", t); if (i == 26 && c <= 7) printf("You win.\n"); else if (i < 26 && c < 7) printf("You chickened out.\n"); else printf("You lose.\n"); } return 0; }
#include <stdio.h> int n, k, m, a[25]; int go(int p, int d, int t) { while (t--) { do { p = (p+d+n-1) % n + 1; } while (a[p] == 0); } return p; } int main() { while (scanf("%d%d%d", &n, &k, &m), n&&k&&m) { for (int i = 1; i <= n; i++) a[i] = i; int left = n; int p1 = n, p2 =1; while (left) { p1 = go(p1, 1, k); p2 = go(p2, -1, m); printf("%3d", p1); left--; if (p2 != p1) { printf("%3d", p2); left--;} a[p1] = a[p2] = 0; if (left) printf(","); } printf("\n"); } return 0; }
#include <stdio.h> #include <string.h> char code[8][1<<8], s[1<<8]; // 过滤空行,返回字符串长度 int my_gets(char* s) { int k; while ((k = getchar()) == '\n'); // 空行重读 if (k == -1) k = 0; else gets(s+1); s[0] = k; return strlen(s); } void init() { int i, j, k = 0; for (i = 1; ; i++) { for (j = 0; j < (1<<i)-1; j++) { code[i][j] = s[k++]; if (!s[k]) return; } } } // 读取01字符,返回0、1整型值 int read() { char ch; while ((ch = getchar()) == '\n'); return ch - '0'; } // 读取c个01字符 int readint(int c) { int v = 0; while (c--) v = (v<<1)+read(); return v; } int main() { int len, v; while (my_gets(s)) { init(); while (len = readint(3)) while ((v = readint(len)) != ((1<<len)-1)) putchar(code[len][v]); putchar('\n'); } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #define FOR() for (int i = 1; i <= R; i++) for (int j = 1; j <= C; j++) int R, C, D[51][51][2]; int cmp(const void* a, const void* b) {return *(int *)a-*(int *)b;} void init() { FOR() D[i][j][0] = i, D[i][j][1] = j; } void EX() { int r1, c1, r2, c2; scanf("%d%d%d%d", &r1, &c1, &r2, &c2); FOR() if (D[i][j][0]==r1&&D[i][j][1]==c1) {D[i][j][0] = r2; D[i][j][1] = c2;} else if (D[i][j][0]==r2&&D[i][j][1]==c2) {D[i][j][0] = r1; D[i][j][1] = c1;} } void DR(int r) { FOR() { if (D[i][j][0] == r) D[i][j][0] = 0; else if (D[i][j][0] > r) D[i][j][0]--; } } void DC(int c) { FOR() { if (D[i][j][1] == c) D[i][j][0] = 0; else if (D[i][j][1] > c) D[i][j][1]--; } } void IR(int r) { FOR() if (D[i][j][0] >= r) D[i][j][0]++; } void IC(int c) { FOR() if (D[i][j][1] >= c) D[i][j][1]++; } int main() { int kase, cnt, i, j, r, c, a[15], k; char cmd[5]; for (kase = 1; scanf("%d%d",&R,&C), R; kase++) { init(); scanf("%d", &cnt); for (i = 0; i < cnt; i++) { scanf("%s", cmd); if (!strcmp(cmd,"EX")) EX(); else { scanf("%d", &k); for (j = 0; j < k; j++) scanf("%d", a+j); qsort(a, k, sizeof(int), cmp); int dt = 1; void (*pf)(int); // 函数指针 if (!strcmp(cmd,"DR")) pf = DR, dt = -1; else if (!strcmp(cmd,"DC")) pf = DC, dt = -1; else if (!strcmp(cmd,"IR")) pf = IR; else pf = IC; for (j = 0; j < k; j++) pf(a[j]+dt*j); } } scanf("%d", &cnt); if (kase != 1) putchar('\n'); printf("Spreadsheet #%d\n", kase); for (i = 0; i < cnt; i++) { scanf("%d%d", &r, &c); printf("Cell data in (%d,%d) ", r, c); if (D[r][c][0]) printf("moved to (%d,%d)\n", D[r][c][0], D[r][c][1]); else puts("GONE"); } } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #define maxn 105 const double eps = 1e-5; // 比保留2位有效数字多3个数量级 struct Student{ char SID[50], name[50]; int CID, sub[4], sco;// sub(subject),下标0123分别代表语数英编程成绩 }stu[maxn], *p; int sum = 0, sco[maxn]; void menu() { char s[6][50] = {"Add", "Remove", "Query", "Show ranking", "Show Statistics", "Exit"}; printf("Welcome to Student Performance Management System (SPMS).\n\n"); for (int i = 1; i <= 6; i++) printf("%d - %s\n", i%6, s[i-1]); putchar('\n'); } Student* find_SID(char* SID) { for (int i = 0; i < sum; i++) if (!strcmp(SID, stu[i].SID)) return stu+i; return NULL; } void Add() { while (puts("Please enter the SID, CID, name and four scores. Enter 0 to finish."), p = stu+sum, scanf("%s", p->SID), strcmp(p->SID, "0")) { scanf("%d%s", &p->CID, p->name); for (int i = 0; i < 4; i++) scanf("%d", &p->sub[i]); if (find_SID(p->SID) != NULL) puts("Duplicated SID."); else sum++; } } void Remove() { char s[50]; while (puts("Please enter SID or name. Enter 0 to finish."), scanf("%s", s), strcmp(s, "0")) { int xx = 0; for (int i = 0; i < sum; i++) if (!strcmp(s,stu[i].SID) || !strcmp(s,stu[i].name)) { sum--; xx++; for (int j = i; j < sum; j++) stu[j] = stu[j+1]; i--; } printf("%d student(s) removed.\n", xx); } } int cmp(const void* a, const void* b) { return *(int*)b-*(int*)a;} int ranklist(int t) { int i = -1; while (sco[++i]>t); return i+1; } void Query() { int i, j; char s[50]; for (i = 0; i < sum; i++) { stu[i].sco = stu[i].sub[0]; for (j = 1; j < 4; j++) stu[i].sco += stu[i].sub[j]; sco[i] = stu[i].sco; } qsort(sco, sum, sizeof(int), cmp); while (puts("Please enter SID or name. Enter 0 to finish."), scanf("%s", s), strcmp(s, "0")) { for (i = 0; i < sum; i++) if (!strcmp(s,stu[i].SID) || !strcmp(s,stu[i].name)) { printf("%d %s %d %s", ranklist(stu[i].sco), stu[i].SID, stu[i].CID, stu[i].name); for (j = 0; j < 4; j++) printf(" %d", stu[i].sub[j]); printf(" %d %.2lf\n", stu[i].sco, stu[i].sco/4.0 + eps); } } } void Show_ranking() { puts("Showing the ranklist hurts students' self-esteem. Don't do that."); } void Show_Statistics() { puts("Please enter class ID, 0 for the whole statistics."); // c[i]:第i门课有多少人合格; cnt[i]:过i门课恰好有多少人 int r, c[4] = {}, cnt[5] = {}; int i, j, n = 0, s[4] = {}, cid; // n人数,s[i]:第i门课的总分,cid班级编号 scanf("%d", &cid); for (i = 0; i < sum; i++) if (!cid || stu[i].CID == cid) { n++; for (r = j = 0; j < 4; j++) { s[j] += stu[i].sub[j]; if (stu[i].sub[j] >= 60) { r++; c[j]++; } } cnt[r]++; } char str[4][50] = {"Chinese", "Mathematics", "English", "Programming"}; for (i = 0; i < 4; i++) { printf("%s\nAverage Score: %.2lf\n", str[i], 1.0*s[i]/n + eps); printf("Number of passed students: %d\nNumber of failed students: %d\n\n", c[i], n-c[i]); } printf("Overall:\nNumber of students who passed all subjects: %d\n", cnt[4]); for (i = 3; i > 0; i--) printf("Number of students who passed %d or more subjects: %d\n", i, cnt[i]+=cnt[i+1]); printf("Number of students who failed all subjects: %d\n\n", n - cnt[1]); } int main() { #ifdef DEBUG freopen("12412.in", "r", stdin); freopen("12412.ans", "w", stdout); #endif int c; while (menu(), scanf("%d", &c), c) { if (c == 1) Add(); else if (c == 2) Remove(); else if (c == 3) Query(); else if (c == 4) Show_ranking(); else if (c == 5) Show_Statistics(); } return 0; }最后看LRJ的代码,竟然没有用结构体解!
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> using namespace std; struct Xiangqi { int board[11][10]; int ax, ay, bx, by; // a是红方的帅,b是黑方的将; x,y是坐标 // 构造函数遵循题目的输入格式 Xiangqi(int n, int _bx, int _by): bx(_bx), by(_by) { memset(board, 0, sizeof(board)); board[bx][by] = 'Y'; // 用字母Y代表黑将 char type[5]; int x, y; for (int i = 0; i < n; i++) { scanf("%s%d%d", type, &x, &y); board[x][y] = type[0]; } } // 黑将做(dx,dy)的移动,移动成功则返回1,否则0 int go(int dx, int dy) { if (bx+dx>0 && bx+dx<4 && by+dy>3 && by+dy<7) { board[bx][by] = 0; bx += dx; by += dy; board[bx][by] = 'Y'; return 1; } return 0; } // 判断(x,y)到(bx,by)中间一共有多少棋子,重叠与不在同一竖直、水平线返回-1 int Cnt(int x, int y) { int cnt = -1, i, j; if (x == bx && y != by) for (cnt = 0, j = min(y,by)+1; j < max(y,by); j++) if (board[x][j]) cnt++; if (x != bx && y == by) for (cnt = 0, i = min(x,bx)+1; i < max(x,bx); i++) if (board[i][y]) cnt++; return cnt; } // 判断在(x,y)的马能不能吃掉黑将 int H(int x, int y) { int dx = bx - x, dy = by - y; if (abs(dx*dy) != 2) return 0; if (abs(dx)==2) if (board[x+dx/2][y]) return 0; if (abs(dy)==2) if (board[x][y+dy/2]) return 0; return 1; } // 判断如果当前棋盘红方移动,黑方是否被将死 int isBlackLost() { for (int i = 1; i <= 10; i++) for (int j = 1; j <= 9; j++) { switch (board[i][j]) { case 'G': case 'R': if (Cnt(i,j)==0) return 1; break; case 'H': if (H(i,j)) return 1; break; case 'C': if (Cnt(i,j)==1) return 1; } } return 0; } void out() { // 用于输出棋盘进行调试的函数 printf("**123456789\n"); for (int i = 1; i <= 10; i++) { printf("%2d", i); for (int j = 1; j <= 9; j++) { putchar(board[i][j]?board[i][j]:' '); } putchar('\n'); } putchar('\n'); } }; int isCheckmate(Xiangqi& A) { int dx[4] = {-1,1,0,0}, dy[4] = {0,0,1,-1}; for (int i = 0; i < 4; i++) { Xiangqi B(A); // 用复制构造函数拷贝副本 if (B.go(dx[i],dy[i]) && !B.isBlackLost()) return 0; } return 1; } int main() { #ifdef DEBUG freopen("1589.in", "r", stdin); #endif // DEBUG int n, bx, by; while (scanf("%d%d%d", &n, &bx, &by), n) { Xiangqi A(n, bx, by); //A.out(); //cout << A.Cnt(6,4) << endl; //cout << A.isBlackLost() << endl; //A.out(); //printf("答案: %d\n", isCheckmate(A)); printf("%s\n", isCheckmate(A)?"YES":"NO"); } return 0; }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; struct Squares { int h[10][10], v[10][10]; int n, cnt[10]; // 接下来判断(x1,y1)-(x2,y2)能否连环 int isSqu(int x1, int y1, int x2, int y2) { int px = x1, py = y1; while (py < y2) { if (!h[px][py]) return 0; py++; } while (px < x2) { if (!v[px][py]) return 0; px++; } while (py > y1) { if (!h[px][py-1]) return 0; py--; } while (px > x1) { if (!v[px-1][py]) return 0; px--; } return 1; } void Count() { int i, j, k; memset(cnt, 0, sizeof(cnt)); for (i = 1; i < n; i++) for (j = 1; j < n; j++) for (k = 1; i+k<=n && j+k<=n; k++) if (isSqu(i,j,i+k,j+k)) cnt[k]++; } Squares(int _n, int m):n(_n) { memset(h, 0, sizeof(h)); memset(v, 0, sizeof(v)); char str[10]; int x, y, i; for (i = 0; i < m; i++) { scanf("%s%d%d", str, &x, &y); if (!strcmp(str, "H")) h[x][y] = 1; else v[y][x] = 1; } Count(); } void Msg() { int logo = 1; for (int i = 1; i < n; i++) if (cnt[i]) { logo = 0; printf("%d square (s) of size %d\n", cnt[i], i); } if (logo) puts("No completed squares can be found."); } }; int main() { int n, m, kase = 0; while (cin >> n >> m) { Squares A(n, m); if (kase) puts("\n**********************************\n"); printf("Problem #%d\n\n", ++kase); A.Msg(); } return 0; }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; struct Othello { char board[10][10], cur; Othello() { memset(board, 0, sizeof(board)); for (int i = 1; i < 9; i++) scanf("%s", &board[i][1]); char str[5]; scanf("%s", str); cur = str[0]; } char NextCur() { return cur=='B'?'W':'B'; } void OutBoard() { for (int i = 1; i < 9; i++) puts(&board[i][1]); } void List() { int cnt = 0, i, j; for (i = 1; i < 9; i++) for (j = 1; j < 9; j++) if (board[i][j] == '-' && isMakeOk(i,j)) { if (cnt) putchar(' '); cnt++; printf("(%d,%d)", i, j); } if (cnt) putchar('\n'); else puts("No legal move."); } void Count() { int b = 0, w = 0; for (int i = 1; i < 9; i++) for (int j = 1; j < 9; j++) if (board[i][j]=='B') b++; else if (board[i][j]=='W') w++; printf("Black -%3d White -%3d\n", b, w); } // 从(x,y)出发,沿dir所指方向查找. change确定要不要进行修改操作 //返回值表示该条路径是否可以进行删除 int walk(int x, int y, int dir, int change = 0) { static int dirx[8] = {-1,-1,0,1,1,1,0,-1}; static int diry[8] = {0,1,1,1,0,-1,-1,-1}; int dx = dirx[dir], dy = diry[dir], k = 0; char ch1 = NextCur(), ch; //ch1存与cur对应的另一种字符 do { k++; ch = board[x+k*dx][y+k*dy]; if (ch == '_' || !ch) return 0; } while (ch == ch1); if (k == 1 || ch != cur) return 0; if (change) for (int i = 1; i < k; i++) board[x+i*dx][y+i*dy] = cur; return 1; } // 当前棋子能否放在(x,y) int isMakeOk(int x, int y) { for (int i = 0; i < 8; i++) if (walk(x,y,i)) return 1; return 0; } void MakeMove(int x, int y) { if (!isMakeOk(x,y)) cur = NextCur(); board[x][y] = cur; for (int i = 0; i < 8; i++) walk(x,y,i,1); cur = NextCur(); Count(); } }; int main() { int T, kase; char c[10]; // choose cin >> T; for (kase = 1; kase <= T; kase++) { if (kase != 1) putchar('\n'); Othello A; while (scanf("%s", c), strcmp(c, "Q")) { if (c[0] == 'L') A.List(); else A.MakeMove(c[1]-'0', c[2]-'0'); } A.OutBoard(); } return 0; }
#include <iostream> #include <string> using namespace std; struct Cube { string s; Cube(string _s): s(_s) { } void tran(int* a, int k) { for (int i = 0; i < k; i++) { string t(6,0); for (int j = 0; j < 6; j++) t[a[j]] = s[j]; s = t; } } void up(int k = 1) { int a[6] = {4,0,2,3,5,1}; tran(a, ((k%4)+4)%4); } void right(int k = 1) { int a[6] = {0,3,1,4,2,5}; tran(a, ((k%4)+4)%4); } bool Equal(Cube A, Cube B) { for (int i = 0; i < 4; i++) { B.right(); if (A.s == B.s) return 1; } return 0; } bool operator == (Cube B) { string s0 = B.s; int r[6] = {0,0,1,1,0,0}, u[6] = {0,1,1,-1,-1,2}; for (int i = 0; i < 6; i++) { B.s = s0; B.right(r[i]); B.up(u[i]); if (Equal(*this, B)) return 1; } return 0; } }; int main() { string s; while (cin >> s) { Cube A(s.substr(0,6)), B(s.substr(6,6)); cout << (A==B?"TRUE":"FALSE") << "\n"; } return 0; }
#include <cstdio> #include <iostream> using namespace std; struct Network { unsigned int ip[1005], addr, mask; int n; void init() { int logo; mask = 0xffffffff; do { logo = 0; addr = ip[0]&mask; for (int i = 1; i < n; i++) if ((ip[i]&mask) != addr) logo = 1; if (logo) mask <<= 1; } while (logo); } Network(int _n): n(_n) { unsigned int a[4]; for (int i = 0; i < n; i++) { scanf("%u.%u.%u.%u", &a[0], &a[1], &a[2], &a[3]); ip[i] = (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; } init(); } }; void OutIp(unsigned a) { for (int i = 0; i < 3; i++) printf("%d.", (a>>(3-i)*8)&0xff); printf("%d\n", a&0xff); } int main() { int n; while (cin >> n) { Network A(n); OutIp(A.addr); OutIp(A.mask); } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <string> using namespace std; const int inf = 10000; map<char, string> unit_code; map<string, string> dict; int dist(string a, string b) { if (a == b) return 0; if (b.size() < a.size()) swap(a, b); // a必须是b的子串 if (a != b.substr(0,a.size())) return inf; else return b.size()-a.size(); } string encode(string& s) { string ans; for (int i = 0; i < s.size(); i++) ans = ans + unit_code[s[i]]; return ans; } string decode(string s) { map<string, string>::iterator it = dict.begin(); string ans = it->first; int dis = dist(it->second, s); while (++it != dict.end()) { int d = dist(it->second, s); if (d < dis) ans = it->first, dis = d; else if (d == dis && d==0 && ans[ans.size()-1] != '!') ans = ans + "!"; } if (dis) ans = ans + "?"; return ans; } int main() { string s1, s2; while (cin >> s1, s1 != "*") { cin >> s2; unit_code[s1[0]] = s2; } while (cin >> s1, s1 != "*") dict[s1] = encode(s1); while (cin >> s1 && s1 != "*") cout << decode(s1) << "\n"; return 0; }
#include <cstdio> #include <iostream> using namespace std; struct RAID { int n; bool data[64*100*6+10]; int Set(int d, int s, int b) { bool parity = 0; char str[7][6410], str1[10]; // 读入数据 scanf("%s", str1); if (str1[0]=='O') parity = 1; for (int i = 0; i < d; i++) scanf("%s", str[i]); // 统计每列有多少个x, 并统计已有01的异或值 int cnt[6410] = {}, i, j; bool check[6410] = {}; for (i = 0; i < d; i++) for (j = 0; j < s*b; j++) { if (str[i][j]=='x') cnt[j]++; else check[j] ^= (str[i][j]-'0'); } for (j = 0; j < s*b; j++) { // 每列校验 if (cnt[j] > 1) return 0; if (cnt[j] == 0 && check[j] != parity) return 0; // 否则只有一个x,直接恢复 for (i = 0; i < d; i++) if (str[i][j]=='x') { str[i][j] = (parity^check[j]) + '0'; break; } } // 生成数据data,遇到'x'时return 0 for (n = j = 0; j < s*b; j += s) for (i = 0; i < d; i++) { if (i == (j/s)%d) continue; // 注意此处技巧,巧妙的跳过校验码 for (int k = 0; k < s; k++) data[n++] = str[i][j+k]-'0'; } while (n%4 != 0) data[n++] = 0; return 1; } void out() { for (int i = 0; i < n; i += 4) printf("%X", data[i]*8 + data[i+1]*4 + data[i+2]*2 + data[i+3]); putchar('\n'); } }; int main() { int d, s, b, kase = 0; RAID A; while (cin >> d >> s >> b) { if (A.Set(d, s, b)) { printf("Disk set %d is valid, contents are: ", ++kase); A.out(); } else printf("Disk set %d is invalid.\n", ++kase); } return 0; }
#include <algorithm> #include <cstdio> using namespace std; struct Stu { int n, cnt; // n:总人数, cnt:当前睡觉人数 int a[10], b[10], c[10]; Stu(int _n): n(_n) { for (int i = cnt = 0; i < n; i++) { scanf("%d%d%d", a+i, b+i, c+i); if (c[i]>a[i]) cnt++; } } int Lim() { // 返回迭代上限 long long lim = 1; for (int i = 0; i < n; i++) lim *= a[i]+b[i]; return min(lim+5, 10000LL); } int Next() { // 返回当前睡觉总人数 int CanSleep = 0; if (cnt > n - cnt) CanSleep = 1; for (int i = 0; i < n; i++) { if (c[i]==a[i]+b[i]) cnt--, c[i] = 0; else if (c[i]==a[i]) { if (CanSleep) cnt++; else c[i] = 0; } c[i]++; } return cnt; } }; int main() { int n, kase = 0; while (scanf("%d", &n), n) { Stu A(n); int lim = A.Lim(), t = 2; while (A.Next() && t < lim) t++; printf("Case %d: %d\n", ++kase, t==lim?-1:t); } return 0; }我这里把迭代次数优化了下,有些多此一举把代码写的复杂了些,与时俱进yhj这份写的比较简洁。
#include <iostream> using namespace std; int main() { long long n, x, y, N, A, B, ansN, ansA, ansB; while (cin >> n >> x >> y) { ansN = n*y<<10; for (A = 0; A < 32; A++) { for (B = 0; B < 32; B++) { N = (((n-1)*x+((n-1)*x<<A))>>B) + y; if (N>=n*y && N<ansN) { ansA = A; ansB = B; ansN = N; } } } cout << ansN << " " << ansA << " " << ansB << "\n"; } return 0; }
#include <algorithm> #include <cstdio> using namespace std; #define rep(i,a,b) for(int i=a; i<b; i++) const int maxn = 35; int a[maxn*maxn], n, m; int main() { for (int kase = 1; scanf("%d%d", &n, &m), n&&m; kase++) { n *= m; rep(i, 0, n) scanf("%d", a+i); sort(a, a+n); a[n] = 0x7fffffff; int k; double sum; scanf("%lf", &sum); sum /= 100.0; for (k = 1; k <= n; k++) { sum += a[k-1]; if (sum/k <= a[k]) break; } printf("Region %d\n", kase); printf("Water level is %.2lf meters.\n", sum/k); printf("%.2lf percent of the region is under water.\n\n", k*100.0/n); } return 0; }LRJ虽然建议多想几种方法,不过我想不到比这效率更优或代码更简单的思路。也就不乱凑方法了。
原文地址:http://blog.csdn.net/code4101/article/details/38540759