A :找到一个数Y,使 Y & X>0 且 Y ^ X >0
因为 1 & 1 =1,其余都是 0,当Y和X完全相同的时候,Y ^ X 才会=0
所以问题转换为,把X转为二进制,从最后一位往前的第一个1就是Y的首位,其余位0,如果Y=X,则Y+1
#include <cstdio>
#include <iostream>
#define int long long
using namespace std;
signed main() {
int t;
cin >> t;
while (t--) {
int x;
cin >> x;
if (x == 1) {
cout << "3\n";
continue;
}
int a[100], len = 1, rest = x;
while (true) {
if (rest == 1 || rest == 0) {
a[len] = rest;
break;
}
a[len] = rest % 2;
rest /= 2;
len++;
}
for (int i = 1; i <= len; i++) {
if (a[i] == 1) {
int ans = 1;
for (int j = 1; j < i; j++) {
ans *= 2;
}
if (ans == x) {
ans++;
}
cout << ans << endl;
break;
}
}
}
system("pause");
return 0;
}
B :
对于任意一个数字,看做(2 ** R)*k; k为奇数,那么两个数字相加后 (假设R1 > R2) ,得到
(2**R2 )*{ [ 2**(R1-R2) ] * k1 + k2 }
此时能 / 2 得到一个奇数的次数(用函数 cul 计算),变成了最小的那个值,因为对于奇数来说,R=0,所以如果有奇数,那么这个次数就是0
在这道题目中,在偶数里找到min( cul ),再加上相加的次数,如果数列里有奇数,那么就是偶数相加的次数 + 那个奇数 (+1)
#include <cstdio>
#include <iostream>
using namespace std;
int cul(int t) {
int ts = 0;
while (t % 2 == 0) {
t /= 2;
ts++;
}
return ts;
}
int min(int a, int b) {
if (a < b)
return a;
return b;
}
signed main() {
int t;
cin >> t;
while (t--) {
int n, x, sum = 0, cnt = 0, rr = 32;
bool f = true;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
if (x % 2 == 0) {
rr = min(rr, cul(x));
cnt++;
}
}
int cnt0 = n - cnt, ans = 0;
if (cnt0 > 0) {
ans = cnt;
} else {
ans = rr + cnt - 1;
}
cout << ans << endl;
}
system("pause");
return 0;
}
C :
题目的意思是总共会有奇数个字符,我们不妨假设已经知道了转换的顺序,就是
p1 -> p2 -> p3 -> p4 -> .... -> pk
k是奇数2*n +1。所以第奇数个p就是相对于下一个的本体,本体一定出现奇数次,直接计算每个字符出现的次数,打出奇数出现次的就行。
#include <string.h>
#include <iostream>
using namespace std;
string s, trag;
int cnt[30];
void solve() {
int n;
cin >> n;
int len = n * 2 + 1;
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < len; i++) {
cin >> s;
for (int j = 0; j < s.length(); j++) {
cnt[s[j] - 'a']++;
}
}
for (int i = 0; i < 26; i++) {
if (cnt[i] % 2 != 0) {
cout << char(i + 97);
}
}
cout << endl;
}
signed main() {
int t;
cin >> t;
while (t--) {
solve();
}
system("pause");
return 0;
}
D :
说实话这题有什么好说的啊,纯模拟啊,就是别贪心去排序就好了 呜呜呜。
写了个sort WA了4次
#include <algorithm>
#include <iostream>
#include <string>
#define int long long
using namespace std;
const int N = 2e5 + 10;
int a[N], s[N];
bool cmd(int aa, int bb) {
return aa > bb;
}
void solve() {
int n, k, sum = 0, ans = 0;
cin >> n >> k;
a[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
s[i] = a[i];
s[i] += s[i - 1];
}
if (k >= n) {
ans = s[n] + (((n - 1) * n) / 2) + ((k - n) * n);
}
if (k < n) {
for (int i = k; i <= n; i++) {
ans = max(ans, s[i] - s[i - k]);
}
ans += ((k - 1) * k) / 2;
}
cout << ans << endl;
}
signed main() {
int t;
cin >> t;
while (t--) {
solve();
}
system("pause");
return 0;
}