A
A ^ A = A,^a 或 ^b结果最小,都为a ^ b.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t, a, b;cin >> t;
while (t--){
cin >> a >> b;
cout << (a ^ b) << endl;//直接异或输出加括号
}
}
B
1.s的四周为0,f的四周为1.
2.s的四周为1,f的四周为0.
哪种满足条件使用哪种.
#include<bits/stdc++.h>
using namespace std;
char a[210][210];
int main()
{
int t, n, i, j;cin >> t;
while (t--){
cin >> n;
for (i = 1; i <= n; i++)
cin >> a[i] + 1;
int cnt1 = 0, cnt2 = 0;
if (a[1][2] == '1') cnt1++;
if (a[2][1] == '1') cnt1++;
if (a[n][n - 1] == '0') cnt1++;
if (a[n - 1][n] == '0') cnt1++;
if (a[1][2] == '0') cnt2++;
if (a[2][1] == '0') cnt2++;
if (a[n][n - 1] == '1') cnt2++;
if (a[n - 1][n] == '1') cnt2++;
if (cnt1 <= 2){
cout << cnt1 << endl;
if (a[1][2] == '1') cout << 1 << ' ' << 2 << endl;
if (a[2][1] == '1') cout << 2 << ' ' << 1 << endl;
if (a[n][n - 1] == '0') cout << n << ' ' << n - 1 << endl;
if (a[n - 1][n] == '0') cout << n - 1 << ' ' << n << endl;
}
else{
cout << cnt2 << endl;
if (a[1][2] == '0') cout << 1 << ' ' << 2 << endl;
if (a[2][1] == '0') cout << 2 << ' ' << 1 << endl;
if (a[n][n - 1] == '1') cout << n << ' ' << n - 1 << endl;
if (a[n - 1][n] == '1') cout << n - 1 << ' ' << n << endl;
}
}
}
C
规律题.有一个通用的构造回文串的规律:
R n - 1
L n
L 2
#include<bits/stdc++.h>
using namespace std;
char s[100010];
bool check(char *s)
{
int i, l = strlen(s);
for (i = 0; i < l / 2; i++)
if (s[i] != s[l - i - 1]) return 0;
return 1;
}
int main()
{
cin >> s;
int i, l = strlen(s);
if (check(s)) cout << 0 << endl;//原串回文输出0
else{
cout << 3 << endl;
cout << 'R' << ' ' << l - 1 << endl;
cout << 'L' << ' ' << l << endl;
cout << 'L' << ' ' << 2 << endl;
}
}