12-15真题
求数列1-1/2+1/3-1/4+…1/n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include<iostream> using namespace std; int main(){ int n; double total; cin>>n; for(int i=0;i<n;i++){ double flag=0; if(i%2==0){ flag=1.0; }else{ flag=-1.0; } total+=(flag)/(i+1); } cout<<"total is"<<total<<endl; return 0; }
|
输入字符串以!结束,小写转大写,存到text.txt文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include<iostream> using namespace std; int main(){ char str[100]; FILE *p; int i,len=0; cout<<"请输入一串英文字符"<<endl; while((str[len]=getchar())!='!'){ len++; } for(int i=0;i<len;i++) if(str[i]>='a'&&str[i]<'z') str[i]-=32; p=fopen("text.txt","a+"); fputs(str,p); fclose(p); return 0; }
|
输入某年某月某日,判断这一天是这一年的第几天
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include<iostream> using namespace std; int main(){ int year,month,day,sum,leap; cin>>year>>month>>day; switch(month){ case 1:sum=0;break; case 2:sum=31;break; case 3:sum=59;break; case 4:sum=90;break; case 5:sum=120;break; case 6:sum=151;break; case 7:sum=181;break; case 8:sum=212;break; case 9:sum=243;break; case 10:sum=273;break; case 11:sum=304;break; case 12:sum=334;break; default:cout<<"data error"<<endl;break; } sum+=day; if(year%400==0||year%4==0&&year%100!=0) leap=1; else leap=0; if(leap&&month>2)sum++; cout<<sum<<endl; return 0; }
|
将文件a和b中的两个字符串交叉合并成为一个字符串,并写入c
列:”aaaaa”和”bbb”合并结果为”abababaa”,”bbb”和”aaaaa”合并结果为”bababaaaa”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include<iostream> using namespace std; int main(){ FILE *fp,*fq,*fc; char ch; if((fp=fopen("a.txt","r"))==NULL){ cout<<"can't open file a.txt"; exit(0); } if((fp=fopen("b.txt","r"))==NULL){ cout<<"can't open file b.txt"; exit(0); } fr=fopen("c.txt","a+"); while(!feof(fp)) { ch=fgetc(fp); fputc(ch,fr); if(!feof(fq)){ ch=fgetc(fq); fputc(ch,fr); } } while(!feof(fq)){ ch=fgetc(fq); fputc(ch,fr); } fclose(fp); fclose(fq); fclose(fr); return 0; }
|
实现student类对学号姓名三门课的成绩进行管理
- 单独设置获取三门课的成绩
- 可以计算平均成绩
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include<iostream> using namespace std; struct student{ char num[6]; char name[20]; float score[3]; float aver; }stu[3]; void input(student stu[]){ int i; cout<<"please enter the information of student"<<endl; for(i=0;i<3;i++) scanf("%s%s%f%f%f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]); } void avg(struct student stu[]){ int i,j; float sum,aver; for(j=0;j<3;j++){ sum=0; for(i=0;i<3;i++) sum+=(stu[j].score[i]); aver=sum/3.0; stu[i].aver=aver; } } int main(){ int i; input(student stu[]); avg(student stu[]); for(i=0;i<3;i++) printf("%s%s%f%f%f%f",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].aver); return 0; }
|
输入n组等长字符串,用指针的方法进行冒泡排序,在函数中实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include<iostream> #include<cstring> using namespace std; void sort(char *str[],int size){ int i,j; char *temp; for(i=0;i<size-1;i++){ for(j=0;j<size-i-1;j++){ if(strcmp(str[j],str[j+1])>0){ temp=str[j]; str[j]=str[j+1]; str[j+1]=temp; } } } int main(){ char *p[200],str[200][20]; int i,n; cout<<"请输入字符串的个数:"; cin>>n; for(int i=0;i<n;i++){ cin>>str[i]; p[i]=str[i]; } sort(p,n); cout<<"排序后的结果:"; for(int i=0;i<n;i++){ cout<<p[i]<<endl; } return 0; }
|
输入一组字符倒序输出,输出结果保存到out.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include<stdio.h> void string(char *str,FILE *fp){ if(*str=='\0') return; else{ string(str+1,fp); fputc(*str,fp); } } int main(){ FILE *fp; char *ch; char str[100]; if(fp=fopen("out.txt","w+")==NULL){ cout<<"cannot open file"<<endl; exit(1); } cout<<"input a string:\n"; scanf("%s",str); ch=str; string(ch,fp); fclose(fp); }
|
黑色星期五,每个月中十三号是星期五称为黑色星期五
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include<iostream> using namespace std; int run(int year){ if(year%400==0||year%100!=0&&year%4==0) return 1; return 0; } int main(){ int n; cin>>n; int arr[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int sum=0; for(int i=1900;i<n;i++){ if(run(i)==1) sum+=366; else sum+=365; } if(run(n)==1){ arr[2]=29; } for(int i=1;i<=12;i++){ if(run(n)==1){ sum=13+sum; if(sum%7==5){ cout<<n<<"年"<<i<<"月"<<"13号"<<endl; } sum=sum-13+arr[i]; } } return 0; }
|
二叉树以二叉链表存储,证明二叉树是满二叉树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include<stdio.h> #include<math.h> int i=0; int sumtree(Bitree *T){ if(T){ i++; sumtree(T->lchild); sumtree(T->rchild); } }
int height(BiTree T){ if(T==NULL) return 0; int u=height(T->lchild); int v=height(T->rchild); if(u>v) return u+1; return v+1; } int main(){ BiTree *T; sumtree(T); int h=height(T); if(i==(pow(2,h)-1)) printf("此二叉树为满二叉树"); else printf("此二叉树不是满二叉树"); }
|
二叉排序树以x为根节点的子树。要求用非递归算法并释放掉该结点。(删除某个结点)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| bool delete(BiTree *p){ BiTree *q,*s; if(p->rchild==NULL) { p=p->lchild; }else if(p->lchild==NULL) { p=p->rchild; }else{ q=p; s=p->lchild; while(s->rchild){ q=s; s=s->rchild; } p->data=s->data; if(q!=p) q->rchild=s->lchild; else q->lchild=s->lchild; free(s); } return true; }
|
ABCDEF六个变量分别是1-6的整数,且各不相同,组成一个等边三角形,找出三边相等有多少种可能性。即A+B+D=A+C+F=D+E+F
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include<iostream> using namespace std; int main(){ int a,b,c,d,e,f; int cnt=0; for(a=6;a>=1;a--){ for(b=6;b>=1;b--){ if(b!=a){ for(c=6;c>=1;c--){ if(b!=c&&a!=c){ for(d=6;d>=1;d--){ if(d!=c&&d!=b&&d!=a){ for(e=6;e>=1;e--){ if(e!=a&&e!=b&&e!=c&&e!=d){ for(f=6;f>=1;f--){ if(f!=a&&f!=b&&f!=c&&f!=e){ if(a+b+d==a+c+f=d+e+f) cnt++; } } } } } } } } } } } cout<<cnt<<endl; return 0; }
|