第三次双周赛
3-1 打字
思路分析
纯模拟
#include<bits/stdc++.h>
using namespace std;
string s;
int a[10];
int main(){
getline(cin,s);
for (int i=0; i<s.size(); i++){
if (s[i]=='`' || s[i]=='1' || s[i]=='Q' || s[i]=='A' || s[i]=='Z')
a[1]++;
if (s[i]=='2' || s[i]=='W' || s[i]=='S' || s[i]=='X')
a[2]++;
if (s[i]=='3' || s[i]=='E' || s[i]=='D' || s[i]=='C')
a[3]++;
if (s[i]=='4' || s[i]=='R' || s[i]=='F' || s[i]=='V')
a[4]++;
if (s[i]=='5' || s[i]=='T' || s[i]=='G' || s[i]=='B')
a[4]++;
if (s[i]=='6' || s[i]=='Y' || s[i]=='H' || s[i]=='N')
a[5]++;
if (s[i]=='7' || s[i]=='U' || s[i]=='J' || s[i]=='M')
a[5]++;
if (s[i]=='8' || s[i]=='I' || s[i]=='K' || s[i]==',')
a[6]++;
if (s[i]=='9' || s[i]=='O' || s[i]=='L' || s[i]=='.')
a[7]++;
if (s[i]=='0' || s[i]=='P' || s[i]==';' || s[i]=='/' || s[i]=='-' || s[i]=='=' || s[i]=='[' || s[i]==']' || s[i]=='\'' )
a[8]++;
}
for (int i=1; i<=8; i++)
cout<<a[i]<<endl;
// system("pause");
return 0;
}
3-2 分香肠
思路分析
所有香肠连在一起去思考,只要找到不用切的地方即可。直接用double(n)/m*i - int(double(n)/m*i) == 0会存在精度差,所以用*lower_bound(b+1,b+n+1,x) == x查询。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
double b[maxn];
int main(){
int n,m;
cin>>n>>m;
int cnt=0;
for (int i=1; i<=n; i++){
b[i]=double(i)/n;
}
for (int i=1; i<=m; i++){
double x=double(i)/m;
if (*lower_bound(b+1,b+n+1,x) == x){
cnt++;
}
}
cout<<m-cnt<<endl;
system("pause");
return 0;
}
3-3 会议安排
思路分析
贪心,对于每一次选择,只要该会议结束时间越靠前,对于后续影响越小,答案就越优。
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e4+5;
struct node{
int a,b;
}t[maxn];
int T,n;
bool cmp(node a,node b){
return (a.b<b.b || (a.b==b.b && a.a>b.a));
}
int main(){
cin>>T;
while (T--){
cin>>n;
for (int i=1; i<=n; i++){
scanf("%d%d",&t[i].a,&t[i].b);
}
sort(t+1,t+1+n,cmp);
int cnt=0,st=-1;
for (int i=1; i<=n; i++){
if (t[i].a>=st){
st=t[i].b;
cnt++;
}
}
cout<<cnt<<endl;
}
// system("pause");
return 0;
}
3-4 神秘密码
思路分析
递归,每次读入分为三种情况,如果是[ ,则递归,如果是] ,则结束当前递归,否则就是其他字符直接累加。
#include <bits/stdc++.h>
using namespace std;
string doit(){
string ans,tmp;
char c;
int k;
while(cin>>c){
if(c=='['){
cin>>k;
tmp=doit();
for (int i=1; i<=k; i++)
ans+=tmp;
}
else if(c==']'){
return ans;
}
else {
ans+=c;
}
}
return ans;
}
int main(){
cout<<doit();
//system("pause");
return 0;
}
3-5 国王游戏
思路分析
贪心+高精度处理

#include <bits/stdc++.h>
using namespace std;
int n,l=1;
int ans[1005];
struct node
{
int a,b;
}t[1005];
bool cmp(node a,node b){
return a.a*a.b<b.a*b.b;
}
void multi(int x){
for(int i=1; i<=l; i++)
ans[i]*=t[x].a;
for(int i=1; i<=l; i++){
ans[i+1]+=ans[i]/10;
ans[i]=ans[i]%10;
}
l++;
while(ans[l]>9)
{
ans[l+1]+=ans[l]/10;
ans[l]%=10;
l++;
}
if(ans[l]==0)
l--;
}
void divis(){
for(int i=l; i>=1; i--){
ans[i-1]+=ans[i]%t[n].b*10;
ans[i]/=t[n].b;
}
while(ans[l]==0)
l--;
if(l==0)
cout<<1<<endl;
}
int main(){
cin>>n;
for(int i=0; i<=n; i++){
cin>>t[i].a>>t[i].b;
}
sort(t+1,t+n+1,cmp);
ans[1]=t[0].a;
for(int i=1; i<n; i++)
multi(i);
divis();
for(int i=l; i>=1; i--)
cout<<ans[i];
system("pause");
return 0;
}

444

被折叠的 条评论
为什么被折叠?



