vector<vector<int>> s(n, vector<int>(4, 0)); for (int i = 0; i < n; ++i) { int a, b, g, k; scanf("%d%d%d%d", &a, &b, &g, &k); s[i][0] = a, s[i][1] = b, s[i][2] = g, s[i][3] = k; }
scanf("%d%d", &x, &y);
int ans = -1; for (int i = 0; i < n; ++i) { if (s[i][0] <= x && s[i][1] <= y && s[i][0] + s[i][2] >= x && s[i][1] + s[i][3] >= y) ans = i + 1; } printf("%d", ans); return0; }
intmain(){ int n, a; scanf("%d", &n); bool first = true; // 标志是否是输出中的第一项 for (int i = n; i >= 0; --i) { scanf("%d", &a); if (a == 0) continue; // 系数部分 if (first) { // 第一项,特殊之处在于正数前面不用加 + 号 if (a == -1) printf("-"); elseif (a != 1) printf("%d", a); first = false; } else { if (a < 0) { if (a != -1) printf("%d", a); else printf("-"); // -1 前面只要一个负号也是易忽略的点 } elseif (a != 1) printf("+%d", a); else printf("+"); } // 指数部分 if (i > 1) { printf("x^%d", i); } elseif (i == 1) { printf("x"); } // 指数为 0 的且系数是 +1, -1 的要补上个 1 elseif (i == 0 && (a == 1 || a == -1)) printf("1"); } if (first) printf("0"); return0; }
intgame(int a, int b){ // a 胜利返回 1, b 胜利返回 -1,平局返回 0 if (a == b) return0; elseif (a == 0) return b == 2 || b == 3 ? 1 : -1; elseif (a == 1) return b == 0 || b == 3 ? 1 : -1; elseif (a == 2) return b == 1 || b == 4 ? 1 : -1; elseif (a == 3) return b == 2 || b == 4 ? 1 : -1; elseif (a == 4) return b == 0 || b == 1 ? 1 : -1; else return0; }
int a[207], b[207];
intmain(){ int n, na, nb, p, q; scanf("%d%d%d", &n, &na, &nb); int ca = 0, cb = 0; // 计分 for (int i = 0; i < na; ++i) { scanf("%d", &p); a[i] = p; } for (int i = 0; i < nb; ++i) { scanf("%d", &q); b[i] = q; } for (int i = 0; i < n; ++i) { int res = game(a[i % na], b[i % nb]); if (res == 1) ca++; elseif (res == -1) cb++; } printf("%d %d\n", ca, cb); return0; }
publicclassMain { publicstaticvoidmain(String[] args) { Scannersc=newScanner(System.in); Mains=newMain(); intn= sc.nextInt(), na = sc.nextInt(), nb = sc.nextInt(); int[] sa = newint[na], sb = newint[nb]; for (inti=0; i < na; ++i) { sa[i] = sc.nextInt(); } for (inti=0; i < nb; ++i) { sb[i] = sc.nextInt(); } int[] ans = s.game(n, sa, sb); System.out.println(ans[0] + " " + ans[1]); sc.close(); }
int[] game(int n, int[] playerA, int[] playerB) { intn1= playerA.length, n2 = playerB.length; intr= lcm(n1, n2); intd= n / r, m = n % r; int[] score = newint[2]; for (inti=0; i < m; ++i) { if (playerA[i % n1] == playerB[i % n2]) continue; if (RPSSL(playerA[i % n1], playerB[i % n2])) score[0]++; else score[1]++; } if (d == 0) return score; inta= score[0], b = score[1]; for (inti= m; i < r; ++i) { if (playerA[i % n1] == playerB[i % n2]) continue; if (RPSSL(playerA[i % n1], playerB[i % n2])) a++; else b++; } score[0] += a * d; score[1] += b * d; return score; }
booleanRPSSL(int a, int b) { // a 胜利返回 true, b 胜利返回 false if (a == 0) return b == 2 || b == 3 ? true : false; elseif (a == 1) return b == 0 || b == 3 ? true : false; elseif (a == 2) return b == 1 || b == 4 ? true : false; elseif (a == 3) return b == 2 || b == 4 ? true : false; elseif (a == 4) return b == 0 || b == 1 ? true : false; else returnfalse; }
intgcd(int a, int b) { // 计算两个数的最大公约数 while (b != 0) { inttmp= a % b; a = b; b = tmp; } return a; }
intlcm(int a, int b) { // 计算两个数的最小公倍数 intg= gcd(a, b); return g * (a / g) * (b / g); } }
defgame(a: int, b: int) -> int: # a 胜利返回 1, b 胜利返回 -1,平局返回 0 if a == b: return0 elif a == 0: return1if b == 2or b == 3else -1 elif a == 1: return1if b == 0or b == 3else -1 elif a == 2: return1if b == 1or b == 4else -1 elif a == 3: return1if b == 2or b == 4else -1 elif a == 4: return1if b == 0or b == 1else -1 else: return0
n, na, nb = map(int, input().split())
# r = lcm(na, nb) g = gcd(na, nb) r = g * (na // g) * (nb // g)
a, b = list(map(int, input().split())) * (r // na), list(map(int, input().split())) * (r // nb) tim, k = divmod(n, r) sa = sum((1for x, y inzip(a, b) if game(x, y) == 1)) sb = sum((1for x, y inzip(a, b) if game(x, y) == -1)) ca = sum((1for x, y, _ inzip(a, b, range(k)) if game(x, y) == 1)) cb = sum((1for x, y, _ inzip(a, b, range(k)) if game(x, y) == -1)) print("{} {}".format(sa * tim + ca, sb * tim + cb))
intnext(int cur, int d, int dir, int i){ // 当前位置 cur, 小人朝向 d, 方向 dir, 第 i 个 // 小人位置在数组中是逆时针的,所以逆序是顺时针方向 // d = 0 表示朝向圈内(左顺右逆),d = 1 表示朝向圈外(左逆右顺) // dir = 0 表示向左数,dir = 1 表示向右数 // 返回执行完一次指令后的位置下标 if (d == 1) dir = 1 - dir; // 统一朝向方便讨论 if (dir == 0) return (cur - i + n) % n; else return (cur + i) % n; }
intmain(){ cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> dir[i] >> prof[i]; } int d, s, cur = 0; for (int i = 0; i < m; ++i) { cin >> d >> s; cur = next(cur, dir[cur], d, s); } cout << prof[cur] << "\n"; return0; }
intmain(){ int a = 0, b = 0; int c = 0, d = 0; int i = 0; char s; bool p1 = true, p2 = true; while (true) { s = getchar(); p1 = true; p2 = true; if (s == 'E') break; elseif (s == 'W') { a++, c++; } elseif (s == 'L') { b++, d++; } if ((a >= 11 && a - b >= 2) || (b >= 11 && b - a >= 2)) { printf("%d:%d\n", a, b); a = 0; b = 0; p1 = false; } if ((c >= 21 && c - d >= 2) || (d >= 21 && d - c >= 2)) { s1[i] = c; s2[i++] = d; c = 0; d = 0; p2 = false; } } if (a + b != 0 || p1) { printf("%d:%d\n", a, b); } putchar('\n'); for (int j = 0; j < i; ++j) { printf("%d:%d\n", s1[j], s2[j]); } if (c + d != 0 || p2) printf("%d:%d\n", c, d); return0; }
#include<cstdio> usingnamespace std; // 数据范围 constint N = 17; typedeflonglong ll; ll l, r, dp[N], mi[N]; int a[N];
inline ll Count(ll n, int k){ ll tmp = n, ans = 0; int len = 0; // 拆开 n 各位上的数存入 a 中 while (n) a[++len] = n % 10, n /= 10; // 由高位向低位枚举 for (int i = len; i >= 1; --i) { ans += dp[i - 1] * a[i]; if (k < a[i]) ans += mi[i - 1]; tmp -= mi[i - 1] * a[i]; if (k == a[i]) ans += tmp + 1; if (k == 0) ans -= mi[i - 1]; } return ans; }
privatelongCount(long n, int s) { longans=0; int[] num = newint[N]; longk= n; // 拆开 n 各位上的数存入 数组中 inti=0; while (n != 0) { num[++i] = (int) (n % 10); n /= 10; } for (; i >= 1; --i) { ans += dp[i - 1] * num[i]; if (s < num[i]) ans += base[i - 1]; k -= base[i - 1] * num[i]; if (s == num[i]) ans += k + 1; if (s == 0) ans -= base[i - 1]; } return ans; }
publiclongCountProblem(long start, long end, int k) { return Count(end, k) - Count(start - 1, k); } }
模拟解法:
1 2 3 4 5
defCountProblem(start: int, end: int, k: int) -> int: """统计 k 在范围 [start,end] 的出现次数""" returnsum((str(i).count('2') for i inrange(start, end + 1)))
#include<cstdio> usingnamespace std; constint N = 41; int Magic[N][N];
inlinevoidsetMagic(int n){ int ed = n * n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) Magic[i][j] = 0; int x = 0, y = n >> 1; Magic[x][y] = 1; for (int i = 2; i <= ed; ++i) { if (x == 0 && y != n - 1) x = n - 1, y++; elseif (y == n - 1 && x != 0) x--, y = 0; elseif (x == 0 && y == n - 1) x++; else { if (Magic[x - 1][y + 1] == 0) x--, y++; else x++; } Magic[x][y] = i; } }
intmain(){ int n; scanf("%d", &n); setMagic(n); for (int i = 0; i < n; ++i) { printf("%d", Magic[i][0]); for (int j = 1; j < n; ++j) printf(" %d", Magic[i][j]); putchar('\n'); } return0; }
for (inti=1, x = 0, y = n >> 1; i <= ed; ++i) { Magic[x][y] = i; if (Magic[(x + n - 1) % n][(y + 1) % n] != 0) x = (x + 1) % n; else { x = (x + n - 1) % n; y = (y + 1) % n; } } for (inti=0; i < n; ++i) { System.out.printf("%d", Magic[i][0]); for (intj=1; j < n; ++j) System.out.printf(" %d", Magic[i][j]); System.out.println(); } System.out.println(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = int(input()) Magic = [[0for _ inrange(n)] for _ inrange(n)] x, y = 0, n >> 1 for i inrange(1, n * n + 1): Magic[x][y] = i if Magic[(x + n - 1) % n][(y + 1) % n]: x, y = (x + 1) % n, y else: x, y = (x + n - 1) % n, (y + 1) % n
for x in Magic: for y in x: print(y, end=" ") print()
接下来 \(L\)
行代表程序中循环结构中的F i x y或者 E。
程序行若以F开头,表示进入一个循环,之后有空格分离的三个字符(串)i x y,
其中 \(i\)
是一个小写字母(保证不为\(n\)),表示新建的变量名,\(x\) 和 \(y\) 可能是正整数或 \(n\) ,已知若为正整数则一定小于 \(100\)。
程序行若以E开头,则表示循环体结束。
输出格式
输出文件共 \(t\) 行,对应输入的
\(t\) 个程序,每行输出 Yes
或 No 或者
ERR,若程序实际复杂度与输入给出的复杂度一致则输出
Yes,不一致则输出
No,若程序有语法错误(其中语法错误只有: ① F 和
E 不匹配
②新建的变量与已经存在但未被销毁的变量重复两种情况),则输出
ERR。
8 2O(1) F i 11 E 2O(n^1) F x 1 n E 1O(1) F x 1 n 4O(n^2) F x 5 n F y 10 n E E 4O(n^2) F x 9 n E F y 2 n E 4O(n^1) F x 9 n F y n 4 E E 4O(1) F y n 4 F x 9 n E E 4O(n^2) F x 1 n F x 110 E E
栈空的情况下复杂度的更新会出错(因为依赖于栈顶元素的前一个计算,而且数组越界不会报错),解决方法是特判栈空的情况;循环过程中栈中元素没有重置为默认值,导致结点的
start, ed
变量依靠是否为初值来判断更新与否的地方不会执行更新,使得除第一次查询外的后序检查大多失效,解决方法是在循环开始将两变量恢复到默认值
classloop { // 栈结点,记录遍历名,循环起点和终点、当前复杂度 public: char var = '#'; int start = -1, ed = -1; int comp; };
int n, L; // 程序数量、程序行数 int state = 1; // 0 表示错误,返回 ERR,1 表示正常循环 int inv = -1; // 记录 不会循环的层数 在栈中的位置 string s, comp, start, ed; char var; loop st[27]; // 存放变量名的栈 int p = -1; // 栈顶指针,指向栈中栈顶元素 int w = 0; // 计算得到的时间复杂度的指数
inlineintstr2int(string s){ int ans = 0; for (char v : s) ans = ans * 10 + (v - '0'); return ans; }
// 使用一张类型为 c 的牌, 无牌可用则返回 false boolplay(char c){ auto s = find(c); if (s == nullptr) returnfalse; discard(s); returntrue; }
// 牌堆中查找一张类型为 c 的牌并返回,若不存在则返回 nullptr card* find(char c){ for (card* p = head->next; p != head; p = p->next) { if (p->c == c) return p; } returnnullptr; } };
char player::cards[MAX_CARDS]; int player::cur_card = 0; int player::max_card = 0; player* player::mp = nullptr; int player::rcg[MAX_PLAYER]; int player::cnt_fp = 0;
player::player(int _r, int _id) : r(_r), id(_id) { // 构造函数 love = MAX_LOVE; pre = this, next = this; }
Invalid command Invalid command Invalid command Invalid command red guard;NA;no;no Invalid command blue captain;NA;no;no red soldier;NA;no;no Invalid command Invalid command blue elephant;NA;no;no red duck;NA;no;no blue horse;NA;no;no red duck;blue soldier;no;no Invalid command blue captain;NA;yes;no red duck;blue captain;no;yes Invalid command
// 能否将对应棋子移动到对应位置 boolcanMoveCaptain(int x1, int y1, int x2, int y2); boolcanMoveGuard(int x1, int y1, int x2, int y2); boolcanMoveElephant(int x1, int y1, int x2, int y2); boolcanMoveHorse(int x1, int y1, int x2, int y2); boolcanMoveCar(int x1, int y1, int x2, int y2); boolcanMoveDuck(int x1, int y1, int x2, int y2); boolcanMoveSoldier(int x1, int y1, int x2, int y2); };
// 初始化棋盘 board::board() : d(nullptr) { // 棋盘初始化 for (int i = 0; i < 10; ++i) { for (int j = 0; j < 9; ++j) { b[i][j] = nullptr; } } // 红方棋子摆上棋盘 for (int i = 0; i < 16; ++i) { piece* cur = player_red.p[i]; int x = cur->get_x(), y = cur->get_y(); b[x][y] = cur; } // 蓝方棋子摆上棋盘 for (int i = 0; i < 16; ++i) { piece* cur = player_blue.p[i]; int x = cur->get_x(), y = cur->get_y(); b[x][y] = cur; } }
// 能否将棋子移动到对应位置(假设起始位置合法且有一枚棋子) boolboard::canMove(int x1, int y1, int x2, int y2){ // 目标位置不合法 if (x2 < 0 || x2 > 9 || y2 < 0 || y2 > 8) returnfalse; // 目标位置有己方棋子 if (b[x2][y2] != nullptr && b[x1][y1]->camp() == b[x2][y2]->camp()) returnfalse;
// 根据当前位置棋子类型调用判断函数 switch (b[x1][y1]->type()) { case CAPTAIN: returncanMoveCaptain(x1, y1, x2, y2); case GUARD: returncanMoveGuard(x1, y1, x2, y2); case ELEPHANT: returncanMoveElephant(x1, y1, x2, y2); case HORSE: returncanMoveHorse(x1, y1, x2, y2); case CAR: returncanMoveCar(x1, y1, x2, y2); case DUCK: returncanMoveDuck(x1, y1, x2, y2); case SOLDIER: returncanMoveSoldier(x1, y1, x2, y2); default: returnfalse; } }
// 移动棋子,移动成功返回 true,否则返回 false boolboard::Move(int x1, int y1, int x2, int y2, Camp cap){ // 游戏已经结束不可移动 if (isEnd()) returnfalse; // 检查起始位置是否合法,是否有一个棋子 if (x1 < 0 || x1 > 9 || y1 < 0 || y1 > 8 || b[x1][y1] == nullptr) returnfalse; // 起始位置的棋子是否是所给的阵营 if (b[x1][y1]->camp() != cap) returnfalse; // 检查目的地是否可达 if (canMove(x1, y1, x2, y2)) { // 目的地可达那就移动 if (b[x2][y2] != nullptr) { // 吃掉目的地的棋子 b[x2][y2]->dead(); d = b[x2][y2]; } else d = nullptr; // 移动棋子 b[x1][y1]->move(x2, y2); b[x2][y2] = b[x1][y1]; b[x1][y1] = nullptr; returntrue; } else returnfalse; }
inline string piece::print_piece(){ switch (p) { case CAPTAIN: return"王"; case GUARD: return"士"; case ELEPHANT: return"象"; case HORSE: return"马"; case CAR: return"车"; case DUCK: return"鸭"; case SOLDIER: return"兵"; default: return"无"; } }
// 返回 [阵营] [类型] 的字符串 inline string piece::tostring(){ string c = color == RED ? "red " : "blue "; string t; switch (p) { case CAPTAIN: t = "captain"; break; case GUARD: t = "guard"; break; case ELEPHANT: t = "elephant"; break; case HORSE: t = "horse"; break; case CAR: t = "car"; break; case DUCK: t = "duck"; break; case SOLDIER: t = "soldier"; break; default: t = "err"; } return c + t; }
// ------------------- END - 棋子的方法成员 -----------------------