A. Vlad and the Best of Five
标签:实现问题,编程技巧,模拟
问题:一个长度为5的字符串,由字母A和B组成,输出出现频率最高的字符
思路:依次遍历求出A和B分别出现的次数,进行比较再输出
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t;cin>>t;
while(t--) {
string s;cin>>s;
int a[2]={0};
for(int i=0;i<s.size();i++) {
if(s[i]=='A') a[0]++;
else a[1]++;
}
cout<<(a[0]>a[1] ? "A\n" : "B\n");
}
return 0;
}
B. Vlad and Shapes
标签:实现问题,编程技巧,模拟
问题:判断网格中所有1形成的是不是一个正方形,如果是输出"SQUARE",如果不是输出"TRIANGLE"
思路:先记录每一行1出现的个数为ans,进行比较求出最长边的个数为Max,并记录出网格中存在1的个数为sum,如果Max的平方等于sum,输出"SQUARE",否则输出"TRIANGLE"
#include <bits/stdc++.h>
using namespace std;
const int N=10+10;
char a[N][N];
int main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t;cin>>t;
while(t--) {
int n;cin>>n;
int sum=0,Max=0;
for(int i=0;i<n;i++) {
int ans=0;
for(int j=0;j<n;j++) {
cin>>a[i][j];
if(a[i][j]=='1') sum++,ans++;
}
Max=max(Max,ans);
}
cout<<(Max*Max==sum ? "SQUARE\n" : "TRIANGLE\n");
}
return 0;
}
C. Vlad and a Sum of Sum of Digits
标签:动态规划,实现问题,编程技巧,模拟
问题:将1-n的数,替换为其数位之和,求出其数之和
样例解释:1-12:1,2,3,4,5,6,7,8,9,10,11,12
1,2,3,4,5,6,7,8,9,1,2,3
1+2+3+4+5+6+7+8+9+1+2+3=51
思路:需要预处理个前缀和,a[i]=a[i-1]+temp(i)
temp(i)用于求 i 的数位之和
n<=2e5,令N=2e5+10
则先求出1-N中的情况,这样需要 n 直接在a[N],输出a[n]的值即可
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int a[N];
int temp(int x) {
int ans=0;
while(x) {
ans+=x%10;
x/=10;
}
return ans;
}
void solve() {
int n;cin>>n;
cout<<a[n]<<'\n';
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
for(int i=1;i<N;i++) a[i]=a[i-1]+temp(i);
int t;cin>>t;
while(t--) solve();
return 0;
}