PTA基础编程题目集(二)(输入带空格字符串,字符数组、7-28 猴子选大王(*)、7-30 字符串的冒泡排序、7-31 字符串循环左移 、7-1 厘米换算英尺英寸、7-35 有理数均值(*))
7-28 猴子选大王 (20分)
https://pintia.cn/problem-sets/14/problems/808
题意:n个猴子围成圈,从1到3循环报数,为3就退出直到只剩最后一个猴子,输出该猴子初序;
思路:模拟题,数组存储,对数组1~3循环编号,同时k从1递增,逢4变1,数组编号逢3变0,同时计数值count--,直到count==1退出while循环,输出该值初序;
代码:
#include <cstdio> #include <iostream> #include <cmath> #include <algorithm> using namespace std; int main() { int array[1010]={0}; int n,i; int k=1; cin>>n; int count=n; for(i=1;i<=n;i++) array[i]=i; while(count>1) { for(i=1;i<=n;i++) { if(array[i]!=0){ array[i]=k; k++; } if(k==4) k=1; if(array[i]==3) { array[i]=0; count--; } } } for(i=1;i<=n;i++) { if(array[i]!=0) cout<<i<<endl; } return 0; }
7-30 字符串的冒泡排序 (20分)
https://pintia.cn/problem-sets/14/problems/810
代码:
#include <cstdio> #include <cstring> #include <string> #include <iostream> #include <algorithm> #include <cmath> using namespace std; int main() { int n,k; char chr[110][13]; char chr2[13]; //string chr[110]; int i,j; cin>>n>>k; for(i=0;i<n;i++) cin>>chr[i]; for(i=0;i<k;i++) { for(j=0;j<n-1;j++) { if(strcmp(chr[j],chr[j+1])>0) { strcpy(chr2,chr[j]); strcpy(chr[j],chr[j+1]); strcpy(chr[j+1],chr2); } } } for(i=0;i<n;i++) cout<<chr[i]<<endl; return 0; }
7-31 字符串循环左移 (20分)
https://pintia.cn/problem-sets/14/problems/811
代码:
#include <cstdio> #include <iostream> #include <cstring> #include <string> using namespace std; int main() { char chr[210]; int n; int i,j,k; //gets(chr); cin.getline(chr,210); // cin>>n; int len=strlen(chr); int s=n%(len); for(i=len,j=0;i<len+s;i++,j++) { chr[i]=chr[j]; } for(i=s;i<len+s;i++) cout<<chr[i]; cout<<endl; return 0; }
ps:如何输入带空格字符串 https://www.cnblogs.com/houchen/p/10356396.html
7-1 厘米换算英尺英寸 (15分)
https://pintia.cn/problem-sets/14/problems/781
代码:
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespace std; int main() { int foot,inch; int c; cin>>c; foot=c/100.0/0.3048; inch=(c/100.0/0.3048-foot)*12; cout<<foot<<" "<<inch<<endl; return 0; }
7-35 有理数均值 (20分)
https://pintia.cn/problem-sets/14/problems/815
思路:第一次提交只得了18分,最后一个测试点没过,报浮点错误,然后加了下面两段代码就过了;
if(n==1&&a[1]==0) { cout<<"0"<<endl; return 0; }
if(value3==0){ cout<<"0"<<endl; return 0; }
代码:
#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> #include <iostream> using namespace std; int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } int main() { int a[110],b[110]; int n; int i,j,k; cin>>n; int value1,value2=1,value3=0; int value4,value5; for(i=1;i<=n;i++) scanf("%d/%d",&a[i],&b[i]); if(n==1&&a[1]==0) { cout<<"0"<<endl; return 0; } for(i=1;i<=n;i++){ //scanf("%lld/%lld",&a[i],&b[i]); if(b[i]==0) { n--; continue; } int x=a[i],y=b[i]; a[i]=x/gcd(x,y); b[i]=y/gcd(x,y); value2*=b[i]; } value2*=n; for(i=1;i<=n;i++) { value1=1; for(j=1;j<=n;j++) { if(j!=i) value1*=b[j]; else value1*=a[i]; } value3+=value1; } if(value3==0){ cout<<"0"<<endl; return 0; } value4=value3/gcd(value3,value2); value5=value2/gcd(value3,value2); if(value5==1) cout<<value4<<endl; else cout<<value4<<"/"<<value5<<endl; return 0; }
天晴了,起飞吧