A. Splitting into digits
Solved.
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int n; 5 6 void solve() 7 { 8 printf("%d\n", n); 9 for (int i = 1; i <= n; ++i) printf("%d%c", 1, " \n"[i == n]); 10 } 11 12 int main() 13 { 14 while (scanf("%d", &n) != EOF) 15 solve(); 16 return 0; 17 }
B. Game with string
Solved.
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 100010 5 char s[N]; 6 7 int main() 8 { 9 while (scanf("%s", s + 1) != EOF) 10 { 11 stack <char> sta; 12 int cnt = 0; 13 for (int i = 1, len = strlen(s + 1); i <= len; ++i) 14 { 15 if (!sta.empty() && sta.top() == s[i]) sta.pop(), ++cnt; 16 else sta.push(s[i]); 17 } 18 puts(cnt & 1 ? "Yes" : "No"); 19 } 20 return 0; 21 }
C. Grid game
Solved.
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 1010 5 char s[N]; 6 7 int main() 8 { 9 while (scanf("%s", s + 1) != EOF) 10 { 11 int x = 0, y = 0; 12 for (int i = 1, len = strlen(s + 1); i <= len; ++i) 13 { 14 if (s[i] == ‘1‘) 15 { 16 printf("%d %d\n", 1, x % 2 ? 3 : 1); 17 ++x; 18 } 19 else 20 { 21 printf("%d %d\n", 3, y % 4 + 1); 22 ++y; 23 } 24 } 25 } 26 return 0; 27 }
D. Game with modulo
Solved.
题意:
交互题
猜数,猜一个$a$
每次询问格式为$(x, y)$
返回结果有两种
$x \;if (x \; mod\; a) >= (y \;mod\; a)$
$y \;if (x \;mod\; a) < (y \;mod\; a) $
思路:
我们考虑 从小到大倍增,去找到$a的一个单调范围$
比如说$1, 2, 4, 8, 16, 32 如果某一次询问中返回了x$
那么说明$a在询问的(x, y)范围中 并且2 \cdot a 不在这个范围内$
因为是从小到大进行倍增
那么我们考虑某一次询问是$(x, 2\cdot x)$
$a在其中$
$如果 a > x, 那么必然有x >= 2 \cdot x - a$
$如果a = x 那么必然有 x \;mod\;a = 2 \cdot x \;mod\; a$
那么这个区间就具有一个单调性,可以进行二分
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 char op[110]; 5 6 bool check(int x, int y) 7 { 8 printf("? %d %d\n", x, y); 9 fflush(stdout); 10 scanf("%s", op); 11 return op[0] == ‘y‘; 12 } 13 14 void solve(int l, int r) 15 { 16 int base = 1; 17 for (int i = l; i <= r; i += base, base <<= 1) 18 { 19 printf("? %d %d\n", i, i + base); 20 fflush(stdout); 21 scanf("%s", op); 22 if (op[0] == ‘x‘) 23 { 24 int l = i + 1, r = i + base - 1, res = -1; 25 while (r - l >= 0) 26 { 27 int mid = (l + r) >> 1; 28 if (check(i, mid)) 29 { 30 l = mid + 1; 31 res = mid; 32 } 33 else 34 { 35 r = mid - 1; 36 } 37 } 38 if (res == -1) res = i; 39 printf("! %d\n", res + 1); 40 fflush(stdout); 41 return; 42 } 43 } 44 45 } 46 47 int main() 48 { 49 while (scanf("%s", op) && op[0] != ‘e‘) 50 solve(0, 1000000000); 51 return 0; 52 }
原文地址:https://www.cnblogs.com/Dup4/p/10306995.html
时间: 2024-11-08 08:44:03