L1-002 打印沙漏
细节:就是在 (i>j&&i+j<r+1) 这个区间里才有空格,然后就是 for 循环 for(r=1; ;r+=2) 条件不满足之后还会再+2;
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; int main(){ int n; char c; scanf("%d %c",&n,&c); // int r=1,sum=1; // for(r=3;sum<=n ;r+=2){ // sum+=r*2; // } // sum-=r*2; // r-=2; int r=1,sum=0; for(r=1; ;r+=2){ if(r==1)sum+=r; else sum+=r<<1; if(sum>n)break; } sum-=r<<1; r-=2; // cout<<sum<<" "<<r<<endl; for(int i=1;i<=r;i++){ for(int j=1;j<=r;j++){ if(i<=j&&i+j<=r+1||i>=j&&i+j>=r+1)cout<<c; else if(i>j&&i+j<r+1) cout<<" "; } cout<<endl; } cout<<n-sum<<endl; return 0; }
L1-003
搞个map即可,然后注意map只能从first 映射到second
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; int main(){ string s; cin>>s; map<int,int>mp; int len=s.size(); for(int i=0;i<len;i++){ mp[s[i]-'0']++; } map<int,int>::iterator it; for(it=mp.begin();it!=mp.end();it++){ cout<<it->fi<<":"<<it->se<<endl; } return 0; }
L1-006 连续因子
解法:就是你把他的全部因子求出来,然后去枚举每一个因子的最大连续次数,最后取一个最大值即可;
然后如果n是素数,输出n即可;
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; int main(){ int n; cin>>n; vector<int>v; int ans,max=-1; for(int i=2;i<=sqrt(n)+1;i++){ if(n%i==0)v.pb(i); } int len=v.size(); for(int i=0;i<len;i++){ int sum=v[i],cnt=0; for(int j=v[i]+1; ;j++){ sum*=j; if(n%sum!=0)break; cnt++; } if(cnt>max){ ans=v[i]; max=cnt; } } if(len==0)cout<<1<<endl<<n<<endl; else { cout<<max+1<<endl; for(int i=0;i<=max;i++){ if(i)cout<<"*"; cout<<ans+i; } cout<<endl; } return 0; }
L1-008 求整数段和
细节,最后不要打出两个换行即可;
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; int main(){ int a,b; cin>>a>>b; ll sum=0; int i,j; for(i=a,j=1;i<=b;j++,i++){ printf("%5d",i); sum+=i; if(j%5==0)printf(" "); } if((j-1)%5!=0)printf(" "); printf("Sum = %d",sum); // cout<<"Sum = "<<sum<<endl; return 0; }
L1-009 N个数求和
细节:这里就是模拟通分,然后注意每一步都要约分,不然爆long long,然后这里我犯一些细节错误,就是
判断语句应该这样写: if(a/b!=0&&a%b!=0)
而不是这样:
if(a>=b&&a%b!=0)
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define fi first #define se second typedef long long ll; typedef unsigned long long ull; ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);} int main(){ int n; scanf("%d",&n); ll g,a,b,c,d; scanf("%lld/%lld",&a,&b); g=gcd(a,b); a/=g; b/=g; for(int i=1;i<n;i++){ scanf("%lld/%lld",&c,&d); ll tmp=gcd(b,d); a=a*d/tmp+c*b/tmp; b=b/gcd(b,d)*d; g=gcd(a,b); a/=g; b/=g; } if(a>0&&b<0||a<0&&b<0)a=-a,b=-b; if(a>=b&&a%b!=0)printf("%lld %lld/%lld ",a/b,a%b,b); else if(a/b!=0&&a%b==0)printf("%lld ",a/b); else if(a/b==0&&a!=0)printf("%lld/%lld ",a,b); else puts("0"); return 0; }
就是考虑负数的情况这样就不对了;
L1-020 帅到没朋友
注意是五位ID就行了,然后搞个set判重,vector保存结果;
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define pf push_front #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; typedef double db; const db PI=acos(-1.0); const ll INF=0x3f3f3f3f3f3f3f3fLL; const int inf=0x3f3f3f3f;//0x7fffffff; const double eps=1e-9; const ll MOD=1e9+9; const int maxn=1e6+5; bool flag[maxn]; int main(){ int n,k,m,x; scanf("%d",&n); memset(flag,0,sizeof flag); while(n--){ scanf("%d",&k); rep(i,1,k){ scanf("%d",&x); if(k>1)flag[x]=1; } } set<int>st; scanf("%d",&m); vector<int>v; while(m--){ scanf("%d",&x); if(!flag[x]&&st.find(x)==st.end())v.pb(x); if(!flag[x])st.insert(x); } if(st.empty())cout<<"No one is handsome"; else { for(int i=0;i<v.size();i++) printf("%.5d%c",v[i],i==v.size()-1?' ':' '); } return 0; }
L1-025 正整数A+B
就是写个函数操作会很方便,要学会结构化程序设计;
然后注意读入的时候第二个要用getline读入
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define pf push_front #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; typedef double db; const db PI=acos(-1.0); const ll INF=0x3f3f3f3f3f3f3f3fLL; const int inf=0x3f3f3f3f;//0x7fffffff; const double eps=1e-9; const ll MOD=1e9+9; const int maxn=1e6+5; int f(string s){ int num=0; int len=s.size(); for(int i=0;i<len;i++)if(s[i]>'9'||s[i]<'0')return -1; for(int i=0;i<len;i++)num=num*10+s[i]-'0'; return num; } int main(){ string sa,sb; cin>>sa; getchar(); getline(cin,sb); int a=f(sa); int b=f(sb); if(a<=1000&&a>=1&&(b<1||b>1000)) cout<<a<<" + ? = ?"; else if(b<=1000&&b>=1&&(a<1||a>1000)) cout<<"? + "<<b<<" = ?"; else if(a<=1000&&a>=1&&b<=1000&&b>=1) cout<<a<<" + "<<b<<" = "<<a+b<<endl; else cout<<"? + ? = ?"<<endl; return 0; }
L1-027 出租
注意 自定义的set这样写:set<int,greater<int> >st;
搞个vector就行了;
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define pf push_front #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; typedef double db; const db PI=acos(-1.0); const ll INF=0x3f3f3f3f3f3f3f3fLL; const int inf=0x3f3f3f3f;//0x7fffffff; const double eps=1e-9; const ll MOD=1e9+9; const int maxn=1e6+5; int main(){ string s; cin>>s; set<int,greater<int> >st; int len=s.size(); for(int i=0;i<len;i++){ st.insert(s[i]-'0'); } vector<int>arr; set<int,greater<int> >::iterator it; for(it=st.begin();it!=st.end();it++){ int x=*it; arr.pb(x); } vector<int>index; for(int i=0;i<11;i++){ for(int j=0;j<arr.size();j++){ if(arr[j]==s[i]-'0')index.pb(j); } } cout<<"int[] arr = new int[]{"; for(int i=0;i<arr.size();i++){ if(i)cout<<","; cout<<arr[i]; } cout<<"};"<<endl; cout<<"int[] index = new int[]{"; for(int i=0;i<index.size();i++){ if(i)cout<<","; cout<<index[i]; } cout<<"};"<<endl; return 0; }
L1-030 一帮一
就是vector 你不能这样读进来
错误写法:
rep(i,1,n)scanf("%d",&v[i]);
正确写法:
rep(i,1,n)scanf("%d",&x),v.pb(x);
不会用pair,只会结构,这题可能用pair更好做吧;
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define pf push_front #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; typedef double db; const db PI=acos(-1.0); const ll INF=0x3f3f3f3f3f3f3f3fLL; const int inf=0x3f3f3f3f;//0x7fffffff; const double eps=1e-9; const ll MOD=1e9+9; const int maxn=1e5+5; struct node{int sex,vis;string name;}; struct ANS{string fi,se;}; int main(){ int n; cin>>n; vector<node>v; rep(i,1,n){ node tmp; cin>>tmp.sex>>tmp.name; tmp.vis=0; v.push_back(tmp); } vector<ANS>ans; int len=v.size(); for(int i=0;i<len;i++){ for(int j=len-1;j>=0;j--){ if(v[i].sex!=v[j].sex&&v[j].vis==0&&v[i].vis==0){ ANS tmp; tmp.fi=v[i].name; tmp.se=v[j].name; ans.pb(tmp); v[i].vis=1,v[j].vis=1; } } } len=ans.size(); for(int i=0;i<len;i++) cout<<ans[i].fi<<" "<<ans[i].se<<endl; system("pause"); return 0; }
L1-039 古风排版
这题挺有意思的;也不是很难;
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define pf push_front #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; typedef double db; const db PI=acos(-1.0); const ll INF=0x3f3f3f3f3f3f3f3fLL; const int inf=0x3f3f3f3f;//0x7fffffff; const double eps=1e-9; const ll MOD=1e9+9; const int maxn=1e5+5; int main(){ int n,num; cin>>n; string s; getchar(); getline(cin,s); vector<char>v[200]; int len=s.size(); for(int i=len-1;i>=0;i--){ if((i+1)%n==0)num=n; else num=(i+1)%n; v[num].pb(s[i]); } // cout<<"test :"<<endl; if(len%n==0)num=n; else num=len%n; for(int i=1;i<=n;i++){ if(i>num)cout<<" "; for(int j=0;j<v[i].size();j++){ cout<<v[i][j]; } cout<<endl; } return 0; }
L1-044 稳赢
注意取余这么写:cnt%(k+1)==0
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define pf push_front #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; typedef double db; const db PI=acos(-1.0); const ll INF=0x3f3f3f3f3f3f3f3fLL; const int inf=0x3f3f3f3f;//0x7fffffff; const double eps=1e-9; const ll MOD=1e9+9; const int maxn=1e5+5; int main(){ int k; scanf("%d",&k); string s; int cnt=0,flag; while(cin>>s){ if(s[0]=='E')break; cnt++; // cout<<"test "<<s<<" "<<cnt<<": "<<endl; if(cnt%(k+1)==0)flag=1; else flag=0; if(flag)cout<<s<<endl; else { if(s[0]=='C')cout<<"Bu"<<endl; else if(s[0]=='B')cout<<"JianDao"<<endl; else if(s[0]=='J')cout<<"ChuiZi"<<endl; } } // system("pause"); return 0; }
L1-046 整除光棍
这题本来想暴力,但必然是妥妥的T了
然后考虑模拟手算的除法;
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define pf push_front #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; typedef double db; const db PI=acos(-1.0); const ll INF=0x3f3f3f3f3f3f3f3fLL; const int inf=0x3f3f3f3f;//0x7fffffff; const double eps=1e-9; const ll MOD=1e9+9; const int maxn=1e5+5; int main(){ ll ans,x,s=1,n=1; cin>>x; while(s<x)s=s*10+1,n++; while(1){ cout<<s/x; s%=x; if(s==0){ ans=s; break; } s=s*10+1; n++; } cout<<" "<<n<<endl; // system("pause"); return 0; }
L1-054 福到了
这题推了好久,其实就一个东西,旋转180度的话,要交换的满足 x1+x2=n+1,y1+y2=n+1;
因为他们绕着中心旋转180;
也不用转换,直接判断输出就是了;
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define pf push_front #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; typedef double db; const db PI=acos(-1.0); const ll INF=0x3f3f3f3f3f3f3f3fLL; const int inf=0x3f3f3f3f;//0x7fffffff; const double eps=1e-9; const ll MOD=1e9+9; const int maxn=1e5+5; char mp[200][200]; int main(){ char c; int n; scanf("%c %d",&c,&n); rep(i,1,n){ getchar(); rep(j,1,n){ scanf("%c",&mp[i][j]); } } // cout<<"test :"<<endl; // rep(i,1,n){ // rep(j,1,n){ // printf("%c",mp[i][j]); // } // cout<<endl; // } int cen=n+1; bool issame=1; rep(i,1,n){ rep(j,1,n){ int x=cen-i; int y=cen-j; if(mp[x][y]!=mp[i][j])issame=0; } } if(issame){ cout<<"bu yong dao le"<<endl; rep(i,1,n){ rep(j,1,n){ if(mp[i][j]==' ')cout<<" "; else cout<<c; } cout<<endl; } return 0; } rep(i,1,n){ rep(j,1,n){ int x=cen-i; int y=cen-j; if(mp[x][y]!=' ')printf("%c",c); else printf(" "); } cout<<endl; } // shit: system("pause"); return 0; }
L1-043 阅览室 :
其实就是遍历,注意一下细节就行了;
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define pf push_front #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; typedef double db; const db PI=acos(-1.0); const ll INF=0x3f3f3f3f3f3f3f3fLL; const int inf=0x3f3f3f3f;//0x7fffffff; const double eps=1e-9; const ll MOD=1e9+9; const int maxn=1e3+5; struct book{bool flag;int time;}; int get(int h,int m){return h*60+m;} book a[maxn]; int main(){ rep(i,1,maxn)a[i].flag=0; int num,h,m,n; char op; scanf("%d",&n); while(n--){ int cnt=0,time=0; while(1){ scanf("%d %c %d:%d",&num,&op,&h,&m); if(num==0)break; if(op=='S'){ a[num].flag=1,a[num].time=get(h,m); } else if(a[num].flag==1){ time+=get(h,m)-a[num].time; a[num].flag=0; cnt++; } } if(cnt==0&&time==0)cout<<"0 0"<<endl; else printf("%d %.0lf ",cnt,time*1.0/cnt); } // system("pause"); return 0; }
L1-049 天梯赛座位分配
挺有意思的,注意格式输出,然后就是用一个nun记录已经安排满的队伍,如果没满就一直放,再记个pos表示位置,就行了;
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define pf push_front #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; typedef double db; const db PI=acos(-1.0); const ll INF=0x3f3f3f3f3f3f3f3fLL; const int inf=0x3f3f3f3f;//0x7fffffff; const double eps=1e-9; const ll MOD=1e9+9; const int maxn=1e3+5; int team[maxn]; vector<int>ans[maxn]; int main(){ int n,sum=0; scanf("%d",&n); rep(i,1,n){ scanf("%d",&team[i]); team[i]*=10; } int pos=1,num=0; while(num<n){ for(int i=1;i<=n;i++){ if(team[i]!=0){ team[i]--,ans[i].pb(pos); if(num==n-1)pos+=2; else pos++; if(team[i]==0)num++; } } } rep(i,1,n){ printf("#%d ",i); for(int j=0;j<ans[i].size();j++){ printf("%d%c",ans[i][j],(j+1)%10==0?' ':' '); } } // system("pause"); return 0; }
L1-059 敲笨钟
做法:暴力枚举一下,然后注意读掉回车;
#include<bits/stdc++.h> using namespace std; #define per(i,j,k) for(int i=k;i>=j;i--) int main(){ int n; cin>>n; string s; getchar(); while(n--){ getline(cin,s); int flag1=0,flag2=0; int len=s.size(); for(int i=0;i<len;i++){ if(s[i]==','){ if(s[i-1]=='g'&&s[i-2]=='n'&&s[i-3]=='o')flag1=1; } if(s[i]=='.'){ if(s[i-1]=='g'&&s[i-2]=='n'&&s[i-3]=='o')flag2=1; } } if(flag1&&flag2){ int cnt=0,seat; for(int i=len-1;i>=0;i--){ if(s[i]==' ')cnt++; if(cnt>=3){ seat=i; break; } } for(int i=0;i<=seat;i++)printf("%c",s[i]); cout<<"qiao ben zhong."<<endl; } else cout<<"Skipped"<<endl; } return 0; }
L1-064 估值一亿的AI核心代码
这题挺有意思的;
解法:如果给你一个字符串,单纯让你替换单词,频繁用string的搜索是很麻烦的,参考了大神的做法;
就是搞个vector<string>v ; 这样你每次截取单词,空格,和标点符号就很方便,然后按题目意思模拟即可;
PS:有个函数 isalnum 就是判断是否为数字或者字母;
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++) #define per(i,j,k) for(int i=(int)k;i>=(int)j;i--) #define pb push_back #define pf push_front #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef long double ldb; typedef double db; const db PI=acos(-1.0); const ll INF=0x3f3f3f3f3f3f3f3fLL; const int inf=0x3f3f3f3f;//0x7fffffff; const double eps=1e-9; const ll MOD=1e9+9; const int maxn=1e3+5; int main(){ int n; cin>>n; getchar(); while(n--){ string s; vector<string>v; getline(cin,s); cout<<s<<endl<<"AI: "; int len=s.size(); for(int i=0;i<len;i++){ if(s[i]=='?')s[i]='!'; else if(isupper(s[i])&&s[i]!='I')s[i]=tolower(s[i]); } for(int i=0;i<len; ){ string tmp=""; if(isalpha(s[i])){ while(isalpha(s[i])&&i<len)tmp+=s[i++]; } else if(isdigit(s[i])){ while(isdigit(s[i])&&i<len)tmp+=s[i++]; } else if(s[i]==' '){ tmp=" "; while(s[i]==' ')i++; } else { tmp+=s[i++]; if(!v.empty()&&v.back()==" ")v.pop_back(); } if(tmp==" "&&(v.empty()||i==len))continue; v.pb(tmp); } for(int i=0;i<v.size();i++){ if(v[i]=="I"||v[i]=="me")v[i]="you"; else if(v[i]=="you"){ if(i<2||!(v[i-1].size()==1&&!isalnum(v[i-1][0])))continue; if(v[i-2]=="can")v[i-2]="I",v[i]="can"; if(v[i-2]=="could")v[i-2]="I",v[i]="could"; } } for(int i=0;i<v.size();i++)cout<<v[i]; cout<<endl; } system("pause"); return 0; }
未完待续;