1001:^&^
题意:求最小的C使(A xor C) & (B xor C)最小
思路:要使(A xor C) & (B xor C)最小,那么它最小只能是零。问题就变成了找到一个C使(A xor C) & (B xor C)为0,当时比赛的时候蒙对了,发现C=A&B,发现倒推是可以证明的,但正推不太会。
代码:
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long m,n;
scanf("%d%d",&m,&n);
m=m&n;
if(m==0)
cout<<"1"<<endl;
else
cout<<m<<endl;
}
return 0;
}
1006:Shuffle Card
题意:给你n个数,从1到n排列,进行m次操作,每次操作都把这个数放到这个数列的第一个位置。
题目很好做,关键是你用什么容器装1-n个数,可以用vector,也可以用stack。有一个问题要注意,就是重复的数要对其进行标记
代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
const int maxn = 1e5;
int a[maxn], b[maxn], temp[maxn];
int main() {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(temp, 0, sizeof(temp));
vector<int> ans;
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= m; i++)
cin >> b[i];
for (int i = m; i >= 1; i--) {
if (temp[b[i]] == 1)
continue;
ans.push_back(b[i]);
temp[b[i]] = 1; //标记
}
for (int i = 1; i <= n; i++) {
if (temp[a[i]] == 1)
continue;
ans.push_back(a[i]);
}
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
//system("pause");
return 0;
}
1007:Windows Of CCPC
大佬博客:https://blog.csdn.net/qq_41505957/article/details/100043677
代码:
#include<iostream>
#include<cmath>
using namespace std;
void f(int n, int s, int t)
{
if (n == 2) {
if (t == 1) {
if (s == 1)
cout << "CC";
else
cout << "PC";
}
else {
if (s == 1)
cout << "PP";
else
cout << "CP";
}
return ;
}
int x = s % (n / 2);
if (x == 0)
x = n / 2;
if (t == 1)
{
if (s > n / 2)
f(n / 2, x, 0);
else
f(n / 2, x, 1);
f(n / 2, x, 1);
}
else if (t == 0) {
if (s > n / 2)
f(n / 2, x, 1);
else
f(n / 2, x, 0);
f(n / 2, x, 0);
}
}
int main() {
int n,m;
cin >> n;
while (n--) {
cin >> m;
m = pow(2, m);
for (int i = 1; i <= m; i++) {
f(m, i, 1);
cout << endl;
}
}
//system("pause");
return 0;
}