团体程序设计天梯赛-练习集(三)(string.replace()函数用法、L1-009、L1-011、L1-025、L1-015、L1-019(*) )
L1-009 N个数求和 (20分)
https://pintia.cn/problem-sets/994805046380707840/problems/994805133597065216
第一次得17分,少考虑分子为负情况
if(sum2<0) { sum2*=-1; cout<<"-"; }
#include <cstdio> #include <cstring> #include <string> #include <iostream> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); } int main() { int n; int i,j,k; ll a[101],b[101]; ll c,d,sum1=1,sum2=0; cin>>n; for(i=0;i<n;i++){ scanf("%lld/%lld",&c,&d); a[i]=c/gcd(c,d),b[i]=d/gcd(c,d); sum1*=b[i]; } if(n==1&&a[0]==0) return 0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i!=j) a[i]*=b[j]; } sum2+=a[i]; } if(sum2==0) { cout<<0<<endl; return 0; } c=sum1,d=sum2; sum1=c/gcd(c,d); //分母 sum2=d/gcd(c,d); //分子 if(sum2<0) { sum2*=-1; cout<<"-"; } if(sum2>=sum1) { if(sum2%sum1==0) cout<<sum2/sum1<<endl; else cout<<sum2/sum1<<" "<<sum2%sum1<<"/"<<sum1<<endl; } else cout<<sum2<<"/"<<sum1<<endl; return 0; }
L1-011 A-B (20分)
https://pintia.cn/problem-sets/994805046380707840/problems/994805130426171392
string.replace()函数用法:https://blog.csdn.net/qq_41628088/article/details/80731630
法一(101ms)
#include <cstdio> #include <cstring> #include <string> #include <iostream> using namespace std; int main() { string str1,str2; getline(cin,str1); getline(cin,str2); int i,j,k,len1,len2; len1=str1.length(); len2=str2.length(); for(i=0;i<len2;i++) for(j=0;j<len1;j++) if(str2[i]==str1[j]){ str1.replace(j,1,""); j--; } cout<<str1<<endl; return 0; }
法二(9ms)
#include <cstdio> #include <iostream> #include <cstring> #include <string> using namespace std; int main() { string str1,str2; getline(cin,str1); getline(cin,str2); int len1=str1.length(); int len2=str2.length(); int i,j,k,index=0,t; for(i=0;i<len2;i++){ index=0; while((t=str1.find(str2[i],index))!=string::npos) { str1.replace(t,1,""); index=t; } } cout<<str1<<endl; return 0; }
法三(5ms)
#include <cstdio> #include <iostream> #include <string> #include <cstring> using namespace std; int main() { int book[256]={0}; string str1,str2; getline(cin,str1); getline(cin,str2); for(int i=0;i<str2.length();i++) book[str2[i]]=1; for(int i=0;i<str1.length();i++) if(!book[str1[i]]) cout<<str1[i]; cout<<endl; return 0; }
L1-025 正整数A+B (15分)
https://pintia.cn/problem-sets/994805046380707840/problems/994805110318678016
#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> #include <iostream> using namespace std; int main() { string str; int i,j,k,sign,digit; int flagA=1,flagB=1; int numA=0,numB=0; getline(cin,str); for(i=0;i<str.length();i++) { if(str[i]==' ') { sign=i; if(i==0) flagA=0; else if(i==1&&str[0]=='0') flagA=0; else { digit=1; for(j=i-1;j>=0;j--) { if(str[j]<'0'||str[j]>'9') flagA=0; else { numA+=(str[j]-'0')*digit; digit*=10; } } } if(numA<1||numA>1000) flagA=0; if(flagA==0) cout<<"?"; else cout<<numA; break; } } cout<<" "<<"+"<<" "; digit=1; for(k=sign+1;k<str.length();k++) { if(str[k]<'0'||str[k]>'9') { flagB=0; break; } } if(flagB!=0){ for(j=str.length()-1;j>=sign+1;j--) { numB+=(str[j]-'0')*digit; digit*=10; } } if(numB<1||numB>1000) flagB=0; if(flagB!=0) cout<<numB; else if(flagB==0) cout<<"?"; cout<<" "<<"="<<" "; if(flagA==0||flagB==0) cout<<"?"<<endl; else cout<<numA+numB<<endl; return 0; }
L1-015 跟奥巴马一起画方块 (15分)
https://pintia.cn/problem-sets/994805046380707840/problems/994805124398956544
四舍五入
if(n%2==0) p=n/2; else p=n/2+1;
#include <iostream> #include <string> #include <cstring> #include <cmath> #include <algorithm> using namespace std; int main() { int n,p; char chr; cin>>n; cin>>chr; if(n%2==0) p=n/2; else p=n/2+1; for(int i=1;i<=p;i++) { for(int j=1;j<=n;j++) { cout<<chr; } cout<<endl; } return 0; }
L1-019 谁先倒 (15分)
https://pintia.cn/problem-sets/994805046380707840/problems/994805118568873984
一开始理解错意思,以为就算某一局同输各自罚酒也各加一杯,当某方已罚酒杯数超过最大不倒酒量并且对应场次没有平局时结束运行;实际上是当两方同输时各自罚酒不增一,当某方已罚酒杯数超过其最大不倒酒量时结束运行。
#include <cstdio> #include <iostream> #include <cstring> #include <string> #include <cmath> #include <algorithm> using namespace std; int main() { int CapA,CapB,n; int ShoA[101],DrawA[101],ShoB[101],DrawB[101]; int countA=0,countB=0; int flagA=1,flagB=1; cin>>CapA>>CapB>>n; for(int i=0;i<n;i++) cin>>ShoA[i]>>DrawA[i]>>ShoB[i]>>DrawB[i]; for(int i=0;i<n;i++) { int temp=ShoA[i]+ShoB[i]; if((temp==DrawA[i])&&(DrawA[i]!=DrawB[i])) countA++; if((temp==DrawB[i])&&(DrawA[i]!=DrawB[i])) countB++; if(countA>CapA) { cout<<"A\n"; cout<<countB<<endl; break; } else if(countB>CapB) { cout<<"B\n"; cout<<countA<<endl; break; } } return 0; }
天晴了,起飞吧