18104 练习使用多case解题
#include <iostream> 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; cin>>n; LL a,b; while(n--) { cin>>a>>b; cout<<a*b/gcd(a,b)<<endl; } cout<<"group 1 done"<<endl; while(cin>>a>>b,a!=0&&b!=0) { cout<<a*b/gcd(a,b)<<endl; } cout<<"group 2 done"<<endl; while(scanf("%lld%lld",&a,&b)>0) { cout<<a*b/gcd(a,b)<<endl; } cout<<"group 3 done"<<endl; }
18276 走迷宫
#include <iostream> #include <algorithm> #include <cstring> #include <queue> using namespace std; typedef pair<int,int> PII; const int N = 110; string s[N]; int dist[N][N]; int sx,sy,ex,ey; int k; int n,m; int dx[]={-1,0,0,1} , dy[]={0,1,-1,0}; int st[N][10]; void bfs() { queue<PII> q; q.push({sx,sy}); dist[sx][sy]=0; while(!q.empty()) { PII t = q.front(); q.pop(); bool flag = false; int nx ,ny; for(int i = 0 ; i < k ; i ++) { if(st[i][0]==t.first&&st[i][1]==t.second&&s[nx][ny]=='0') { nx = st[i][2] , ny = st[i][3]; flag = true; break; } } if(flag) { dist[nx][ny] = dist[t.first][t.second]+1; q.push({nx,ny}); } else { for(int i = 0 ; i < 4 ; i ++) { nx = t.first+dx[i] , ny = t.second+dy[i]; if(nx>=0&&nx<n&&ny>=0&&ny<m&&s[nx][ny]=='0'&&dist[nx][ny]==0x3f3f3f3f) { dist[nx][ny] = dist[t.first][t.second]+1; q.push({nx,ny}); } } } } } int main() { int t; cin>>t; while(t--) { memset(dist,0x3f,sizeof dist); cin>>n>>m; for(int i = 0 ; i < n ; i++) cin>>s[i]; cin>>k; for(int i = 0 ; i < k ; i ++) { cin>>st[i][0]>>st[i][1]>>st[i][2]>>st[i][3]; } cin>>sx>>sy>>ex>>ey; bfs(); if(dist[ex][ey]==0x3f3f3f3f) cout<<"die"<<endl; else cout<<dist[ex][ey]<<endl; } }
18440 走迷宫2
#include <iostream> #include <algorithm> #include <cstring> #include <queue> using namespace std; typedef pair<int,int> PII; const int N = 110; string s[N]; int dist[N][N]; int sx,sy,ex,ey; int k; int n,m; int dx[]={-1,0,0,1} , dy[]={0,1,-1,0}; int st[N][10]; void bfs() { queue<PII> q; q.push({sx,sy}); dist[sx][sy]=0; while(!q.empty()) { PII t = q.front(); q.pop(); for(int i = 0 ; i < 4 ; i ++) { int nx = t.first+dx[i] , ny = t.second+dy[i]; if(nx<0) nx = n-1; if(nx>=n) nx = 0; if(ny<0) ny = m-1; if(ny>=m) ny = 0; if(s[nx][ny]=='0'&&dist[nx][ny]==0x3f3f3f3f) { dist[nx][ny] = dist[t.first][t.second]+1; q.push({nx,ny}); } } } } int main() { int t; cin>>t; while(t--) { memset(dist,0x3f,sizeof dist); cin>>n>>m; for(int i = 0 ; i < n ; i++) cin>>s[i]; cin>>sx>>sy>>ex>>ey; bfs(); if(dist[ex][ey]==0x3f3f3f3f) cout<<"die"<<endl; else cout<<dist[ex][ey]<<endl; } }
19116 丑数
#include <iostream> #include <algorithm> #include <cstring> #include <map> using namespace std; typedef long long LL; const int N = 100000010; map<int,int> S; int F(int n) { if(S[n]) return S[n]; int id2 = 1 , id3 = 1 , id5 = 1; int t2 , t3 , t5; S[1] = 1; for(int i = 2 ; i <= n ; i ++) { t2 = S[id2]*2; t3 = S[id3]*3; t5 = S[id5]*5; S[i] = min(t2,min(t3,t5)); if(S[i]==t2) id2++; if(S[i]==t3) id3++; if(S[i]==t5) id5++; } return S[n]; } int main() { int t; cin>>t; while(t--) { int n; cin>>n; cout<<F(n)<<endl; } }
18005 它不是丑数
Source Code #include <iostream> #include <algorithm> #include <cstring> #include <map> using namespace std; typedef long long LL; const int N = 100000010; map<int,int> S; int F(int n) { if(S[n]) return S[n]; int id2 = 1 , id3 = 1 , id5 = 1; int t2 , t3 , t5; S[1] = 1; for(int i = 2 ; i <= n ; i ++) { t2 = S[id2]*2; t3 = S[id3]*3; t5 = S[id5]*5; S[i] = min(t2,min(t3,t5)); if(S[i]==t2) id2++; if(S[i]==t3) id3++; if(S[i]==t5) id5++; } return S[n]; } int main() { int t; cin>>t; while(t--) { int n; cin>>n; int cnt = 0; int i = 0; while(cnt<n) { cnt+=F(i+1)-F(i)-1; i++; } i--; cnt -= F(i+1)-F(i)-1; cout<<F(i)+n-cnt<<endl;; } }
18105 银行的叫号顺序
#include <iostream> #include <algorithm> #include <cstring> #include <queue> using namespace std; typedef pair<int,int> PII; const int N = 100010; priority_queue<PII> q; struct node { int t,v,id; string name; }a[N]; int n; int main() { cin>>n; for(int i = 1 ; i <= n ; i ++) { cin>>a[i].t>>a[i].v>>a[i].name; a[i].id = i; } int t = 0; for(int i = 1 ; i <= n ; i ++) { if(a[i].t<=t) q.push({a[i].v,-a[i].id}); else { if(!q.empty()) { cout<<a[-q.top().second].name<<endl; q.pop(); } t+=5; i--; } } while(!q.empty()) { cout<<a[-q.top().second].name<<endl; q.pop(); } }
18118 勇者斗恶龙
#include <iostream> #include <algorithm> using namespace std; const int N = 200010; int a[N],b[N]; int n,m; void solve() { for(int i = 1 ; i <= n ; i ++) cin>>a[i]; for(int i = 1 ; i <= m ; i ++) cin>>b[i]; sort(a+1,a+1+n); sort(b+1,b+1+m); int ans = 0; int i = 1; for(int j = 1; j <= m ; j ++) { if(b[j]>=a[i]) { i++; ans+=b[j]; } if(i==n+1) break; } if(i<=n) cout<<"Loowater is doomed!"<<endl; else { cout<<ans<<endl; } } int main() { while(scanf("%d%d",&n,&m),n!=0&&m!=0) { solve(); } }
18107 校赛排名
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 500010; struct node { int v,t,id; string name; bool operator < (const node & W) const { if(v!=W.v) return v>W.v; else if(t!=W.t) return t<W.t; else return id<W.id; } }a[N]; int main() { int n; cin>>n; for(int i = 1 ; i <= n ; i ++) { scanf("%d%d",&a[i].v,&a[i].t); cin>>a[i].name; a[i].id = i; } sort(a+1,a+1+n); for(int i = 1 ; i <= n ; i ++) { cout<<a[i].name<<endl; } return 0; }
18290 校赛排名2
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace std; const int N = 1000010; typedef long long LL; struct node { char name[200]; int pass; int time; int id; int right[30]; int wrong[30]; }a[N]; bool cmp(node a,node b) { if(a.pass!=b.pass) return a.pass>b.pass; else if(a.time!=b.time) return a.time<b.time; else return a.id<b.id; } int main() { int nowtime; char teamname[200]; char qid; int res; int num = 0; int i; while((scanf("%d%*c%s%*c%c%*c%d",&nowtime,teamname,&qid,&res))>0) { int ok = 0; for(i = 0 ; i < num ; i ++) { if(strcmp(a[i].name,teamname)==0) { ok = 1; break; } } if(!ok) { num++; strcpy(a[i].name,teamname); a[i].id = i; memset(a[i].right,0,sizeof a[i].right); memset(a[i].wrong,0,sizeof a[i].wrong); } if(res==0&&a[i].right[qid-65]==0) { a[i].right[qid-65] = 1; a[i].pass++; a[i].time = a[i].time+a[i].wrong[qid-65]*20+nowtime; } else if(res!=0) { a[i].wrong[qid-65]++; } } sort(a,a+num,cmp); for(int i = 0 ; i < num ; i ++) { if(a[i].pass!=0) { cout<<a[i].name<<' '<<a[i].pass<<' '<<a[i].time<<endl; } } }
1142 巡逻的士兵
#include <iostream> #include <algorithm> #include <cstring> #include <map> using namespace std; typedef long long LL; map<LL,LL> S; LL F(LL n) { if(n<3) return 0; else if(n==3) return 1; else { if(S[n]) return S[n]; S[n] = F(n/2) + F((n+1)/2); return S[n]; } } int main() { LL n; while(cin>>n,n) { cout<<F(n)<<endl; } }
18441 偷懒的士兵
#include <iostream> #include <algorithm> #include <cstring> #include <map> using namespace std; typedef long long LL; map<LL,LL> S; LL F(LL n) { if(n<3) return 0; else if(n==3) return 1; else { if(S[n]) return S[n]; S[n] = F(n/2) + F((n+1)/2); return S[n]; } } int main() { LL n; while(cin>>n,n) { cout<<n-3*F(n)<<endl; } }
18442 偷懒的士兵2
#include <iostream> #include <algorithm> #include <cstring> #include <map> using namespace std; typedef long long LL; map<LL,LL> S; LL F(LL n,LL st,LL div) { if(n<3) return st; else if(n==3) return 0x3f3f3f3f; else return min(F(n/2,st+div,div*2),F((n+1)/2,st,div*2)); } int main() { LL n; while(cin>>n,n) { if(F(n,1,1)==0x3f3f3f3f) cout<<0<<endl; else cout<<F(n,1,1)<<endl; } }
18443 除法等式
#include <iostream> #include <algorithm> #include <cstring> #include <ctime> using namespace std; const int N = 1000010; typedef long long LL; int num1[10],num2[10]; bool st[10]; int n; int la,lb; bool check1(int x) { memset(st,false,sizeof st); while(x) { if(st[x%10]&&x%10!=0) return false; st[x%10] = true; num1[++la] = x%10; x/=10; } return true; } bool check2(int x) { while(x) { if(st[x%10]&&x%10!=0) return false; st[x%10] = true; num2[++lb] = x%10; x/=10; } return true; } int main() { while(cin>>n,n) { int k = 98765/n; for(int i = 1 ; i <= k ; i ++) { la = 0 , lb = 0; memset(num1,0,sizeof num1); memset(num2,0,sizeof num2); if(check1(i)&&check2(i*n)) { for(int i = 5 ; i >= 1 ; i --) { cout<<num2[i]; } cout<<"/"; for(int i = 5 ; i >= 1; i --) { cout<<num1[i]; } cout<<"="<<n<<endl; } } cout<<endl; } }
1079 三角形
#include <iostream> #include <algorithm> #include <cstring> using namespace std; int main() { int t; cin>>t; while(t--) { int x; cin>>x; for(int i = x*x - 1 ; i >= x + 1 ; i --) for(int j = x*x - i ; j >= 1 ; j --) { if(i*i==x*x+j*j) cout<<i<<","<<j<<endl; } for(int i = x - 1 ; i >= 1 ; i --) for(int j = i ; j >= 1 ; j --) if(x*x==i*i+j*j) cout<<i<<","<<j<<endl; cout<<endl; } }
8623 龙龙
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace std; const int N = 1000010; typedef long long LL; long long H(int n){ LL s = 0; int q = sqrt(n); for(int i = 1 ; i <= q ; i ++) s+=(n/i-n/(i+1))*i; for(int i = 1 ; i <= n/(q+1) ; i ++) s+=n/i; return s; } int main() { int t; cin>>t; while(t--) { int n; cin>>n; cout<<H(n)<<endl; } }
18444 分数拆分
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace std; const int N = 1000010; typedef long long LL; LL k,x,y,z,a,b; int main() { int n; while(cin>>k,k) { for(z = k+1 ; z <= 3*k ; z ++) { if(z*k%(z-k)==0) { a = z*k/(z-k); if(a>=z) { printf("1/%lld=1/%lld+1/%lld\n",k,a,z); //long long是%lld } } LL tmax = 2*z*k/(z-k); LL tmin = z*k/(z-k)+1; for(y = tmin ; y <= tmax ; y ++) { if(k*z*y%(z*y-k*z-k*y)==0) { b = k*z*y/(z*y-k*z-k*y); if(b>=y&&y>=z) { printf("1/%lld=1/%lld+1/%lld+1/%lld\n",k,b,y,z); } } } } printf("\n"); } }
A N皇后问题
#include <cstring> #include <iostream> #include <algorithm> using namespace std; int n; int ans; int a[110]; void search(int u) { if(u==n) { ans++; return; } for(int i = 0 ; i < n ; i ++) { int ok = 1; a[u] = i; for(int j = 0 ; j < u ;j ++) { if(a[u]==a[j]||u-a[u]==j-a[j]||u+a[u]==j+a[j]) { ok = 0; break; } } if(ok) search(u+1); } } int main() { int t; cin>>t; while(t--) { ans = 0; cin>>n; search(0); cout<<ans<<endl; } }
18444 分数拆分
#include<iostream> #include<cstdio> //scanf、printf #define int long long using namespace std; signed main() { /*int m; while(scanf("%d",&m)==1&&m) //多case,输入0结束 { long long x,y,z,a=0; for(z=m+1;z<=3*m;z++) //因为x大于等于y大于等于z,所以z临界条件是三个相等时,即z小于3*m { if((m*z)%(z-m)==0) //此时为拆成两个分数时的情况 { a=(m*z)/(z-m); //提前赋值给a,再判断,若将式子放到printf中输出,在判断是否比z大时多了一次运算 if(z<=a) printf("1/%d=1/%lld+1/%lld\n",m,a,z); } long long t=(m*z*2)/(z-m)+1,b=0; //可能存在大于21亿的数,用long long,此处的t是在已知z的情况下的y的临界值,即y与x相等的时候 for(y=(m*z)/(z-m)+1;y<=t;y++) { if((m*y*z)%(y*z-m*z-m*y)==0) { b=(m*y*z)/(y*z-m*z-m*y); //提前赋值,减少运算次数 if(y>=z&&b>0) printf("1/%d=1/%lld+1/%lld+1/%lld\n",m,b,y,z); } } } printf("\n"); } return 0;*/ int m; while(cin>>m,m) { for(int z = m + 1 ; z <= 3 * m ; z ++) { if(m*z%(z-m)==0) { int a = m*z/(z-m); if(z<=a) printf("1/%lld=1/%lld+1/%lld\n",m,a,z); } int t = 2*m*z/(z-m),b = 0; for(int y=(m*z)/(z-m)+1;y<=t;y++) { if((m*y*z)%(y*z-m*z-m*y)==0) { b=(m*y*z)/(y*z-m*z-m*y); //提前赋值,减少运算次数 if(y>=z&&b>0) printf("1/%lld=1/%lld+1/%lld+1/%lld\n",m,b,y,z); } } } printf("\n"); } }
19010 最小的特殊数字
#include <iostream> #include <algorithm> #include <map> #include <cmath> #define int long long using namespace std; typedef pair<int,int> PII; const int N = 11; int a[N]; map<PII,int> S; int n,k; signed main() { cin>>n>>k; for(int i = 1 ; i <= n ; i ++) cin>>a[i]; sort(a+1,a+1+n); do { int x = 0; for(int i = 1 ; i <= n ; i ++) { x = x*10+a[i]; } if((x>=pow(10,n-1)||x==0)&&x%k==0) { cout<<x<<endl; return 0; } }while(next_permutation(a+1,a+1+n)); return 0; }