2012年试题
一、
编写一个函数求数列
1−1/2+1/3−1/4…+1/n
利用主函数调用这个函数并输出结果
#include<iostream>
using namespace std;
double sum;
void function(int n)
{
for(int i = 1;i <= n;i ++)
{
if(i % 2)
sum += double(1.0/i);
else
sum += double(1.0/(-1*i));
}
}
int main()
{
int n;
cin>>n;
function(n);
cout<<sum<<endl;
return 0;
}
二、
编写程序,打印杨辉三角形,要求打印10行。
#include<iostream>
using namespace std;
int q[20][20];
int main()
{
for(int i = 0;i < 10;i ++)
{
q[i][i] = 1;
q[i][0] = 1;
}
for(int i = 1;i < 10;i ++)
for(int j = 0;j < 10;j ++)
{
if(j < i && j != 0)
q[i][j] = q[i - 1][j] + q[i - 1][j - 1];
}
for(int i = 0;i < 10;i ++)
{
for(int j = 0;j <= i;j ++)
cout<<q[i][j]<<" ";
cout<<endl;
}
return 0;
}
三、
从键盘输入一个字符串,将小写字母全部转化为大写字母,然后输入到一个磁盘文件test.txt中保存,输入的字符以!号结束。
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char ch,filename[100];
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("无法打开!");
exit(0);
}
printf("请输入一个准备存储的字符串(以!结束)");
ch = getchar();
while(ch != '!')
{
if(ch >= 'a' && ch <= 'z')
ch = ch - 32;
fputc(ch,fp);
putchar(ch);
ch = getchar();
}
fclose(fp);
putchar(10);
}
四、
输入某年某月某日,判断这一天是这一年的第几天。
#include<iostream>
using namespace std;
int year,month,day;
int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int function(int n)
{
if((n % 4 == 0 && n % 100 != 0) || n % 400 == 0)
return 1;
else
return 0;
}
int main()
{
cin>>year>>month>>day;
int sum = 0;
for(int i = 1;i < month;i ++)
{
if(i == 2) sum += months[i] + function(year);
else
sum += months[i];
}
sum += day;
cout<<sum<<endl;
return 0;
}
五、
设在文件a.txt和文件b.txt中分别存有两个字符串,设计一个程序,将两个字符串按依次交叉的方式合并成一个字符串,并将结果存入到a.txt中。
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp,*fq,*fr;
char ch;
if((fp=fopen("a.txt","r+"))==NULL)
{
printf("找不到文件a.txt!");
exit(0);
}
if((fq=fopen("b.txt","r+"))==NULL)
{
printf("找不到文件b.txt!");
exit(0);
}
fr = fopen("c.txt","w+");
while(!feof(fp))
{
ch=fgetc(fp);
fputc(ch,fr);
if(!feof(fq))
{
ch = fgetc(fq);
fputc(ch,fr);
}
}
while(!feof(fq))
{
ch=getc(fq);
fputc(ch,fr);
}
rewind(fp);
rewind(fr);
while(!feof(fr))
{
ch = fgetc(fr);
fputc(ch,fp);
}
fclose(fp);
fclose(fq);
fclose(fr);
return 0;
}
六、
有一篇文章,共有三行文字,每行文字有60个字符,要求分别求出其中的英文大写字母小写字母,数字以及其他字符的个数。
#include<iostream>
#include<string>
using namespace std;
string st1,st2,st3;
int main()
{
int low = 0,up = 0,num = 0,oth = 0;
cin>>st1>>st2>>st3;
for(int i = 0;i < st1.size();i ++)
{
if('0' <= st1[i] && st1[i] <= '9')
num ++;
else if('a' <= st1[i] && st1[i] <= 'z')
low ++;
else if('A' <= st1[i] && st1[i] <= 'Z')
up ++;
else oth ++;
}
for(int i = 0;i < st2.size();i ++)
{
if('0' <= st2[i] && st2[i] <= '9')
num ++;
else if('a' <= st2[i] && st2[i] <= 'z')
low ++;
else if('A' <= st2[i] && st2[i] <= 'Z')
up ++;
else oth ++;
}
for(int i = 0;i < st3.size();i ++)
{
if('0' <= st3[i] && st3[i] <= '9')
num ++;
else if('a' <= st3[i] && st3[i] <= 'z')
low ++;
else if('A' <= st3[i] && st3[i] <= 'Z')
up ++;
else
oth ++;
}
cout<<up<<" "<<low<<" "<<num<<" "<<oth<<" "<<endl;
return 0;
}
七、
从键盘上输入三个整数a,b,c输出这三个数,然后交换他们中的数,把a中原来的值给b,把b中原来的值给c,把中原来的值给a,并输出交换后的数字。
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
cin>>a>>b>>c;
d = c;
c = b;
b = a;
a = d;
cout<<a<<" "<<b<<" "<<c<<endl;
return 0;
}
八、
实现一个学生类Student,对学号、姓名、三门课的成绩进行管理。
(1) 类应该能够单独设置和获取三门课的成绩
(2) 可以计算平均成绩
坚持的很棒,加油
好