题目要求假设第i个位置,i的二进制中有奇数个1,把第i个位置变成大写。长度最多有500,那么先预处理出500个位置哪些位置需要变化为大写,然后面对每个字符串先把该变化为大写的位置变化,然后输出即可
#include <bits/stdc++.h>
using namespace std;
#define N 2005
#define int long long
int a[N];
signed main() {
int w='A'-'a';
for(int i=1;i<=500;i++){
int now=__builtin_popcount(i);
if(now%2==1) a[i]=w;
}
int t;
cin>>t;
string s;
while(t--){
cin>>s;
int len=s.length();
s=' '+s;
for(int i=1;i<=len;i++){
s[i]+=a[i];
cout<<s[i];
}
cout<<'\n';
}
return 0;
}
def main():
N = 2005
w = ord('A') - ord('a')
a = [0] * N
for i in range(1, 501):
now = bin(i).count('1')
if now % 2 == 1:
a[i] = w
t = int(input())
for _ in range(t):
s = input()
s = ' ' + s
result = []
for i in range(1, len(s)):
result.append(chr(ord(s[i]) + a[i]))
print(''.join(result))
if __name__ == "__main__":
main()
import java.util.Scanner;
public class Main {
static final int N = 2005;
static long[] a = new long[N];
public static void main(String[] args) {
long w = 'A' - 'a';
for (int i = 1; i <= 500; i++) {
int now = Long.bitCount(i);
if (now % 2 == 1) a[i] = w;
}
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine(); // 读取换行符
while (t-- > 0) {
String s = sc.nextLine();
StringBuilder result = new StringBuilder();
for (int i = 1; i <= s.length(); i++) {
char modifiedChar = (char)(s.charAt(i - 1) + a[i]);
result.append(modifiedChar);
}
System.out.println(result.toString());
}
sc.close();
}
}
小红拿到了一个仅包含小写字母的字符串s。
对于每个下标p,如果p的二进制表示有奇数个1,那么将sp修改为对应的大写字母。(下标从1开始)
例如,字符串"abcdefg",其中下标1、2、4、7在二进制表示下都有奇数个1,因此字符串为"ABcDefG"。
小红想知道她拿到的字符串修改是什么,你能帮帮她吗?
第一行为T,表示有T组输入。
接下来T行,每行一个仅包含小写字母的字符串s。
1≤T≤2000
1≤len(s)≤500
输出T行,每行一个字符串,表示修改后的字符串。
输入
1
abcdefg
输出
ABcDefG
输入
3
vwcvnwaomy
ovoxcfdtf
yynbve
输出
VWcVnwAOmy
OVoXcfDTf
YYnBve