第一周测验
1、 输出第二个整数
#include <iostream> #include <cstdio> using namespace std; int main() { int a,b,c; scanf("%d %d %d",&a,&b,&c); printf("%d",b); return 0; }
2、字符菱形
#include <iostream> #include <cstdio> using namespace std; #include <math.h> int main() { char ch; cin>>ch; cout<<" "<<ch<<endl; cout<<" "<<ch<<ch<<ch<<endl; cout<<ch<<ch<<ch<<ch<<ch<<endl; cout<<" "<<ch<<ch<<ch<<endl; cout<<" "<<ch<<endl; return 0; }
3、打印ASCII码
#include <iostream> #include <cstdio> using namespace std; int main() { char ch; scanf("%c",&ch); printf("%d",ch); return 0; }
4、打印字符
#include <iostream> #include <cstdio> using namespace std; int main() { int x; scanf("%d",&x); printf("%c",x); return 0; }
5、整型数据类型存储空间大小
#include <iostream> #include <cstdio> using namespace std; int main() { int x; short y; printf("%d %d",sizeof(x),sizeof(y)); return 0; }
6、浮点型数据类型存储空间大小
#include <iostream> #include <cstdio> using namespace std; int main() { float x; double y; printf("%d %d",sizeof(x),sizeof(y)); return 0; }
第二周单元测验
1、对齐输出
#include <iostream> #include <cstdio> using namespace std; int main() { int a,b,c; scanf("%d %d %d",&a,&b,&c); printf("%8d %8d %8d",a,b,c); return 0; }
2、输出保留12位小数的浮点数
#include <iostream> #include <cstdio> using namespace std; int main() { double x; scanf("%lf",&x); printf("%.12f",x); return 0; }
3、空格分隔输出
#include <iostream> #include <cstdio> using namespace std; int main() { char ch; int x; float y; double z; scanf("%c",&ch); scanf("%d",&x); scanf("%f",&y); scanf("%lf",&z); printf("%c %d %f %lf",ch,x,y,z); return 0; }
4、计算球的体积
#include <iostream> #include <cstdio> using namespace std; int main() { double r,v; scanf("%lf",&r); v = 4.0/3*3.14*r*r*r; printf("%.2lf",v); return 0; }
5、大象喝水
#include <iostream> #include <cstdio> using namespace std; int main() { int h,r; scanf("%d%d",&h,&r); double x = 20*1000/(3.14159*r*r*h)+1; printf("%d",(int)x); return 0; }
第三周测验
1、奇偶数判断
#include <iostream> #include <cstdio> using namespace std; int main() { int n; cin>>n; if(n%2)cout<<"odd"<<endl; else cout<<"even"<<endl; return 0; }
2、求一元二次方程的根
#include <iostream> #include <cmath> #include <cstdio> using namespace std; int main( ) { double a,b,c,x1,x2,x3,x4; scanf("%lf%lf%lf",&a,&b,&c); x1 = (-b + sqrt(b*b-4*a*c))/(2*a)*1.0; x2 = (-b - sqrt(b*b-4*a*c))/(2*a)*1.0; x3 = -b / (2*a)*1.0; x4 = sqrt(4*a*c-b*b) / (2*a)*1.0; if(!x3)x3=0; if (b*b==4*a*c) printf("x1=x2=%.5f",x1); if (b*b>4*a*c) printf("x1=%.5f;x2=%.5f",x1,x2); if (b*b<4*a*c) printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi",x3,x4,x3,x4); return 0; }
3、点和正方形的关系
#include <iostream> #include <cstdio> using namespace std; int main() { int x,y; cin>>x>>y; if(x>1||x<-1||y>1||y<-1)cout<<"no"<<endl; else cout<<"yes"<<endl; return 0; }
4、苹果和虫子2
#include <iostream> #include <cstdio> using namespace std; int main() { int n,x,y; cin>>n>>x>>y; int m = n-y/x-1; int k = n-y/x; if(m<0)m=0; if(k<0)k=0; if(y%x)cout<<m<<endl; else cout<<k<<endl; return 0; }
5、简单计算器
#include <iostream> #include <cstdio> using namespace std; int main() { int x,y; char ch; cin>>x>>y>>ch; switch(ch) { case '+': cout<<x+y<<endl; break; case '-': cout<<x-y<<endl; break; case '*': cout<<x*y<<endl; break; case '/': if(y==0)cout<<"Divided by zero!"<<endl; else cout<<x/y<<endl; break; default: cout<<"Invalid operator!"<<endl; } return 0; }
6、求整数的和与均值
#include <iostream> #include <cstdio> using namespace std; int main() { int n,x; double sum=0; scanf("%d",&n); for(int i=0;i<n;++i) { scanf("%d",&x); sum+=x; } printf("%.0f %.5f",sum,sum/n); return 0; }
7、整数序列的元素最大跨度值
#include <iostream> #include <cstdio> using namespace std; int main() { int n,x,min=1000,max=0; cin>>n; for(int i=0;i<n;++i) { cin>>x; if(x>max)max=x; if(x<min)min=x; } cout<<max-min<<endl; return 0; }
8、奥运奖牌计数
#include <iostream> #include <cstdio> using namespace std; int main() { int n,g,s,c,sum=0; int sumg=0,sums=0,sumc=0; cin>>n; for(int i=0;i<n;++i) { cin>>g>>s>>c; sumg+=g; sums+=s; sumc+=c; } sum=sumg+sums+sumc; cout<<sumg<<" "<<sums<<" "<<sumc<<" "<<sum<<endl; return 0; }
9、乘方计算
#include <iostream> #include <cstdio> using namespace std; int main() { long long a,n,sum=1; cin>>a>>n; for(int i=0;i<n;++i) { sum =sum*a; } cout<<sum<<endl; return 0; }
10、鸡尾酒疗法
#include <iostream> #include <cstdio> using namespace std; int main() { int n,a,b,x,y; double tsub,osub; cin>>n>>a>>b; for(int i=0;i<n-1;++i) { cin>>x>>y; tsub = 1.0*b/a-1.0*y/x; osub = 1.0*y/x-1.0*b/a; if(tsub>0.05) cout<<"worse"<<endl; else if(osub>0.05) cout<<"better"<<endl; else cout<<"same"<<endl; } return 0; }
第四周测验
1、角谷猜想
#include <iostream> #include <cstdio> using namespace std; int main() { long long n; cin>>n; while(n!=1) { if(n%2) { cout<<n<<"*3+1="<<n*3+1<<endl; n = n*3+1; } else { cout<<n<<"/2="<<n/2<<endl; n = n/2; } } cout<<"End"<<endl; return 0; }
2、正常血压
#include <iostream> #include <cstdio> using namespace std; int main() { int n, x, y; int thisCnt=0,maxCnt=0; cin>>n; do { cin>>x>>y; if((x>=90&&x<=140)&&(y>=60&&y<=90)) { thisCnt++; if(thisCnt>maxCnt) { maxCnt=thisCnt; } } else { thisCnt=0; } } while(--n); cout<<maxCnt<<endl; return 0; }
3、数字反转
#include <iostream> #include <cstdio> using namespace std; int main() { int t,ret=0; cin>>t; while(t) { ret=ret*10+t%10; t=t/10; } cout<<ret<<endl; return 0; }
4、求特殊自然数
#include <iostream> #include <cstdio> using namespace std; int main() { for(int a=1;a<7;++a) { for(int c=1;c<7;++c) { if(a+c*49==c+a*81) { cout<<a + c*49<<endl; cout<<c<<0<<a<<endl; cout<<a<<0<<c<<endl; break; } } } return 0; }
5、雇佣兵
#include <iostream> #include <cstdio> using namespace std; int main() { int m,n,x; cin>>m>>n>>x; if(n<=m) { int t=0; while(x--) { t+=n; if(t>=m) { t=0; n+=m/n; } } } cout<<n<<endl; return 0; }
6、数字统计
#include <iostream> using namespace std; int main() { int x,y; int cnt=0; cin>>x>>y; while(x<=y) { int temp = x; while(temp) { if(temp%10==2)cnt++; temp=temp/10; } x++; } cout<<cnt<<endl; return 0; }
第五周测验
1、与指定数字相同的数的个数
#include <iostream> using namespace std; int main() { int n,x; cin>>n; int a[n]; for(int i=0;i<n;++i) { cin>>x; a[i]=x; } int y,cnt=0; cin>>y; for(int i=0;i<n;++i) { if(a[i]==y)cnt++; } cout<<cnt<<endl; return 0; }
2、陶陶摘苹果
#include <iostream> using namespace std; int main() { int a[10],x; for(int i=0;i<10;++i) { cin>>x; a[i]=x; } int h,cnt=0; cin>>h; h+=30; for(int i=0;i<10;++i) { if(a[i]<=h)cnt++; } cout<<cnt<<endl; return 0; }
3、年龄与疾病
#include <iostream> #include <cstdio> using namespace std; int main() { int n,x; scanf("%d",&n); int a[n]; //c99 for(int i=0;i<n;++i) { scanf("%d",&x); a[i]=x; } int old18=0,old35=0,old60=0,old61=0; for(int i=0;i<n;++i) { if(a[i]<=18)old18++; else if(a[i]<=35)old35++; else if(a[i]<=60)old60++; else old61++; } double m = n; printf("%.2f% %.2f% %.2f% %.2f% ", old18/m*100,old35/m*100,old60/m*100,old61/m*100); return 0; }
4、校门外的树
#include <iostream> using namespace std; int main() { int L,M,a=0,s,e,tree[10001]={0}; cin>>L>>M; for(;M>0;--M) { cin>>s>>e; for(;s<=e && s<=10000;++s) tree[s]=1; } for(int i=0;i<=L;++i) a+=tree[i]; a=L+1-a; cout<<a; return 0; }
#include <iostream> using namespace std; int main() { int L,m; cin>>L>>m; int a[L+1]={0}; while(m--) { int s,e; cin>>s>>e; for(int i=s;i<=e;++i) { a[i]=1; } } int cnt=0; for(int i=0;i<L+1;++i) { if(a[i]==0)cnt++; } cout<<cnt<<endl; return 0; }
5、计算鞍点
#include <iostream> using namespace std; int main() { int a[5][5],iMax[5]={0},maxI[5]; for(int i=0;i<5;++i) { for(int j=0;j<5;++j) { cin>>a[i][j]; if(a[i][j]>iMax[i]) { iMax[i] = a[i][j]; maxI[i] = j; } } } bool b = false; for(int i=0;i<5;++i) { int iMin = iMax[i]; for(int j=0;j<5;++j) { if(a[j][maxI[i]]<iMin) { iMin=a[j][maxI[i]]; } } if(iMin ==iMax[i]) { b=true; cout<<i+1<<" "<<maxI[i]+1<<" "<<iMax[i]<<endl; } } if(!b) cout<<"not found"<<endl; return 0; }
6、图像模糊处理
#include <iostream> #include <cmath> using namespace std; int main() { int n,m; cin>>n>>m; int a[n][m], b[n][m]; for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { cin>>a[i][j]; b[i][j]=a[i][j]; } } for(int i=1;i<n-1;++i) { for(int j=1;j<m-1;++j) { double x = (a[i][j]+a[i][j-1]+a[i][j+1]+a[i-1][j]+a[i+1][j])/5.0; b[i][j] = round(x); } } for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { cout<<b[i][j]; if(j<m-1)cout<<" "; } cout<<endl; } return 0; }
7、矩阵转置
#include <iostream> using namespace std; int main() { int n,m; cin>>n>>m; int a[n][m]; for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { cin>>a[i][j]; } } for(int i=0;i<m;++i) { for(int j=0;j<n;++j) { cout<<a[j][i]; if(j<n-1)cout<<" "; } cout<<endl; } return 0; }
第六周测验
1、Pell数列
# include<stdio.h> int b[1000010]={0,1,2}; int main() { int n,i,k; scanf("%d",&n); for(i=3;i<1000000;++i) { b[i]=(2*b[i-1]+b[i-2])%32767; } for(i=0;i<n;++i) { scanf("%d",&k); printf("%d ",b[k]); } return 0; }
#include<iostream> #include<cstdio> using namespace std; long long Pell(int k) { long long a1=1,a2=2,a3; if(k==1)return a1; if(k==2)return a2; k-=2; while(k--) { a3=a1+2*a2; a1=a2; a2=a3; a1%=32767; a2%=32767; a3%=32767; } return a3; } int main() { int n; cin>>n; while(n--) { int k; cin>>k; cout<<Pell(k)<<endl; } }
2、求最大公约数问题
#include<iostream> #include<cstdio> using namespace std; int gcd(int a,int b) { while(a%b) { int temp; temp=a; a=b; b=temp%b; } return b; } int main() { int a,b; cin>>a>>b; cout<<gcd(a,b); return 0; }
3、第i位替换
#include <iostream> using namespace std; int bitManipulation1(int n, int m, int i) { return (n&(~(1<<i)) | ((m >> i)&1)<< i); } int main() { int n, m, i, t; cin >> t; while (t--) { cin >> n >> m >> i; cout << bitManipulation1(n, m, i) << endl; } return 0; }
4、第i位取反
#include <iostream> using namespace std; int bitManipulation2(int n, int i) { return n^(1<<i); } int main() { int t, n, i; cin >> t; while (t--) { cin >> n >> i; cout << bitManipulation2(n, i) << endl; } return 0; }
5、左边i位取反
#include <iostream> using namespace std; int bitManipulation3(int n, int i) { return n^(0xffffffff<<(32-i)); } int main() { int t, n, i; cin >> t; while (t--) { cin >> n >> i; cout << bitManipulation3(n, i) << endl; } return 0; }
第七周测验
1、统计数字字符个数
#include<iostream> using namespace std; int main(){ char str[255]=""; gets(str); int i=0,cnt=0; while(str[i]!='