PTA的刷题记录
L1
Hello World!
#include<iostream> using namespace std; int main() { cout<<"Hello World!"; return 0; }
打印沙漏
#include<stdio.h> #include<math.h> #include<algorithm> int main(){ char s='*'; int n=5; int sums=3, sums0=3, sum=1; int n0=0; scanf("%d",&n); scanf(" %c",&s); while(sums0<n/2) { sums+=2; sums0+=sums; sum+=2; }; for(int i=-sum;i<=sum;++i,++i) { if(i==1)continue; for(int j=0;j<(sum-abs(i))/2;++j) { printf(" "); } for(int j=0;j<abs(i);++j) { printf("%c",s); n0++; } if(i)printf("\n"); } printf("%d",n-n0); return 0; }
个位数统计
#include<iostream> #include<string> using namespace std; int a[10]; int main() { string n; cin>>n; for(int i=0;i<n.length();++i) { a[n[i]-'0']++; } for(int i=0;i<10;++i) { if(a[i]) cout<<i<<":"<<a[i]<<endl; } return 0; }
计算摄氏温度
#include<iostream> #include<stdio.h> using namespace std; int main() { float a; cin>>a; printf("Celsius = %d",(int)(5*(a-32)/9)); return 0; }
考试座位号
#include<iostream> #include<stdio.h> using namespace std; const int N = 10005; struct p { string id; int k; }stu[N]; int main() { int n,s,k; string id; cin>>n; for(int i=0;i<n;++i) { cin>>id>>s>>k; stu[s].id=id; stu[s].k= k; } cin>>n; for(int i=0;i<n;++i) { cin>>s; cout<<stu[s].id<<" "<<stu[s].k<<endl; } }
连续因子💦
#include<iostream> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; ll sum, start;//最长连续因子的个数,开始时的因子 int main() { ll n; cin >> n; for (ll i = 2; i*i <= n; i++) { if (n%i != 0)continue; ll j = i; ll t = n; //临时变量代替n,否则n的值会改变 ll num = 0;//临时记录最长连续银子个数 while(t%j == 0)//暴力连续因子 { t /= j; num++; j++; } if (sum < num)//更新最优值 { sum = num; start = i; } } if (sum == 0)//素数 cout << "1" << endl << n << endl; else { cout << sum << endl; for (int i = 0; i < sum; i++) { if (i == sum - 1) cout << start + i << endl; else cout << start + i<<"*"; } } return 0; }
念数字
#include<iostream> #include<stdio.h> #include<vector> using namespace std; const int N = 10005; string a[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; int main() { int n; cin>>n; if(n<0) { cout<<"fu "; } else if(n==0) { cout<<"ling"; return 0; } n = n<0?-n:n; vector<string >b; while(n) { b.push_back(a[n%10]); n/=10; } cout<<b[b.size()-1]; for(int i=b.size()-2;i>=0;--i) { cout<<" "<<b[i]; } }
求整数段和
#include<iostream> #include<stdio.h> #include<vector> using namespace std; const int N = 10005; int main() { int a,b,sum=0,c=0; cin>>a>>b; for(int i=a;i<=b;++i) { c++; printf("%5d",i); if(c%5==0) cout<<endl; if(i==b&&((b-a+1)%5!=0))cout<<endl; sum+=i; } cout<<"Sum = "<<sum<<endl; return 0; }
N个数求和
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; const int M = 107; struct num { ll a; ll b; }lao[M]; int main() { int n; ll A=0,B=1; cin>>n; for(int i=0;i<n;++i) { scanf("%ld/%ld",&lao[i].a,&lao[i].b); B*=lao[i].b; //同分 } for(int i=0;i<n;++i) { //计算分子 A+=B/lao[i].b*lao[i].a; } if(A==0) { cout<<0; return 0; } ll C = A/B; A -= B*C; ll gcd = __gcd(A,B); A/=gcd; B/=gcd; int flag=0; if((A<0&&B>0) || (A>0&&B<0)) flag=1; if(flag) { A=A<0?-A:A; B=B<0?-B:B; if(C!=0&&A!=0) printf("%ld -%ld/%ld",C,A,B); else if(A!=0) printf("-%ld/%ld",A,B); else if(C!=0) printf("%ld",C); } else { if(C!=0&&A!=0) printf("%ld %ld/%ld",C,A,B); else if(A!=0) printf("%ld/%ld",A,B); else if(C!=0) printf("%ld",C); } return 0; }
比较大小
#include<iostream> #include<algorithm> #include<vector> using namespace std; const int N = 10005; int main() { int a[3]; for(int i=0;i<3;++i) { cin>>a[i]; } sort(a,a+3); cout<<a[0]<<"->"<<a[1]<<"->"<<a[2]; return 0; }
A-B
#include<iostream> #include<algorithm> #include<string> using namespace std; const int N = 10005; int main() { string a,b,c=""; int n,flag; getline(cin,a); getline(cin,b); for(int i=0;i<a.size();++i) { flag=1; for(int j=0;j<b.size();++j) { if(a[i]==b[j]) { flag=0; break; } } if(flag) c+=a[i]; } cout<<c; return 0; }
计算指数
#include<iostream> #include<algorithm> #include<string> using namespace std; const int N = 10005; int main() { int n; cin>>n; cout<<"2^"<<n<<" = "<<(1<<n); return 0; }
计算阶乘和
#include<iostream> #include<algorithm> #include<string> using namespace std; const int N = 10005; int main() { int n,t=1,s=0; cin>>n; for(int i=1;i<=n;++i) { t*=i; s+=t; } cout<<s; return 0; }
简单题
#include<iostream> #include<algorithm> #include<string> using namespace std; const int N = 10005; int main() { cout<<"This is a simple problem."; return 0; }
跟奥巴马一起画方块
#include<iostream> #include<cstdio> using namespace std; int main() { float n; char c; cin>>n; getchar(); cin>>c; for(float j=0;j<(int)(n/2.0+0.5);++j) { for(float i=0;i<n;++i) cout<<c; cout<<endl; } return 0; }
查验身份证
#include<iostream> #include<cstdio> #include<string> using namespace std; bool Check(string id) { char Z_M[11]={'1','0','X','9','8','7','6','5','4','3','2'}; char q[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; int s=0; for(int i=0;i<=16;++i) { if('0'<=id[i]&&id[i]<='9') s+=q[i]*(id[i]-'0'); else return false; } if(id[17]==Z_M[s%11]) return true; else return false; } int main() { int n,flag=1; string id; cin>>n; while(n--) { cin>>id; if(!Check(id)) { cout<<id<<endl; flag=0; } } if(flag) cout<<"All passed"; return 0; }
到底有多二
#include<iostream> #include<cstdio> #include<string> using namespace std; int main() { string n; float deg=1,len,c=0; cin>>n; len=n.length(); for(int i=0;i<len;++i) { if(n[i]=='2') c++; } if((n[len-1]-'0')%2==0) deg*=2; if(n[0]=='-') { deg*=1.5; len--; } printf("%.2f%%",deg*c/len*100.0); return 0; }
大笨钟
#include<iostream> #include<cstdio> #include<string> using namespace std; void Q(int h ) { for(int i=0;i<h;++i) cout<<"Dang"; } int main() { int h,m; scanf("%d:%d",&h,&m); if((0<=h&&h<=11)||(h==12&&m==0)) { printf("Only %02d:%02d. Too early to Dang.",h,m); return 0; } if(m==0) { Q(h%12); } else { Q(h%12+1); } return 0; }
谁先倒
#include<iostream> #include<cstdio> #include<string> using namespace std; struct Person { int hua; int han; }; int main() { Person a,b; int Sa,Sb,i=0,j=0; cin>>Sa>>Sb; int n; cin>>n; while(n--) { cin>>a.han>>a.hua>>b.han>>b.hua; int s=a.han+b.han; if(s==a.hua&&s!=b.hua) i++; else if(s==b.hua&&s!=a.hua) j++; else continue; if(i>Sa) { cout<<"A"<<endl<<j; return 0; } if(j>Sb) { cout<<"B"<<endl<<i; return 0; } //cout<<i<<":"<<j<<endl; } return 0; }
帅到没朋友💦
#include <iostream> #include <cstdio> using namespace std; int idm[100010] = {0}; int main() { int N; cin >> N; for (int i = 0; i < N; i++) { int n; cin >> n; for (int j = 0; j < n; j++) { int id; cin >> id;//输入盆友圈的标号 if(n > 1) { idm[id]++; } } } //凡是标记的不为0的都是有朋友的 int n, sum = 0; cin >> n; for (int i = 0; i < n; i++) { int sid; cin >> sid; if(idm[sid] == 0) { sum++; if(sum != 1) { printf(" "); } printf("%05d", sid); idm[sid] = 1;//不重复打印 } } if(!sum) { printf("No one is handsome\n"); } return 0; }
重要的话说三遍
#include<iostream> #include<algorithm> #include<string> using namespace std; const int N = 10005; int main() { int i=3; while(i--) cout<<"I'm gonna WIN!"<<endl; return 0; }
奇偶分家
#include<iostream> #include<cstdio> #include<string> using namespace std; int main() { int n,e=0,o=0,t; cin>>n; while(n--) { cin>>t; if(t%2) { o++; } else { e++; } } cout<<o<<" "<<e<<endl; return 0; }
输出GPLT
#include<iostream> #include<cstdio> #include<algorithm> #include<string> using namespace std; int node[4]; char dir[]={'G','P','L','T'}; bool Check() { for(int i=0;i<4;++i) { if(node[i]!=0) return true; } return false; } int main() { string n; cin>>n; transform(n.begin(),n.end(),n.begin(),::toupper); for(int i=0;i<n.length();++i) { switch(n[i]) { case 'G':node[0]++;break; case 'P':node[1]++;break; case 'L':node[2]++;break; case 'T':node[3]++;break; } } while(Check()) { for(int i=0;i<4;++i) { if(node[i]!=0) { cout<<dir[i]; node[i]--; } } } return 0; }
后天
#include<iostream> #include<cstdio> #include<algorithm> #include<string> using namespace std; int main() { int n; cin>>n; if(n==5)cout<<7; else cout<<(n+2)%7; return 0; }
正整数A+B
#include<iostream> #include<cstdio> #include<algorithm> #include<string> using namespace std; int Check(string a) { if(a[0]=='0') return false; // else if(a.length()>4||a.length()<1) return false; for(int i=0;i<a.length();++i) { if(!('0'<=a[i]&&a[i]<='9')) return false; } int A = atoi(a.c_str()); if(A<1||A>1000) return false; return A; } int main() { string a,b; int one=0,two=0,f=0; cin>>a; getchar(); getline(cin,b); if(one = Check(a)) { cout<<a; } else { cout<<"?"; f=1; } cout<<" + "; if(two = Check(b)) { cout<<b; } else { cout<<"?"; f=1; } cout<<" = "; if(f) { cout<<"?"; } else { cout<<one+two; } return 0; }
I Love GPLT
#include<iostream> #include<cstdio> #include<algorithm> #include<string> using namespace std; int main() { string a="I Love GPLT"; for(int i=0;i<a.length();++i) { cout<<a[i]<<endl; } return 0; }
出租
#include<stdio.h> #include<string> #include<iostream> #include<algorithm> using namespace std; int t[10]; int arr[10]; int index[18]; string tel=""; bool cmp(int a,int b) { return a>b; } int main() { cin>>tel; for(int i=0;i<tel.length();++i) { t[tel[i]-'0']++; } int a=0; for(int i=0;i<10;++i) { if(t[i]!=0) arr[a++]=i; } sort(arr,arr+a,cmp); cout<<"int[] arr = new int[]{"; for(int i=0;i<a-1;++i) { cout<<arr[i]<<","; } cout<<arr[a-1]; cout<<"};"<<endl; cout<<"int[] index = new int[]{"; int x=0; for(int i=0;i<tel.length();++i) { for(int j=0;j<a;++j) { // cout<<tel[i]<<","<<arr[j]<<endl; if(tel[i]-'0'==arr[j]) { //cout<<index[x]<<endl; index[x++]=j; } } } for(int i=0;i<x-1;++i) { cout<<index[i]<<","; } cout<<index[x-1]; cout<<"};"; return 0; }
判断素数
#include<stdio.h> #include<math.h> int Is_prime(int n) { if(n<=1) return 0; for(int i=2;i<=sqrt(n);++i) { if(n%i==0) return 0; } return 1; } int main() { int n,m; scanf("%d",&n); while(n--) { scanf("%d",&m); if(Is_prime(m)) printf("Yes\n"); else printf("No\n"); } return 0; }
是不是太胖了
#include<stdio.h> #include<string> #include<iostream> #include<algorithm> using namespace std; int main() { float n; cin>>n; printf("%.1f",(n-100)*0.9*2); return 0; }
一帮一
#include<stdio.h> #include<string> #include<iostream> #include<vector> #include<algorithm> using namespace std; const int M = 52; vector<string> girl; vector<string> boy; struct p { int sex; string name; }stu[M]; int main() { int n; cin>>n; for(int i=0;i<n;++i) cin>>stu[i].sex>>stu[i].name; for(int i=0;i<n;++i) { if(stu[i].sex==0) { for(int j=n-1;j>=0;--j) { if(stu[j].sex==1) { cout<<stu[i].name<<" "<<stu[j].name<<endl; stu[i].sex=-1; stu[j].sex=-1; break; } } } else if(stu[i].sex==1) { for(int j=n-1;j>=0;--j) { if(stu[j].sex==0) { cout<<stu[i].name<<" "<<stu[j].name<<endl; stu[i].sex=-1; stu[j].sex=-1; break; } } } } return 0; }
到底是不是太胖了
#include<stdio.h> #include<string> #include<iostream> #include<vector> #include<algorithm> #include<cmath> using namespace std; const double Tag=0.0000000000000001; int main() { double n,h,w; cin>>n; while(n--) { cin>>h>>w; double stand = 2.0 *(h-100)*0.9; // cout<<stand*0.1<<":"<<w - stand<<endl; if(w - stand < -stand*0.1||abs(w - stand+stand*0.1)<Tag) { cout<<"You are tai shou le!"<<endl; } else if(w-stand>stand*0.1||abs(w - stand-stand*0.1)<Tag) { // cout<<w - stand-stand*0.1<<endl; cout<<"You are tai pang le!"<<endl; } else { cout<<"You are wan mei!"<<endl; } } return 0; }
Left-pad
#include <iostream> #include <cstdio> #include<string> using namespace std; int idm[100010] = {0}; int main() { int n; char c; scanf("%d %c",&n,&c); getchar(); string s; getline(cin,s); int len = s.length(); if(len<n) { for(int i=0;i<n-len;++i)cout<<c; cout<<s; } else { cout<<s.substr(len-n); } return 0; }
出生年
#include<cstdio> #include<cstring> #include<string> #include<iostream> #include<vector> #include<algorithm> using namespace std; int y,t,i=0; bool Check(int y) { char year[5]={}; int sorts[11]={}; int c=0; sprintf(year,"%04d",y); for(int i=0;i<4;++i) sorts[year[i]-'0']++; for(int i=0;i<10;++i) { if(sorts[i]!=0) c++; } if(c==t) return true; else return false; } int main() { cin>>y>>t; while(1) { if(Check(y+i)) { printf("%d %04d",i,y+i); break; } i++; } return 0; }
点赞
#include<cstdio> #include<cstring> #include<string> #include<iostream> #include<vector> #include<map> #include<algorithm> using namespace std; const int M = 10005; struct o { int f; int time; }Person[M]; bool cmp(o a,o b) { if(a.time==b.time) return a.f>b.f; else return a.time>b.time; } int main() { int m,n,f; map<int,int>lao; cin>>n; while(n--) { cin>>m; for(int i=0;i<m;++i) { cin>>f; lao[f]++; } } int p=0; for(map<int,int>::iterator i=lao.begin();i!=lao.end();++i) { Person[p].f = i->first; Person[p++].time = i->second; } sort(Person,Person+p,cmp); cout<<Person[0].f<<" "<<Person[0].time; return 0; }
情人节
#include<cstdio> #include<cstring> #include<string> #include<iostream> #include<vector> #include<map> #include<algorithm> using namespace std; int main() { string m,A="*",B="*"; int c=0; while(1) { cin>>m; c++; if(m==".") break; else if(c==14) { B=m; } else if(c==2) { A=m; } } if(A!="*"&&B!="*") cout<<A<<" and "<<B<<" are inviting you to dinner..."<<endl; else if(A!="*") cout<<A<<" is the only one for you..."<<endl; else cout<<"Momo... No one is for you ..."<<endl; return 0; }
A乘以B
#include<cstdio> #include<cstring> #include<string> #include<iostream> #include<vector> #include<map> #include<algorithm> using namespace std; int main() { int a,b; cin>>a>>b; cout<<a*b; return 0; }
A除以B
#include<cstdio> #include<cstring> #include<string> #include<iostream> #include<vector> #include<map> #include<algorithm> using namespace std; int main() { float a,b; cin>>a>>b; if(b<0) { printf("%.0f/(%.0f)=%.2f",a,b,a/b); } else if(b==0) { printf("%.0f/%.0f=Error",a,b); } else { printf("%.0f/%.0f=%.2f",a,b,a/b); } return 0; }
新世界
#include<iostream> using namespace std; int main() { cout<<"Hello World"<<endl; cout<<"Hello New World"; return 0; }
古风排版
#include <iostream> #include <cstdio> #include<string> #include<cstring> using namespace std; int main() { int n; string s; cin>>n;getchar(); getline(cin,s); int len = s.length(); int m = len/n; m = m*n>=len ? m:m+1; // cout<<len<<","<<n<<","<<m<<endl; int S=0; char c[n][m]; memset(c,' ',sizeof(c)); for(int i=m-1;i>=0;--i) { for(int j=0;j<n;++j) { if(S>=len) {break;break;} c[j][i]=s[S++]; // cout<<c[j][i]; } } //cout<<"************************************"<<endl; for(int j=0;j<n;++j) { for(int i=0;i<m;++i) { cout<<c[j][i]; } cout<<endl; } return 0; }
最佳情侣身高差
#include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<cmath> using namespace std; const int M = 100007; int main() { int n; string sex; float h; cin>>n; for(int i=0;i<n;++i) { cin>>sex>>h; if(sex=="F") { printf("%.2f\n",h*1.09); } else if(sex=="M") { printf("%.2f\n",h/1.09); } } return 0; }
寻找250
#include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<cmath> using namespace std; const int M = 100007; int main() { int n; int i=0; while(cin>>n) { i++; if(n==250) { cout<<i; break; } } return 0; }
日期格式化
#include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<cmath> using namespace std; const int M = 100007; int main() { int n,y,r; scanf("%d-%d-%d",&y,&r,&n); printf("%04d-%02d-%02d",n,y,r); return 0; }
阅览室
#include<iostream> #include<string> #include<cstdio> #include<cstring> using namespace std; const int ID = 1000+5; float s[ID];//开始时间 int k[ID];//借阅,1:借过 int main() { int n,id,conut=0; float time=0,h,m; cin>>n;getchar(); char key; while(1) { scanf("%d %c %f:%f",&id,&key,&h,&m); //cout<<id<<","<<key<<","<<h<<","<<m<<endl; getchar(); if(id==0) { n--; if(conut==0)printf("0 0\n"); else printf("%d %.0f\n",conut,time/conut); conut=0,time=0; memset(s,0,sizeof(s)); memset(k,0,sizeof(k)); if(n==0)break; } if(key=='S') { k[id]=1; s[id]=h*60+m; } else if(key=='E'&&k[id]) { conut++; //cout<<time<<endl; time+=(h*60+m)-s[id]; k[id]=0,s[id]=0; } } return 0; }
稳赢
#include<iostream> #include<string> using namespace std; int k; string Out(string user,int i) { // cout<<"<<"<<user<<endl; if(i>k) return user; if(user=="ChuiZi") { return "Bu"; } else if(user=="JianDao") { return "ChuiZi"; } else { return "JianDao"; } } int main() { int i=0;cin>>k; string user; while(1) { cin>>user; if(user=="End")break; i++;cout<<Out(user,i)<<endl; if(i>k) i=0; } return 0; }
宇宙无敌大招呼
#include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<cmath> using namespace std; const int M = 100007; int main() { string n; cin>>n; cout<<"Hello "<<n; return 0; }
整除光棍💦
#include<stdio.h> #include<iostream> using namespace std; /**模拟除法**/ int main() { int s=1,n,num=0,k=0; scanf("%d",&n); while(1) { if(s/n!=0)k=1;//判断首位是否为零,如果为零不需要输出 num++;//每次循环加一位 if(k)printf("%d",s/n); //cout<<"除数:"<<s; s=s%n;//余数 //cout<<"余数:"<<s<<endl; if(s==0)break; s=s*10+1;//新的除数 } printf(" %d",num); }
装睡
#include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<cmath> using namespace std; const int M = 100007; struct p { string name; int hx; int mb; }t; int main() { int n; cin>>n; for(int i=0;i<n;++i) { cin>>t.name>>t.hx>>t.mb; if((t.hx<15||t.hx>20||t.mb<50||t.mb>70)) cout<<t.name<<endl; } return 0; }
矩阵A乘以B
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; struct o { int r; int c; }; int main() { o A,B; cin>>A.r>>A.c; int a[A.r][A.c]; for(int i=0;i<A.r;++i) for(int j=0;j<A.c;++j) cin>>a[i][j]; cin>>B.r>>B.c; if(A.c!=B.r) { cout<<"Error: "<<A.c<<" != "<<B.r<<endl; return 0; } int b[B.r][B.c]; for(int i=0;i<B.r;++i) for(int j=0;j<B.c;++j) cin>>b[i][j]; cout<<A.r<<" "<<B.c<<endl; for(int i=0;i<A.r;++i) { for(int j=0;j<B.c;++j) { int t=0; for(int k=0;k<A.c;++k) t += a[i][k]*b[k][j]; if(j!=B.c-1) cout<<t<<" "; else cout<<t; } cout<<endl; } return 0; }
天梯赛座位分配💦
#include<stdio.h> int main() { int n,m,max=0,map[101][101]={0},s[101][101]={0}; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&m); if(m>max) max=m; for(int j=0;j<m*10;j++) { map[i][j]=1; //初始化 } } int flag=-1; //flag用于记录是否是同一所学校 int sum=1; //sum表示当前座位号 for(int i=0;i<max*10;i++) { for(int j=0;j<n;j++) { if(map[j][i]) { if(flag!=j) { //不是一个学校的依次输出 s[j][i]=sum++; flag=j; } else { //是一个学校的间隔输出 sum+=1; s[j][i]=sum++; flag=j; } } } } //输出 for(int i=0;i<n;i++) { printf("#%d\n",i+1); for(int j=0;j<max*10;j++) if(s[i][j]) { printf("%d%c",s[i][j],(j+1)%10?' ':'\n'); } } return 0; }
倒数第N个字符串💦
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int main() { int L, N, x, t, i, s, a; cin>>L>>N; x = L; t = 1; while(x--) {//计算长度为L的字符串个数 t *= 26; } s = t - N;//计算顺序位数 for(i = L - 1; i >= 0; i--) {//输出每一位的字符 a = s/(int)pow(26,i); printf("%c", a + 'a'); s = s%(int)pow(26,i); } return 0; }
打折
#include<iostream> #include<cstdio> #include<string> using namespace std; int main() { float l,m; cin>>l>>m; printf("%.2f",l*0.1*m); return 0; }
2018我们要赢
#include<iostream> #include<cstdio> #include<string> using namespace std; int main() { cout<<"2018"<<endl<<"wo3 men2 yao4 ying2 !"<<endl; return 0; }
电子汪
#include<iostream> #include<cstdio> #include<string> using namespace std; int main() { int a,b; cin>>a>>b; for(int i=0;i<a+b;++i) cout<<"Wang!"; return 0; }
福到了
#include<iostream> #include<cstdio> using namespace std; int main() { char a; int b; int flag=1; cin>>a;getchar(); cin>>b;getchar(); string c[b]={}; for(int i=0;i<b;++i) getline(cin,c[i]); for(int i=0;i<b/2;++i) for(int j=0;j<b;++j) { if(c[i][j]!=c[b-1-i][j]) { flag=0;break; } } if(flag) { cout<<"bu yong dao le"<<endl; } for(int i=b-1;i>=0;--i) { for(int j=b-1;j>=0;--j) { if(c[i][j]!=' ') cout<<a; else cout<<' '; } if(i!=0) cout<<endl; } return 0; }
谁是赢家
#include<iostream> #include<cstdio> #include<string> using namespace std; int main() { int Pa,Pb,a,b,c; cin>>Pa>>Pb; cin>>a>>b>>c; if(Pa>Pb&&(a+b+c)<3) cout<<"The winner is a: "<<Pa<<" + "<<!a+!b+!c<<endl; else if(Pa<Pb&&(a+b+c)==0) cout<<"The winner is a: "<<Pa<<" + "<<!a+!b+!c<<endl; else cout<<"The winner is b: "<<Pb<<" + "<<a+b+c<<endl; return 0; }
猜数字
#include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<cmath> using namespace std; const int M = 100007; struct p { string name; float num; }Person[M]; int main() { int n; float sum=0; cin>>n; for(int i=0;i<n;++i) { cin>>Person[i].name>>Person[i].num; sum+=Person[i].num; } sum=sum/n/2; string P_name=Person[0].name; float mins = abs(Person[0].num-sum); for(int i=1;i<n;++i) { if(abs(Person[i].num-sum)<mins) { mins = abs(Person[i].num-sum); P_name = Person[i].name; } } //cin>>sum; printf("%.d ",(int)sum); cout<<P_name; return 0; }
PTA使我精神焕发
#include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<cmath> using namespace std; int main() { cout<<"PTA shi3 wo3 jing1 shen2 huan4 fa1 !"; return 0; }
6翻了
#include<iostream> #include<string> using namespace std; int main() { string s,r; int c=0; getline(cin,s); int len=s.length(); for(int i=0;i<len;++i) { if(s[i]!='6'&&c==0) cout<<s[i]; else if(s[i]=='6') c++; if(c!=0&&s[i]!='6') { // cout<<"c="<<c<<endl; if(c<=3) { while(c--) cout<<6; } else if(c<=9) cout<<9; else cout<<27; c=0; cout<<s[i]; } } if(c!=0) { if(c<=3) { while(c--) cout<<6; } else if(c<=9) cout<<9; else cout<<27; } return 0; }
敲笨钟🌸
心理阴影面积
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; int main() { float x,y; scanf("%f%f",&x,&y); printf("%.0f",5000-(x*y*0.5)-((100+y)*(100-x)*0.5)); return 0; }
新胖子公式
#include<iostream> #include<stdio.h> #include<vector> using namespace std; const int N = 10005; int main() { float h,w; cin>>w>>h; float r = w/(h*h); printf("%.1f\n",r); if(r>25) cout<<"PANG"<<endl; else cout<<"Hai Xing"<<endl; return 0; }
幸运彩票
#include<iostream> #include<stdio.h> #include<vector> using namespace std; int main() { int n,a,b; string t; cin>>n; while(n--) { cin>>t; a=0,b=0; for(int i=0;i<3;++i) a+=t[i]-'0'; for(int i=3;i<6;++i) b+=t[i]-'0'; if(a==b) { cout<<"You are lucky!"<<endl; } else { cout<<"Wish you good luck."<<endl; } } return 0; }
吃鱼还是吃肉
#include<iostream> #include<stdio.h> #include<vector> using namespace std; int n,a,b,c; void Check_h(int t) { if(b<t) { cout<<"duo chi yu!"; } else if(b>t) { cout<<"ni li hai!"; } else if(b==t) { cout<<"wan mei!"; } } void Check_w(int t) { if(c<t) { cout<<" duo chi rou!"<<endl; } else if(c>t) { cout<<" shao chi rou!"<<endl; } else if(c==t) { cout<<" wan mei!"<<endl; } } int main() { cin>>n; while(n--) { cin>>a>>b>>c; if(a==0) {//女生 Check_h(129); Check_w(25); } else {//男生 Check_h(130); Check_w(27); } } return 0; }
估值一亿的AI核心代码🌸
L2
月饼
#include<stdio.h> #include<algorithm> #include<iostream> using namespace std; const int M = 1e5+4; float kc[M],dj[M],t,sum; struct o { float kc; float dj; }goods[M]; bool cmp(o a,o b) { return a.dj>b.dj; } int main() { int n; float d; scanf("%d%f",&n,&d); for(int i=0;i<n;++i) scanf("%f",&goods[i].kc); for(int i=0;i<n;++i) { scanf("%f",&t); goods[i].dj =t/goods[i].kc; } sort(goods,goods+n,cmp); float sum=0; for(int i=0;i<n;++i) { if( d > goods[i].kc) { sum+=goods[i].kc*goods[i].dj; } else { sum+=goods[i].dj*d;break; } d-=goods[i].kc; } printf("%.2f",sum); return 0; }
搜索树判断💦
#include<iostream> #include<cstdlib> #include<cstdio> using namespace std; const int maxn=1005; typedef struct node* BinTree; int n,s=0,t=0,k=0; int a[maxn],b[maxn],c[maxn],d[maxn]; struct node{ int data; BinTree left,right; }; //建立二叉搜索树 BinTree CreatTree(BinTree BST,int x) { if(!BST) { BST=(BinTree)malloc(sizeof(struct node)); BST->data=x; BST->left=BST->right=NULL; } else { if(x<BST->data) BST->left=CreatTree(BST->left,x); else if(x>=BST->data) BST->right=CreatTree(BST->right,x); } return BST; } //先序遍历 void PreorderTraversal(BinTree BST) { if(BST) { b[s++]=BST->data; PreorderTraversal(BST->left); PreorderTraversal(BST->right); } } //镜面先序输出 void MirrorPreorderTraversal(BinTree BST) { if(BST) { c[t++]=BST->data; MirrorPreorderTraversal(BST->right); MirrorPreorderTraversal(BST->left); } } //后序遍历 int k1=0; void PostorderTraversal(BinTree BST) { if(BST) { PostorderTraversal(BST->left); PostorderTraversal(BST->right); if(k1) cout<<" "; cout<<BST->data; k1=1; } } //镜面后序遍历 int k2=0; void MirrorPostorderTraversal(BinTree BST) { if(BST) { MirrorPostorderTraversal(BST->right); MirrorPostorderTraversal(BST->left); if(k2) cout<<" "; cout<<BST->data; k2=1; } } int main() { BinTree BST; BST=NULL; cin>>n; for(int i=0;i<n;i++) { scanf("%d",&a[i]); BST=CreatTree(BST,a[i]); } PreorderTraversal(BST); MirrorPreorderTraversal(BST); int flag1=1,flag2=1; for(int i=0;i<n;i++) { if(a[i]!=b[i]) { flag1=0; break; } } for(int i=0;i<n;i++) { if(a[i]!=c[i]) { flag2=0; break; } } if(flag1==0&&flag2==0) cout<<"NO"<<endl; else { cout<<"YES"<<endl; if(flag1) PostorderTraversal(BST); if(flag1==0&&flag2) MirrorPostorderTraversal(BST); } return 0; }
树的遍历💦
#include <bits/stdc++.h> using namespace std; int mid[35],post[35]; struct node{ int lson,rson; }f[35]; int build(int lmid,int rmid,int lpost,int rpost) //lmid,rmid表示中序遍历 lpost,rpost表示后序遍历 { if(lmid>rmid) return 0; int root=post[rpost]; int fa=lmid; while(mid[fa]!=root) fa++; //在中序遍历中找到根节点 int len=fa-lmid;//在中序里面,根在中间,用根把整个树给二分 f[root].lson=build(lmid,fa-1,lpost,lpost+len-1);//用后序找根 f[root].rson=build(fa+1,rmid,lpost+len,rpost-1); return root; } void bfs(int x) { queue <int> q; vector <int> v; q.push(x); while(!q.empty()) { int w=q.front(); q.pop(); if(!w) break; v.push_back(w); if(f[w].lson) q.push(f[w].lson); if(f[w].rson) q.push(f[w].rson); } int len=v.size(); for(int i=0;i<len;i++) if(i==0)printf("%d",v[i]); else printf(" %d",v[i]); return; } int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&post[i]); //后序 for(int i=0;i<n;i++) scanf("%d",&mid[i]); //中序 int root=post[n-1];//后序遍历的最后一个是根 build(0,n-1,0,n-1); bfs(root); return 0; }
最长对称子串
#include <bits/stdc++.h> using namespace std; bool Check(string s) { int len = s.length(); for(int i=0;i<len/2;++i) { if(s[i]!=s[len-i-1]) return false; } return true; } int main() { string s; getline(cin,s); int len=s.length(),son_len=1; for(int i=0;i<len;++i) { for(int j=1;i+j<=len;++j) { string son = s.substr(i,j); if(Check(son)) { if(son_len<son.length()) { son_len = son.length(); } //cout<<son_len<<","<<son.length()<<endl; } } } cout<<son_len<<endl; return 0; }
抢红包
#include <bits/stdc++.h> using namespace std; const int M = 1e4+5; struct o { int id; int c; float m; }money[M]; bool cmp(o a,o b) { if(a.m==b.m) { if(a.c==b.c) return a.id<b.id; else return a.c>b.c; } else return a.m>b.m; } int main() { int n,k,p; float m; cin>>n; for(int i=1;i<=n;++i) { money[i].id=i; money[i].c=0; money[i].m=0; } for(int i=1;i<=n;++i) { cin>>k; while(k--) { cin>>p>>m; money[p].m+=m; money[i].m-=m; money[p].c++; } } sort(money+1,money+1+n,cmp); for(int i=1;i<=n;++i) { printf("%d %.2f\n",money[i].id,money[i].m/100); } return 0; }
排座位
#include <bits/stdc++.h> using namespace std; const int M = 1e4+5; vector<int> foe[M]; int father[M]; void init(int n) { for(int i=1;i<=n;++i) father[i]=i; } int find_father(int a) { return father[a]==a?a:find_father(father[a]); } void make(int a,int b) { a=find_father(a); b=find_father(b); if(a!=b) father[a]=b; } bool Check(int a,int b) { for(int i=0;i<foe[a].size();++i) { if(foe[a][i]==b) return false; } return true; } int main() { int n,a,b,k,m,flag; cin>>n>>m>>k; init(n); while(m--) { cin>>a>>b>>flag; if(flag==1) { make(a,b); } else { foe[a].push_back(b); foe[b].push_back(a); } } while(k--) { cin>>a>>b; if(Check(a,b))//没有敌对关系 { if(find_father(a)==find_father(b)) cout<<"No problem"<<endl; else cout<<"OK"<<endl; } else { if(find_father(a)==find_father(b)) cout<<"OK but..."<<endl; else cout<<"No way"<<endl; } } return 0;
玩转二叉树💦
#include <bits/stdc++.h> using namespace std; int mid[35],post[35]; struct node{ int lson,rson; }f[35]; int build(int lmid,int rmid,int lfront,int rfront) //lmid,rmid表示中序遍历 lpost,rpost表示后序遍历 { //cout<<lmid<<","<<rmid<<","<<lfront<<","<<rfront<<endl; if(lmid>rmid) return 0; int root=post[lfront]; //cout<<root<<endl; int fa=lmid; while(mid[fa]!=root) fa++; //在中序遍历中找到根节点 int len=fa-lmid+1;//在中序里面,根在中间,用根把整个树给二分 f[root].rson=build(lmid,fa-1,lfront+1,lfront+len);//用后序找根 f[root].lson=build(fa+1,rmid,lfront+len,rfront); return root; } void bfs(int x) { queue <int> q; vector <int> v; q.push(x); while(!q.empty()) { int w=q.front(); q.pop(); if(!w) break; v.push_back(w); if(f[w].lson) q.push(f[w].lson); if(f[w].rson) q.push(f[w].rson); } int len=v.size(); for(int i=0;i<len;i++) if(i==0)printf("%d",v[i]); else printf(" %d",v[i]); return; } int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&mid[i]); //中序 for(int i=0;i<n;i++) scanf("%d",&post[i]); //前序 int root=post[0]; build(0,n-1,0,n-1); bfs(root); return 0; }
关于堆的判断💦
#include<iostream> #include<cstdio> #include<cstring> #include<map> using namespace std; const int maxm = 10010; const int inf = 99999999; int a[maxm],cnt; void creat(int x){ a[++cnt] = x; int t = cnt; // cout<<t<<endl; while(t>1&&(a[t/2]>a[t])){ //模板,建堆的标准模板 //for(int i=1;i<=cnt;++i) cout<<a[i]<<" "; //cout<<endl; a[t] = a[t/2]; a[t/2] = x; t /= 2; } a[t] = x; } int main() { int n,m,x,y; string s; map<int,int> p; scanf("%d%d",&n,&m); cnt = 0; for(int i=1;i<=n;i++){ scanf("%d",&x); creat(x); } //for(int i=1;i<=cnt;++i) cout<<a[i]<<" "; //cout<<endl; for(int i=1;i<=n;i++){ p[a[i]] = i; } for(int i=0;i<m;i++){ scanf("%d",&x); cin>>s; if(s[0]=='a'){ scanf("%d",&y); getline(cin,s); if(p[x]/2==p[y]/2){ printf("T\n"); }else{ printf("F\n"); } }else{ cin>>s; cin>>s; if(s[0]=='r'){ if(p[x]==1){ printf("T\n"); }else{ printf("F\n"); } }else if(s[0]=='p'){ cin>>s; cin>>y; if(p[x]==p[y]/2){ printf("T\n"); }else{ printf("F\n"); } }else{ cin>>s; cin>>y; if(p[x]/2==p[y]){ printf("T\n"); }else{ printf("F\n"); } } } } return 0; }
互评成绩
#include<iostream> #include<cstdio> #include<algorithm> #include<string> #include<cstdlib> #include<vector> #include<cmath> typedef long long ll; using namespace std; const int M=1e-5; vector<float> arr; int main() { int n,k,m; cin>>n>>k>>m; while(n--) { vector<float>l;l.clear(); float t; for(int i=0;i<k;++i) { cin>>t;l.push_back(t); } float sum=0; sort(l.begin(),l.end()); for(int i=1;i<k-1;++i) { //cout<<l[i]<<endl; sum+=l[i]; } arr.push_back(sum/(k-2)); // break; } sort(arr.begin(),arr.end()); for(int i=arr.size()-m;i<arr.size();++i) { if(i==arr.size()-m) printf("%.3f",arr[i]); else printf(" %.3f",arr[i]); } return 0; }
部落💦
#include<iostream> #include<set> using namespace std; const int N = 1e4 + 1; int fri[N]; void init(){ for(int i = 1; i <= N; i++) fri[i] = i; } int find(int a){ int tem1 = a,tem2; while(a != fri[a]) a = fri[a]; while(tem1 != a){ tem2 = fri[tem1]; fri[tem1] = a; tem1 = tem2; } return a; } void Union(int a,int b){ int fri_a = find(a); int fri_b = find(b); if(fri_a != fri_b) fri[fri_a] = fri_b; } int main(){ int n,k,a,b; set<int >num_f,num_p; //统计人数和部落数 cin>>n; init(); while(n--){ cin>>k>>a; num_p.insert(a); for(int i = 1; i < k; i++){ cin>>b; num_p.insert(b); Union(a, b); //将一个圈子的人整合 } } for(int i = 1; i <= num_p.size(); i++){ //应为所有的都是连续的编号 num_f.insert(find(i)); } //集合自动去重 cout<<num_p.size()<<" "<<num_f.size()<<endl; cin>>n; while(n--){ cin>>a>>b; if(find(a) == find(b)) cout<<"Y"<<endl; else cout<<"N"<<endl; } return 0; }
wa:
#include<iostream> #include<vector> #include<map> using namespace std; const int maxn = 10051005; int stu[maxn],height[maxn]; int main() { int n,m,t,sum=0; map<int,int>lao; cin>>n; int key=1,flag=0; for(int i=0;i<n;++i) { cin>>m; vector<int>temp;temp.clear(); for(int j=0;j<m;++j) { cin>>t; if(i==0) lao[t]=1; else { temp.push_back(t); for(map<int,int>::iterator it=lao.begin();it!=lao.end();++it) { if(it->first==t) { flag=1; key=it->second; } } } } if(i==0) continue; if(flag) { flag=0; for(int k=0;k<temp.size();++k) { lao[temp[k]]=key; } } else { key++; for(int k=0;k<temp.size();++k) { lao[temp[k]]=key; } } } cin>>m; for(map<int,int>::iterator it=lao.begin();it!=lao.end();++it) { if(it->second!=0) { //cout<<it->first<<","<<it->second<<endl; sum++; } } // cout<<"*****************************"<<endl; cout<<sum<<" "<<key<<endl; int a,b,key_a,key_b; for(int i=0;i<m;++i) { cin>>a>>b; for(map<int,int>::iterator it=lao.begin();it!=lao.end();++it) { if(it->first==a) { key_a=it->second; } if(it->first==b) { key_b=it->second; } } if(key_a==key_b) cout<<"Y"<<endl; else cout<<"N"<<endl; } return 0; }
集合相似度
这道题虽然做出来了,但是真的老费劲了,用到了set,vector,还有set_intersection,set_union。感觉考的就是看你STL的掌握
#include<iostream> #include<cstdlib> #include<vector> #include<algorithm> #include<cstdio> #include<set> using namespace std; int main() { int n,m,a,b,t; scanf("%d",&n); set<int> v[n]; for(int i=0;i<n;++i) { scanf("%d",&m); for(int j=0;j<m;++j) cin>>t,v[i].insert(t); } scanf("%d",&m); while(m--) { scanf("%d%d",&a,&b); a--,b--; //cout<<a<<b; vector<int>temp; temp.resize(min(v[a].size(),v[b].size())); vector<int>::iterator it = set_intersection(v[a].begin(),v[a].end(),v[b].begin(),v[b].end(),temp.begin()); temp.resize(it-temp.begin()); float Nc=it -temp.begin(); // for(int tt=0;tt<temp.size();++tt) // cout<<temp[tt]<<endl; temp.resize(v[a].size()+v[b].size()); it = set_union(v[a].begin(),v[a].end(),v[b].begin(),v[b].end(),temp.begin()); float Nt=it - temp.begin(); // cout<<Nc<<","<<Nt<<endl; printf("%.2f%%\n",Nc/Nt*100); } return 0; }
是否完全二叉搜索树💦
#include<bits/stdc++.h> using namespace std; int a[105]; int n; void build(int i,int v) { if(a[i]==-1) { a[i]=v; return; } if(v>a[i])build(i*2,v); else build(i*2+1,v); } int main() { scanf("%d",&n); memset(a,-1,sizeof(a)); for(int i=0;i<n;i++) { int x; scanf("%d",&x); build(1,x); } int t=0,i=0; while(t<n) { while(a[i]==-1) i++; if(t)printf(" %d",a[i]); else printf("%d",a[i]); t++; i++; } if(i==n+1)printf("\nYES"); else printf("\nNO"); }
列车调度 💦
#include<iostream> using namespace std; int main(){ int n; cin>>n; int root[n]; int len = 0; int k; while(n--) { cin>>k; if(len == 0 || root[len-1] < k) { root[len++] = k; } else { //折半查找法 int one = 0,two = len-1; while(one < two) { int mid = one + (two-one)/2; if(root[mid] > k) { two = mid - 1; } else { one = mid + 1; } } root[one] = k; } } cout<<len; return 0; }
人以群分
#include<iostream> #include<vector> #include<cmath> #include<algorithm> using namespace std; int main() { int n,t; vector<int> a; cin>>n; for(int i=0;i<n;++i) cin>>t,a.push_back(t); sort(a.begin(),a.end()); int len=a.size(),b; b= len/2; // if(len%2==1) b = len/2 + 1; // else b = len/2; int s1=0,s2=0; for(int i=0;i<b;++i) s1+=a[i]; for(int i=b;i<len;++i) s2+=a[i]; cout<<"Outgoing #: "<<len-b<<endl; cout<<"Introverted #: "<<b<<endl; cout<<"Diff = "<<abs(s1-s2)<<endl; return 0; }
重排链表💦
#include<cstdio> #include<vector> using namespace std; struct note{ int begin; int data; int next; }q[100010]; int main(){ vector<note> kk,ans; int n,first,i; scanf("%d%d",&first,&n); int td,tb,tn; for(i=0;i<n;i++) { scanf("%d%d%d",&tb,&td,&tn); q[tb]={tb,td,tn}; }//将所有的地址存储起来 note ss=q[first]; while(1) { kk.push_back(ss); if(ss.next ==-1) break; ss=q[ss.next]; //排序 } int l=0,r=kk.size()-1; while(1) { ans.push_back(kk[r]);//从最后一位开始 r--; if((r+1)-(l-1)==1) break; ans.push_back(kk[l]);//从第一位开始 l++; if((r+1)-(l-1)==1) break; } for(i=0;i<ans.size() ;i++){ if(i!=ans.size()-1) printf("%05d %d %05d\n",ans[i].begin ,ans[i].data ,ans[i+1].begin ); else printf("%05d %d -1\n",ans[i].begin ,ans[i].data ); } return 0; }
图着色问题
#include<stdio.h> #include<string.h> #include<vector> #include<iostream> #include<set> using namespace std; const int M =1e3+4; vector<int> p[M],q; int v,e,a,b,k; void laolao() { for(int i=1;i<=v;++i) { // cout<<q[i-1]<<":"; for(int j=0;j<p[i].size();++j) { // cout<<q[p[i][j]-1]<<" "; if(q[p[i][j]-1]==q[i-1]) { cout<<"No"<<endl; return; } } // cout<<endl; } cout<<"Yes"<<endl; return; } void lao() { int n,t; cin>>n; set<int> select; while(n--) { q.clear(); select.clear(); for(int i=0;i<v;++i) { cin>>t; q.push_back(t); select.insert(t); } //cout<<">>>"<<select.size()<<endl; if(select.size()!=k) cout<<"No"<<endl; else laolao(); } } int main() { cin>>v>>e>>k; for(int i=0;i<e;++i) { cin>>a>>b; p[a].push_back(b); p[b].push_back(a); } lao(); return 0; }
分而治之
ac:割边
#include<iostream> #include<map> #include<algorithm> using namespace std; /* 割边 */ const int maxm = 10010; struct node { int x; int y; }edge[maxm]; int v[maxm]; int main() { int n,m,k,t,x,y,city,l,p,flag; scanf("%d %d",&n,&m); for(int i=0;i<m;i++) { scanf("%d %d",&edge[i].x,&edge[i].y);//记录边 } scanf("%d",&k); for(int i=0;i<k;i++) { map<int,int> m2;//城市,1攻占 scanf("%d",&t); for(int j=0;j<t;j++) { scanf("%d",&city); m2[city] = 1; } int j = 0; for(j=0;j<m;j++) { if(m2[edge[j].x]!=1&&m2[edge[j].y]!=1) { //如果一条边的两个结点城市没有被同时攻占,就代表失败 printf("NO\n"); break; } } if(j>=m) { printf("YES\n"); } } return 0; }
wa: 再改了再改了
#include<stdio.h> #include<string.h> #include<vector> #include<iostream> #include<set> using namespace std; const int M =1e3+4; vector<int> p[M]; void laolao(int n) { vector<int> q[n]; for(int i=1;i<=n;++i) q[i]=p[i]; int t,v; cin>>t; while(t--) { cin>>v; for(int j=0;j<q[v].size();++j) q[q[v][j]].pop_back(); q[v].clear(); for(int i=1;i<=n;++i) { cout<<i<<":"; for(int j=0;j<q[i].size();++j) { cout<<q[i][j]<<" "; } cout<<endl; } } int c=0; for(int i=1;i<=n;++i) { if(q[i].size()>0) {c=1;break;} } if(c==0) cout<<"YES"<<endl; else cout<<"No"<<endl; } void lao(int m) { int n; cin>>n; while(n--) { laolao(m); } } int main() { int n,m,a,b; cin>>n>>m; for(int i=0;i<m;++i) { cin>>a>>b; p[a].push_back(b); p[b].push_back(a); } lao(n); return 0; }