第一周测验
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%\n%.2f%\n%.2f%\n%.2f%\n", 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\n",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]!='\0') { if(isdigit(str[i])) cnt++; i++; } cout<<cnt<<endl; return 0; }
2、找第一个只出现一次的字符
#include<iostream> using namespace std; int main() { char str[100000]=""; char cc=' '; int ch[26]={0}; gets(str); for(int i=0;str[i];++i) { ch[str[i]-'a']++; } for(int i=0;str[i];++i) { if(ch[str[i]-'a']==1) { cc=str[i]; break; } } if(cc==' ')cout<<"no"; else cout<<cc<<endl; return 0; }
3、石头剪子布
#include<iostream> using namespace std; int main() { int n; cin>>n; while(n--) { string str1,str2; cin>>str1>>str2; if(str1==str2)cout<<"Tie"<<endl; else if(str1=="Rock"&&str2=="Scissors"||str1=="Paper"&&str2=="Rock" ||str1=="Scissors"&&str2=="Paper") cout<<"Player1"<<endl; else cout <<"Player2"<<endl; } return 0; }
4、最长最短单词
#include <iostream> #include <cstring> using namespace std; int main() { char str[200*100]="",word[100]=""; char minWord[100]="",maxWord[100]=""; int min=100,max=0; gets(str); int i=0; while(str[i]!='\0') { while(!(str[i]>=65&&str[i]<=90||str[i]>=97&&str[i]<=122)) ++i; if(!str[i]) break; int j=0; while(str[i]>=65&&str[i]<=90||str[i]>=97&&str[i]<=122) { word[j]=str[i]; ++j,++i; } word[j]='\0'; int len = strlen(word); if(len>max) { max=len; strcpy(maxWord,word); } if(len<min) { min=len; strcpy(minWord,word); } } cout<<maxWord<<endl; cout<<minWord<<endl; return 0; }
5、密码翻译
# include<stdio.h> int main() { char ch[100]=""; gets(ch); int i=0; while(ch[i]!='\0') { if(ch[i]>='a'&&ch[i]<='z') { int c = ch[i]+1; if(c>122)c-=26; printf("%c",c); } else if(ch[i]>='A'&&ch[i]<='Z') { int c = ch[i]+1; if(c>90)c-=26; printf("%c",c); } else printf("%c",ch[i]); i++; } return 0; }
第九周测验
1、指针练习:输出Hello
#include <iostream> using namespace std; int main() { char s[] = "Hello"; char * p; for(p=s;*p;++p) cout << * p ; return 0; }
2、指针练习:输出Tesla
#include <iostream> using namespace std; void Print(const char * p1, const char * p2) { for(;p1<p2;++p1) cout << * p1; } int main() { const char * s = "Tesla123"; Print(s,s+5); cout << endl; Print(s,s+3); cout << endl; return 0; }
3、指针练习:ForEach
#include <iostream> using namespace std; void ForEach(void * a, int width, int num, void (*f)(void *) ) { for(int i = 0;i < num; ++i) f((char*)a+width*i); } void PrintSquare(void * p) { int * q = (int*)p; int n = *q; cout << n * n << ","; } void PrintChar(void * p) { char * q = (char*)p; cout << *q << ","; } int main() { int a[5] = {1,2,3,4,5}; char s[] = "hello!"; ForEach(a,sizeof(int),5,PrintSquare); cout << endl; ForEach(s,sizeof(char),6,PrintChar); return 0; }
4、指针练习:Memcpy之一
#include <iostream> using namespace std; void Memcpy(char * src,char * dest,int n) { char *pdest = (char *)dest; char *psrc =(char *)src; for(int i=0;i<n;++i) { *(pdest+i)=*(psrc+i); } } int Strlen(char * s) { int i; for( i = 0; s[i]; ++i); return i; } int main() { int a; char s1[30]; char s2[30]; int t; cin >> t; for(int i = 0;i < t; ++i) { cin >> a; int b = 99999999; Memcpy((char*)&a,(char *) &b,sizeof(int)); cout << b << endl; } for(int i = 0;i < t; ++i) { cin >> s1; Memcpy(s1,s2,Strlen(s1)+1); cout << s2 << endl; } return 0; }
5、指针练习:double
#include <iostream> using namespace std; void Double(int * p, int n) { for(int i = 0;i < n; ++i) p[i] *= 2; } int main() { int a[3][4] = { { 1,2,3,4},{5,6,7,8}, { 9,10,11,12 } }; Double(a[1],6); for(int i = 0;i < 3; ++i) { for(int j = 0; j < 4; ++j) cout << a[i][j] << ","; cout << endl; } return 0; }
6、指针练习:Memcpy之二
#include <iostream> using namespace std; void Memcpy( void * src, void * dest, int size) { char *pdest = (char*)dest; char *psrc = (char *)src; char p[size]; for(int i=0;i<size;++i) { p[i]=*(psrc+i); } for(int i=0;i<size;++i) { *(pdest+i)=p[i]; } } void Print(int * p,int size) { for(int i = 0;i < size; ++i) cout << p[i] << ","; cout << endl; } int main() { int a[10]; int n; cin >> n; for(int i = 0;i < n; ++i) cin >> a[i]; int b[10] = {0}; Memcpy(a,b,sizeof(a)); Print(b,n); int c[10] = {1,2,3,4,5,6,7,8,9,10}; Memcpy(c,c+5,5*sizeof(int)); //将c的前一半拷贝到后一半 Print(c,10); char s[10] = "123456789"; Memcpy(s+2,s+4,5); //将s[2]开始的5个字符拷贝到s[4]开始的地方 cout << s << endl; char s1[10] = "123456789"; Memcpy(s1+5,s1+1,4); //将s1[5]开始的4个字符拷贝到s1[1]开始的地方 cout << s1 << endl; return 0; }
7、指针练习:MyMax
#include <iostream> using namespace std; void *MyMax(void *a,unsigned int size,int n,int (*f)(void *,void *)) { void* max = (char*)a; for(int i=1; i<n; i++) { if(f(max, ((char*)a)+i*size)<0) max = ((char*)a)+i*size; } return max; } int Compare1(void * n1,void * n2) { int * p1 = (int * )n1; int * p2 = (int * )n2; return ((*p1)%10) - ((*p2)%10); } int Compare2(void * n1,void * n2) { int * p1 = (int * )n1; int * p2 = (int * )n2; return *p1 - *p2; } #define eps 1e-6 int Compare3(void * n1,void * n2) { float * p1 = (float * )n1; float * p2 = (float * )n2; if( * p1 - * p2 > eps) return 1; else if(* p2 - * p1 > eps) return -1; else return 0; } int main() { int t; int a[10]; float d[10]; cin >> t; while(t--) { int n; cin >> n; for(int i = 0;i < n; ++i) cin >> a[i]; for(int i = 0;i < n; ++i) cin >> d[i]; int * p = (int *) MyMax(a,sizeof(int),n,Compare1); cout << * p << endl; p = (int *) MyMax(a,sizeof(int),n,Compare2); cout << * p << endl; float * pd = (float * )MyMax(d,sizeof(float),n,Compare3); cout << * pd << endl; } return 0; }
8、指针练习:指向指针的指针
#include <iostream> using namespace std; int main() { int x,y,z; x = 10; y = 20; z = 30; int * a[3] = { &x, &y,&z}; for(int **p=a;p < a + 3; ++p) cout<< * (*p) << endl; return 0; }
9、指针练习:SwapMemory
#include <iostream> using namespace std; void SwapMemory(void * m1,void * m2, int size) { char *x = (char*)m1; char *y = (char*)m2; for(int i=0;i<size;++i) { char temp; temp=*(x+i); *(x+i)=*(y+i); *(y+i)=temp; } } void PrintIntArray(int * a,int n) { for(int i = 0;i < n; ++i) cout << a[i] << ","; cout << endl; } int main() { int a[5] = {1,2,3,4,5}; int b[5] = {10,20,30,40,50}; SwapMemory(a,b,5 * sizeof(int)); PrintIntArray(a,5); PrintIntArray(b,5); char s1[] = "12345"; char s2[] = "abcde"; SwapMemory(s1,s2,5); cout << s1 << endl; cout << s2 << endl; return 0; }
第十周测验
1、成绩排序
#include<iostream> #include<cstring> using namespace std; struct StudSore{ int psore; //char sname[20]; string sname; }; int main() { //StudSore ssore[25], tmp; int n, i, j; cin>>n; StudSore ssore[n], tmp; for(i=0; i<n; i++) cin>>ssore[i].sname>>ssore[i].psore; for(i=0; i<n-1; i++) { int max=i; for(j=i+1; j<n; j++){ if((ssore[max].psore<ssore[j].psore)|| (ssore[max].psore==ssore[j].psore&&ssore[max].sname>ssore[j].sname)) max=j; } tmp=ssore[i]; ssore[i]=ssore[max]; ssore[max]=tmp; } for(i=0; i<n; i++) cout<<ssore[i].sname<<" "<<ssore[i].psore<<endl; }
2、分数线划定
#include <iostream> #include <cstring> using namespace std; struct student { unsigned str; int achivement; }; void BubbleSort(student stu[] ,int size) { for(int i = size-1;i > 0; --i ) { for(int j = 0; j < i; ++j) { if( stu[j].achivement < stu[j+1].achivement|| ( stu[j].achivement == stu[j+1].achivement&& stu[j].str> stu[j+1].str ) ) { student temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp; } } } } int main() { int n,m; cin>>n>>m; struct student stu[n]; for(int i=0;i<n;++i) { cin>>stu[i].str>>stu[i].achivement; } BubbleSort(stu,n); m *=1.5; while(stu[m-1].achivement==stu[m].achivement)m++; cout<<stu[m-1].achivement<<" "<<m<<endl; for(int i=0;i<m;++i) { cout<<stu[i].str<<" "<<stu[i].achivement<<endl; } return 0; }
3、病人排队
#include <iostream> #include <cstring> using namespace std; struct student { string str; int achivement; }; void BubbleSort(student stu[] ,int size) { for(int i = size-1;i > 0; --i ) { for(int j = 0; j < i; ++j) { if(stu[j].achivement < stu[j+1].achivement) { student temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp; } } } } int main() { int n; cin>>n; struct student stu[n],stu1[n]; for(int i=0;i<n;++i) { cin>>stu[i].str>>stu[i].achivement; stu1[i]=stu[i]; } BubbleSort(stu1,n); for(int i=0;i<n;++i) { if(stu1[i].achivement>=60)cout<<stu1[i].str<<endl; } for(int i=0;i<n;++i) { if(stu[i].achivement<60)cout<<stu[i].str<<endl; } return 0; }
4、mysort
#include <iostream> using namespace std; struct A { int nouse1; int nouse2; int n; }; void mysort(void* a, int n, int w, int(*compare)(const void* , const void* )) { for(int i=n-1; i>=0; --i) { for(int j=0; j<i; ++j) { char* p1 = (char*)a+j*w; char* p2 = (char*)a+j*w+w; if(compare(p1,p2) > 0) { for(int k=0; k<w; ++k) { char tmp = p1[k]; p1[k] = p2[k]; p2[k] = tmp; } } } } } int MyCompare1( const void * e1,const void * e2) { int * p1 = (int * ) e1; int * p2 = (int * ) e2; return * p1 - * p2; } int MyCompare2( const void * e1,const void * e2) { int * p1 = (int * ) e1; int * p2 = (int * ) e2; if( (* p1 %10) - (* p2 % 10)) return (* p1 %10) - (* p2 % 10); else return * p1 - * p2; } int MyCompare3( const void * e1,const void * e2) { A * p1 = (A*) e1; A * p2 = (A*) e2; return p1->n - p2->n; } int a[20]; A b[20]; int main () { int n; while(cin >> n) { for(int i = 0;i < n; ++i) { cin >> a[i]; b[i].n = a[i]; } mysort(a,n,sizeof(int),MyCompare1); for(int i = 0;i < n; ++i) cout << a[i] << "," ; cout << endl; mysort(a,n,sizeof(int),MyCompare2); for(int i = 0;i < n; ++i) cout << a[i] << "," ; cout << endl; mysort(b,n,sizeof(A),MyCompare3); for(int i = 0;i < n; ++i) cout << b[i].n << "," ; cout << endl; } return 0; }
5、从字符串中取数
#include <iostream> #include <iomanip> using namespace std; double GetDoubleFromString(char * str) { static char * start ; if(str) start = str; //非数字 for(; *start &&(*start<'0' ||*start>'9') ; ++start ); //double char * q = start; //double for(; *start>='0' &&*start<='9' || *start=='.'; ++start ); //避免科学计数法 if( * start) * start = 'c'; return atof(q); } int main() { char line[300]; while(cin.getline(line,280)) { double n; n = GetDoubleFromString(line); while( n > 0) { cout << fixed << setprecision(6) << n << endl; n = GetDoubleFromString(NULL); } } return 0; }
第十一周测验
1、派
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; const double PI=acos(-1.0); const int maxn = 10000+10; double a[maxn]; bool can(double s,int n,int f) { int cnt=0; for(int i=1;i<=n;i++) cnt += floor(a[i]/s); return cnt>=f+1; //f+1 自己 } int main() { int n,f; scanf("%d%d",&n,&f); double L=0,R=0; for(int i=1;i<=n;i++) { scanf("%lf",&a[i]); a[i]=PI*a[i]*a[i]; R=max(a[i],R); } double mid; while(R-L>1e-5) { mid=L+(R-L)/2; if(can(mid,n,f)) L=mid; else R=mid; } printf("%.3lf\n",L); return 0; }
2、月度开销
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; int w[100010],ans; int tot,l,r,mid; bool f(int x,int n,int m) { int k=0,num=0; for (int i=1;i<=n;i++) { if (w[i]>x) return false; if (k+w[i]>x) k=w[i],num++; else k+=w[i]; } num++;//最后若干天也要划分为一个月,所以num++ if (num<=m) return true; else return false; } int main() { int n,m; cin>>n>>m; for (int i=1;i<=n;i++) { cin>>w[i]; tot+=w[i]; } r=tot; while (l<=r) { mid=(r+l)/2; if (f(mid,n,m)) ans=mid,r=mid-1; else l=mid+1; } cout<<ans; }
3、Aggressive cows
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> using namespace std; const int MAX = 100010; int a[MAX],n,m; bool C(int d) { int t = a[0],count = 1; for(int i = 1;i < n;i ++) { if(a[i] - t >= d) { count ++; t=a[i]; if(count >= m) return true; } } return false; } int solve() { int x = 0,y = a[n-1] - a[0]; while(x <= y) { int mid=(x+y)/2; if(C(mid)) x=mid + 1; else y=mid - 1; } return x - 1; } int main() { while(~scanf("%d%d",&n,&m)) { for(int i = 0;i < n;i ++) scanf("%d",&a[i]); sort(a,a+n); printf("%d\n",solve()); } return 0; }
第十二周测验
1、sort简单题
#include <iostream> #include <algorithm> using namespace std; int main() { int a[8] = {6,5,14,23,2,9,87,10 }; sort(a+1,a+7,greater<int>()); for(int i = 0;i < 8; ++i) cout << a[i] << "," ; return 0; }
2、还是sort简单题
#include <iostream> #include <algorithm> #include <cmath> using namespace std; struct Point{ int x; int y; }; struct Rule1 { bool operator() (const int &a1,const int &a2) { if(a1%10==a2%10) return a1>a2; return a1%10<a2%10; } }; struct Rule2 { bool operator() (const Point &a1,const Point &a2) { double dis1 = sqrt (abs(a1.x)*abs(a1.x)+abs(a1.y)*abs(a1.y)); double dis2 = sqrt (abs(a2.x)*abs(a2.x)+abs(a2.y)*abs(a2.y)); double d = dis1-dis2; double eps = 1e-6; if(d>-eps&&d<eps&&(a1.x)!=(a2.x)) return a1.x<a2.x; else if(d>-eps&&d<eps&&(a1.x)==(a2.x)) return a1.y<a2.y; return dis1<dis2; } }; int main() { int a[8] = {6,5,55,23,3,9,87,10 }; sort(a,a+8,Rule1()); for(int i = 0;i < 8; ++i) cout << a[i] << "," ; cout << endl; Point ps[8] = {{1,0},{0,1},{0,-1},{-1,0},{1,-1},{1,1},{2,0},{-2,0} } ; sort(ps,ps+8,Rule2()); for(int i = 0;i < 8; ++i) cout << "(" << ps[i].x << "," << ps[i].y << ")"; return 0; }
3、Set
#include <iostream> #include <set> #include <string> using namespace std; int main() { multiset<int> mst; set<int> st;//保存num,因为multiset删除了就没num了 string str; int n,num; cin >> n; while(n--) { cin >> str >> num; if(str=="add") { mst.insert(num); st.insert(num); cout << mst.count(num) << endl; } else if(str=="del") { cout << mst.count(num) << endl; mst.erase(num); } else if(str=="ask") { if (st.find(num)==st.end())//判断num是否加入过 cout << "0 0" << endl; else { cout <<"1 "; cout << mst.count(num) << endl; } } } return 0; }
4、热血格斗场
# include <iostream> # include <cstdio> # include <map> # include <cmath> using namespace std; map<int,int> member; void match(int power) { int lower = 0; int upper = 0; map<int,int>::iterator i; map<int,int>::iterator j; map<int,int>::iterator k; i = member.find(power); if ( i == member.begin() ) { j = i; j++; printf("%d %d\n",i->second,j->second); } else { j = i; k = i; j++; upper = j->first - i->first; k--; lower = i->first - k->first; if ( lower <= upper ) { printf("%d %d\n",i->second,k->second); } else { printf("%d %d\n",i->second,j->second); } } } int main() { int n = 0; int id = 0; int power = 0; cin >> n; member.insert(make_pair(1000000000,1)); for ( int i = 0; i < n; i++ ) { scanf("%d%d",&id,&power); member.insert(make_pair(power,id)); match(power); } return 0; }
第十三周测验
冷血格斗场
#include<cstdio> #include<iostream> #include<algorithm> #include<map> using namespace std; const int maxn=100005; map<int,int>mp; int main() { int n,id,power; scanf("%d",&n); mp[1000000000]=1; //facer for(int i=1;i<=n;i++) { scanf("%d%d",&id,&power); map<int,int>::iterator it; it=mp.lower_bound(power); if(it==mp.end()) it--; int t=abs(it->first-power); int idx=it->second; if(it!=mp.begin()) { it--; if(abs(it->first-power)<t || (abs(it->first-power)==t && it->second<idx)) idx=it->second; } printf("%d %d\n",id,idx); it=mp.find(power); if(it==mp.end() || it->second>id) mp[power]=id; } return 0; }