输入50个学生的某门课程的成绩,打印出低于平均分的学生序号与成绩。
#include <iostream> using namespace std; int n; double a[60],tot,avg; int main(){ cin >> n; for(int i=1;i<=n;i++){ cin >> a[i]; tot = tot + a[i]; } avg = tot / n; for(int i=1;i<=n;i++) if(a[i]<avg) cout << i << "," << a[i] << endl; return 0; }
输入n个数,要求程序按输入时的逆序把这n个数打印出来,已知整数不超过100个。也就是说,按输入相反顺序打印这n个数。
#include <iostream> using namespace std; int a[100],n,x; int main(){ while(scanf("%d",&x)==1) a[n++] = x; for(int i=n-1;i>=1;i--) printf("%d ",a[i]); printf("%d",a[0]); return 0; }
将a数组中第一个元素移到数组末尾,其余数据依此往前平移一个位置。
#include <iostream> #include <iomanip> using namespace std; int a[100],n; int main(){ cin >> n; for(int i=0;i<n;i++) cin >> a[i]; int temp = a[0]; for(int i=0;i<n-1;i++) a[i] = a[i+1]; a[n-1]=temp; for(int i=0;i<n;i++) cout << setw(3) << a[i]; cout << endl; return 0; }
宾馆里有100个房间,从1-100编了号。第一个服务员把所有的房间门都打开了,第二个服务员把所有是2的倍数的房间做“相反处理”,第三个服务员把所有编号是3的倍数的房间做“相反处理”,...以后每个服务员都是如此。当第100个服务员来过之后,哪几扇门是打开的?(所谓“相反处理”是:原来开着的门关上,原来关上的门打开)。
#include <iostream> using namespace std; int a[110],n,first=1; int main(){ for(int i=1;i<=100;i++) for(int j=1;j<=100;j++) if(j%i==0) a[j]=!a[j]; for(int i=1;i<=100;i++) if(a[i]){ if(first) first=0; else cout << " "; cout << i; } cout << endl; return 0; }
约瑟夫问题:N个人围成一圈,从第一个开始报数,数到M的人出圈;再由下一个人开始报数,数到M的人出圈;...输出依次出圈的人的编号。N,M由键盘输入。
#include <iostream> using namespace std; bool a[101]; int n,m; int main(){ cin >> n >> m; for(int i=1;i<=n;i++) a[i] = false; int f=0,t=0,s=0; do{ t++; if(t==n+1) t=1; if(a[t]==false) s++; if(s==m){ s=0; cout << t << " "; a[t]=true; f++; } }while(f!=n); return 0; }
输入n个整数,存放在数组a[1]至a[n]中,输出最大数所在位置(n<=10000)。
输入样例:
5
67 43 90 78 32
输出样例:
3
#include <iostream> using namespace std; int a[10001],n,k,maxa; int main(){ cin >> n; for(int i=1;i<=n;i++) cin >> a[i]; maxa=a[1]; k=1; for(int i=2;i<=n;i++){ if(a[i]>maxa){ maxa=a[i]; k=i; } } cout << k << endl; return 0; }
编程输入十个正整数,然后自动按从大到小的顺序输出。(冒泡排序)
输入样例:
2 5 8 6 12 34 65 22 16 55
输出样例:
65 55 34 22 16 12 8 6 5 2
#include <iostream> using namespace std; const int n=10; int a[n+1]; int main(){ for(int i=1;i<=n;i++) cin >> a[i]; for(int i=1;i<n;i++){ for(int j=1;j<=n-i;j++){ if(a[j]<a[j+1]) swap(a[j],a[j+1]); } } cout << a[1]; for(int i=2;i<=n;i++) cout << " " << a[i]; cout << endl; return 0; }
用筛选法求出100以内的全部质数,并按每行五个数显示。
#include <iostream> #include <iomanip> #include <cmath> using namespace std; const int n=100; bool prime[n+1]; int main(){ for(int i=0;i<=n;i++) prime[i]=true; for(int i=2;i<=sqrt(n);i++){ if(prime[i]) for(int j=2;j<=n/i;j++) prime[i*j]=false; } for(int i=2,t=0;i<=n;i++){ if(prime[i]){ cout << setw(5) << i; t++; if(t%5==0) cout << endl; } } return 0; }
#include <iostream> using namespace std; int main() { int N,m,a[100],sum=0; cin >> N; for(int i=0;i<N;i++) { cin >> a[i]; } cin >> m; for(int i=0;i<N;i++) { if (a[i]==m){ sum = sum + 1; } } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main() { int a[100],h,sum=0; for(int i=0;i<10;i++) { cin >> a[i]; } cin >> h; for(int i=0;i<10;i++) { if (a[i]<=30+h){ sum = sum + 1; } } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main() { float cost=0, price[]={28.9,32.7,45.6,78.0,35.0,86.2,27.8,43.0,56.0,65.0}; int num[10]; for(int i=0;i<10;i++) { cin >> num[i]; } for(int i=0;i<10;i++) { cost = cost + price[i] * num[i]; } printf("%.1f",cost); return 0; }
#include <iostream> using namespace std; int main() { int num[100],n; cin >> n; for(int i=0;i<n;i++) { cin >> num[i]; } cout << num[n-1]; for(int i=n-2;i>=0;i--) { cout << " " << num[i]; } cout << endl; return 0; }
#include <iostream> using namespace std; int main() { int num[100],n,sum=0; float age[4] = {0.0,0.0,0.0,0.0}; cin >> n; for(int i=0;i<n;i++) { cin >> num[i]; sum = sum + 1; if(num[i]<=18){ age[0] = age[0] + 1; }else if(num[i]<=35){ age[1] = age[1] + 1; }else if(num[i]<=60){ age[2] = age[2] + 1; }else{ age[3] = age[3] + 1; } } for(int i=0;i<4;i++) { printf("%.2f%%\n",age[i]*100.00f/sum); } return 0; }
#include <iostream> using namespace std; int main() { int begin[100],end[100],tree[10000],L,M,sum=0; cin >> L >> M; for(int i=0;i<=L;i++){ tree[i] = 1; } for(int i=0;i<M;i++) { cin >> begin[i] >> end[i]; for(int j=begin[i];j<=end[i];j++){ tree[j]=0; } } for(int i=0;i<=L;i++) { if(tree[i]==1){ sum = sum + 1; }; } cout << sum << endl; return 0; }
#include<iostream> #include<cmath> using namespace std; int x[10000],y[10000]; int L,t,g,n,ok=1; int main() { cin>>n; for(int i=1;i<=n;i++){ cin>>x[i]; } for(int i=2;i<=n;i++){ t=abs(x[i]-x[i-1]); y[t]++; } for(int i=1;i<n;i++){ if(y[i]!=1){ ok=0; } } if(ok) cout<<"Jolly"; else cout<<"Not jolly"; return 0; }
#include <iostream> using namespace std; int main(){ int a[10000],b[10000]; int n,na,nb; cin >> n >> na >> nb; for(int i=0;i<na;i++) cin >> a[i]; for(int i=0;i<nb;i++) cin >> b[i]; int p = na; while(p<n){ a[p] = a[p%na]; p++; } p = nb; while(p<n){ b[p] = b[p%nb]; p++; } int A=0,B=0; for(int i=0;i<n;i++){ if(a[i]==0 && b[i]==2){ A++; } if(a[i]==2 && b[i]==5){ A++; } if(a[i]==5 && b[i]==0){ A++; } if(a[i]==2 && b[i]==0){ B++; } if(a[i]==5 && b[i]==2){ B++; } if(a[i]==0 && b[i]==5){ B++; } } if(A>B) cout << "A" << endl; else if(B>A) cout << "B" << endl; else cout << "draw" << endl; return 0; }
#include <iostream> using namespace std; int main() { int n,a[1000],b,sum=0; cin >> n; for(int i=0;i<n;i++) { cin >> a[i]; } for(int i=0;i<n;i++) { cin >> b; sum = sum + a[i] * b; } cout << sum << endl; return 0; }
#include <iostream> #include <string.h> using namespace std; int main(){ char a[300],b[300]; cin >> a >> b ; //cout << a << b << endl; int alen = strlen(a); int blen = strlen(b); //cout << alen << blen << endl; int max = alen; if (blen>max) max = blen; //cout << max << endl; int c[max+1],d[max+1]; int j,k; for (int i=1;i<=max;i++){ if(i<=alen) c[max-i] = a[alen-i] - 48; else c[max-i] = 0; //cout << "a" << c[max-i] << endl; if(i<=blen) d[max-i] = b[blen-i] - 48; else d[max-i] = 0; //cout << "b" << d[max-i] << endl; } //printf("%d\n",max); int more = 0, p=0; //cout << more << endl; for (int i=max-1 ;i>=0;i--){ //cout << more << " "; p = c[i] + d[i] + more; //cout << c[i] << " " << d[i] << " " << p << " " << more << endl; if(p>=10){ more = 1; c[i] = p - 10; }else{ more = 0; c[i] = p ; } } int i = 0; if (more>0) cout << more; else for (i=0;i<max;i++) { //cout << c[i] << endl; if (c[i]>0){ break; } } if(i==max) cout << 0 << endl; for (int j=i;j<max;j++) cout << c[j]; cout << endl; return 0; }
#include <iostream> #include <cstring> #include <algorithm> using namespace std; int a[201],b[201]; char c[201]; int main(){ gets(c); int lena = strlen(c); for(int i=0;i<lena;i++) a[i] = c[i] - '0'; reverse(a,a+lena); gets(c); int lenb = strlen(c); for(int i=0;i<lenb;i++) b[i] = c[i] - '0'; reverse(b,b+lenb); for(int i=0;i<lenb;i++){ if(a[i]<b[i]){ a[i] = a[i] + 10 - b[i]; a[i+1] = a[i+1] -1; }else{ a[i] = a[i] - b[i]; } } reverse(a,a+lena); int i=0; while(a[i]==0) i++; for(;i<lena;i++) cout << a[i]; cout << endl; return 0; }
#include <iostream> using namespace std; int ans[100]; int main(){ int n,i,m=1; cin >> n; ans[0]=1; while(n--){ int more =0; i=0; for(;i<m;i++){ int temp = ans[i] * 2 + more; ans[i]=temp % 10; more = temp / 10; } if(more){ ans[i]=more; m++; } } while(!ans[i]) i--; for(int t=i;t>=0;t--){ cout << ans[t]; } cout << endl; return 0; }
#include <iostream> #include <cstring> using namespace std; int n[100]; char c[100]; int main(){ int flag = 1; gets(c); int len = strlen(c); for(int i=0;i<len;i++) n[i] = c[i]-'0'; for(int k=2;k<=9;k++){ int r = 0; for(int i=0;i<len;i++){ r = (n[i] +r*10) % k; } if(r==0){ cout << k << " "; flag = 0; } } if(flag) cout << "none" << endl; else cout << endl; return 0; }
#include <iostream> using namespace std; int ans[100000]; int main(){ int n,len=1; cin >> n; ans[0]=1; for(int i=2;i<=n;i++){ int more=0,j=0; for(;j<len;j++){ int tmp = ans[j]*i; int r = (tmp + more) / 10; ans[j] = (tmp + more) % 10; more = r; } while(more>0){ ans[len++] = more % 10; more = more / 10; } } for(int i=len-1;i>=0;i--) cout << ans[i]; cout << endl; return 0; }
#include <iostream> using namespace std; int ans[100]; int p[100]; int main(){ int n,len=1; cin >> n; p[0]=1,ans[0]=1; for(int i=2;i<=n;i++){ int more=0; for(int j=0;j<len;j++){ int tmp = p[j]*i; int r = (tmp + more) / 10; p[j] = (tmp + more) % 10; more = r; } while(more>0){ p[len++] = more % 10; more = more / 10; } more = 0; for(int j=0;j<=len;j++){ int tmp = ans[j]+p[j]+more; ans[j] = tmp % 10; more = tmp / 10; } } for(int i=len-1;i>=0;i--) cout << ans[i]; cout << endl; return 0; }
#include <iostream> using namespace std; int main(){ int a,b,flag=1; for(int i=1;i<=7;i++){ cin >> a >> b; if(a+b>8){ flag = 0; cout << i << endl; break; } } if(flag){ cout << 0 << endl; } return 0; }
#include <iostream> using namespace std; int main(){ int n,a,minn=100000,maxx=-100000; cin >> n; for(int i=1;i<=n;i++){ cin >> a; maxx = max(maxx,a); minn = min(minn,a); } cout << maxx - minn <<endl; return 0; }
#include <iostream> using namespace std; int main(){ int n,a[101],sum=0,maxx=-10000000; cin >> n; for(int i=0;i<n;i++){ cin >> a[i]; maxx = max(maxx,a[i]); } for(int i=0;i<n;i++){ if(a[i]!=maxx) sum = sum + a[i]; } cout << sum << endl; return 0; }
#include <iostream> #include <cmath> using namespace std; int main(){ int n,mx,mn; double a[305],avg,sub,sum=0.0,maxx=0.0,minn=10000000000000.0; cin >> n; for(int i=0;i<n;i++){ cin >> a[i]; if(a[i]>maxx){ maxx = a[i]; mx = i; } if(a[i]<minn){ minn = a[i]; mn = i; } sum = sum + a[i]; } avg = (sum-maxx-minn)/(n-2); maxx = 0.0; for(int i=0;i<n;i++){ if(i!=mn && i!=mx){ maxx = max(maxx,abs(avg-a[i])); } } printf("%.2f %.2f",avg,maxx); return 0; }
#include <iostream> using namespace std; int n,a[10005],d[10005],fmax=0; int main(){ cin >> n; for(int i=0;i<n;i++){ cin >> a[i]; fmax = max(fmax,a[i]); d[a[i]]++; } for(int i=0;i<=fmax;i++) cout << d[i] << endl; return 0; }
#include <iostream> using namespace std; int main(){ int n,a,tmp=-1,tim=0,max=1; cin >> n; for(int i=0;i<n;i++){ scanf("%d",&a); if(a!=tmp){ if(tim>max) max = tim; tim = 1; tmp = a; }else{ tim = tim + 1; } } if(tim>max) max = tim; cout << max << endl; return 0; }
#include <iostream> using namespace std; int f[200]; int main(){ int n,a,first=1; cin >> n; for(int i=0;i<n;i++){ scanf("%d",&a); if(!f[a]){ if(first){ cout << a; first = 0; }else{ cout << " " << a; } f[a]=1; } } cout << endl; return 0; }
#include <iostream> using namespace std; int n,dt[10005][4],x,y,idx=-1; int main(){ cin >> n; for(int i=0;i<n;i++){ scanf("%d %d %d %d",&dt[i][0],&dt[i][1],&dt[i][2],&dt[i][3]); } cin >> x >> y; for(int i=0;i<n;i++){ if(x-dt[i][0]>=0 && x-dt[i][0]<=dt[i][2] && y-dt[i][1]>=0 && y-dt[i][1]<=dt[i][3]){ idx = i+1; } } cout << idx << endl; return 0; }