精华1780
威望224359
K币205432 元
注册时间2005-10-7
在线时间5529 小时
最后登录2019-3-21
荣誉会员
安宁的忧郁
 
- 精华
- 1780
- 威望
- 224359
- K币
- 205432 元
- 注册时间
- 2005-10-7

|
2000年
1.输入任意4个字符(如:abcd),并按反序输出(如:dcba)
#include<iostream.h>
void main()
{
char s[5],t[5];
int i;
cout<<"请输入四个字符:";
cin>>s;
for(i=0;i<4;i++)
t=s[3-i];
t[4]='\0';
cout<<"反序后结果为:"<<t<<endl;
}
2.设a、b、c均是 0 到 9 之间的数字,abc、bcc是两个三位数,且有:abc+bcc=532。求满足条件的所有a、b、c的值。
说明:本题结果唯一。
#include<iostream.h>
void main()
{
int a,b,c;
for(a=1;a<10;a++)
for(b=1;b<10;b++)
for(c=0;c<10;c++)
if((a*100+b*10+c+b*100+c*10+c)==532)
cout<<"满足条件的a,b,c为:"<<a<<","<<b<<","<<c<<endl;
// cout<<"满足条件的a,b,c为:3,2,1"<<endl;
}
3.一个数如果恰好等于它的各因子(该数本身除外)子和,如:6=3+2+1,则称其为“完数”;若因子之和大于该数,则称其为“盈数”。求出2到60之间所有“完数”和“盈数”,并以如下形式输出: E: e1 e2 e3 ......(ei为完数) G: g1 g2 g3 ......(gi为盈数)
#include<iostream.h>
void save(int s[],int x,int flag);
int fun(int x);
void main()
{
int E[60],G[60];
int flag,i;
for(i=6;i<=60;i++)
{
flag=fun(i);
if(flag==0)
save(E,i,0);
else if(flag==1)
save(G,i,1);
}
cout<<"E:";
for(i=0;E!=0;i++)
cout<<E<<" ";
cout<<endl<<"G:";
for(i=0;G!=0;i++)
cout<<G<<" ";
cout<<endl;
}
void save(int s[],int x,int flag)
{
static i=0,j=0;
if(flag==0)
{
s=x;
s[i+1]=0;
i++;
}
else
{
s[j]=x;
s[j+1]=0;
j++;
}
}
int fun(int x)
{
int i,sum=0;
for(i=1;i<=x/2;i++)
if(x%i==0)
sum+=i;
if(sum==x)
return 0;
else if(sum>x)
return 1;
else
return -1;
}
4.从键盘输入4个学生的数据(包括姓名、年龄和成绩),并存放在文件sf1上。从该文件读出这些数据,按成绩从高到底排序,并输出其中成绩次高者的所有数据。
2001年A
1、编写程序,计算下列分段函数y=f(x)的值。 y= -x+2.5 0<= x <2 y=2-1.5(x-3)(x-3) 2<= x <4 y=x/2-1.5 4<= x <6
#include<iostream.h>
float fun(float x)
{
float y;
if(x>=0&&x<2)
y=2.5-x;
else if(x>=2&&x<4)
y=2-1.5*(x-3)*(x-3);
else if(x>=4&&x<6)
y=x/2-1.5;
return y;
}
void main()
{
float x;
cout<<"请输入x的值:";
cin>>x;
while(x<0||x>6)
{
cout<<"非法,请重新输入:";
cin>>x;
}
cout<<"结果为:"<<fun(x)<<endl;
}
2、编写程序,读入一个整数 N。若 N 为非负数,则计算 N 到 2N 之间的整数和;若 N 为一个负数,则求 2N 到 N 之间的整数和。
说明:用了下公式
#include<iostream.h>
#include<math.h>
void main()
{
int N;
cout<<"请输入一个整数:";
cin>>N;
cout<<N<<"到"<<2*N<<"之间的整数和为:"<<(abs(N)+1)*1.5*N<<endl;
}
3、设N是一个四位数,它的 9 倍恰好是其反序数(例如:1234的反序数是4321),求N的值。
说明:本题结果唯一
4、N个人围成一圈顺序编号,从1号开始按1、2、3顺序报数,报3者退出圈外,其余的人再从1、2、3开始报数,报3的人再退出圈外,依次类推。请按退出顺序输出每个退出人的原序号。要求使用环行链表编程。
说明:约瑟夫环
#include<iostream.h>
#include<malloc.h>
typedef struct node
{
int num;
struct node *next;
}LNode;
void main()
{
int N,i;
LNode *head,*p,*q;
cout<<"请输入人数:";
cin>>N;
p=(LNode*)(malloc(sizeof(LNode)));
p->num=1;
head=p;
for(i=1;i<N;i++)
{
p->next=(LNode*)(malloc(sizeof(LNode)));
p=p->next;
p->num=i+1;
}
p->next=head;
p=head;
cout<<"出列顺序为:";
while(p->next!=p)
{
q=p->next;
p=q->next;
q->next=p->next;
cout<<p->num<<" ";
delete p;
p=q->next;
}
cout<<p->num<<endl;
delete p;
}
2001年B
1、请输入高度h,输入一个高为h,上底边长为h的等腰梯形(例如h=4,图形如下)。
****
******
********
**********
#include<iostream.h>
void main()
{
int i,j,h;
cout<<"请输入h:";
cin>>h;
for(i=0;i<h;i++)
{
for(j=0;j<h+i;j++)
cout<<"*";
cout<<endl;
}
}
2、请编写一个程序,从键盘上输入n(n的范围是1~20),求n的阶乘。
#include<iostream.h>
int fun(int n);
void main()
{
int n;
cout<<"请输入n:";
cin>>n;
while(n>20||n<1)
{
cout<<"非法,请重新输入:";
cin>>n;
}
cout<<"n的阶乘为:"<<fun(n)<<endl;
}
int fun(int n)
{
int i,result=1;
for(i=1;i<=n;i++)
result*=i;
return result;
}
3、从键盘上任意输入一个长度不超过20的字符串,对所输入的字符串,按照ASCII码的大小从小到大进行排序,请输出排序后的结果。
#include<iostream>
#include<string>
using namespace std;
void main()
{
char str[21];
int i,j,len;
char ch;
cout<<"请输入字符串:";
cin.getline(str,20);
len=strlen(str);
for(i=0;i<len-1;i++)
for(j=0;j<len-1-i;j++)
if(str[j]>str[j+1])
{
ch=str[j];
str[j]=str[j+1];
str[j+1]=ch;
}
cout<<"排序后的字符串为:"<<str<<endl;
}
2002年A
1、某人有8角的邮票5张,1元的邮票4张,1元8角的邮票6张,用这些邮票中的一张或若干张可以得到多少中不同的邮资?
说明:这道题真的找不到好的算法,希望有好算法的朋友发站短给我,多谢
#include<iostream.h>
void main()
{
int i,j,k;
for(i=0;i<=5;i++)
for(j=0;j<=4;j++)
for(k=0;k<=6;k++)
cout<<i*0.8+j+k*1.8<<" ";
}
2、输入n值,使用递归函数,求杨辉三角形中各个位置上的值,按照如下形式打印输出图形。例如:当n=6时。 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
说明:递归
#include<iostream.h>
int fun(int n,int k)
{
if(k==0||n==k)
return 1;
else
return fun(n-1,k-1)+fun(n-1,k);
}
void main()
{
int n,i,j;
cout<<"请输入n:";
cin>>n;
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
cout<<fun(i,j)<<" ";
cout<<endl;
}
}
2002年B
1、打印所有不超过n(n<256)的,其平方具有对称性质的数。如11*11=121。
#include<iostream.h>
#include<stdlib.h>
bool fun(int n)
{
int x,i,t;
char str[10];
x=n*n;
i=0;
while(x)
{
t=x%10;
str[i++]=t+48;
x/=10;
}
str='\0';
if(n*n==atoi(str))
return true;
else
return false;
}
void main()
{
int n,i;
cout<<"请输入n:";
cin>>n;
for(i=1;i<=n;i++)
if(fun(i))
cout<<i<<" ";
cout<<endl;
}
2、编写一个求菲波那奇数列的递归函数,输入n值,使用该递归函数,输出如下图形。例如:当n=6时。 0
0 1 1
0 1 1 2 3
0 1 1 2 3 5 8
0 1 1 2 3 5 8 13 21
0 1 1 2 3 5 8 13 21 34 55
说明:递归
#include<iostream.h>
int fun(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return fun(n-2)+fun(n-1);
}
void main()
{
int n,i,j;
int *p=new int[60];
cout<<"请输入n:";
cin>>n;
for(i=0;i<2*n-1;i++)
p[ i ]=fun(i);
for(i=0;i<n;i++)
{
for(j=0;j<2*i+1;j++)
cout<<p[j]<<" ";
cout<<endl;
}
}
2003年
1、输入球的中心点和球上某一点的坐标,计算球的半径和体积。
#include<iostream.h>
#include<math.h>
void main()
{
double r;
int x1,x2,y1,y2,z1,z2;
const double PI=3.1416;
cout<<"请输入中心点坐标:";
cin>>x1>>y1>>z1;
cout<<"请输入球上某一点的坐标:";
cin>>x2>>y2>>z2;
r=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
cout<<"半径为:"<<r<<endl;
cout<<"体积为:"<<(4*PI*r*r*r)/3<<endl;
}
2、手工建立一个文件,文件种每行包括学号、姓名、性别和年龄。每一个属性使用空格分开。文件如下: 01 李江 男 21 02 刘唐 男 23 03 张军 男 19 04 王娜 女 19 根据输入的学号,查找文件,输出学生的信息。
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
using namespace std;
void main()
{
const int LEN=100;
char s[LEN],k[LEN],*temp;
int num;
cout<<"请输入要查找学生的学号:";
cin>>num;
ifstream fin("myfile.txt");
while(fin.getline(s,LEN))
{
strcpy(k,s);
temp=strtok(k," ");
if(atoi(temp)==num)
{
cout<<num<<"号学生的信息为:"<<s<<endl;
break;
}
}
}
3、输入年月日,计算该填是本年的第几天。例如1990年9月20日是1990年的第263天,2000年5月1日是2000年第122天。(闰年:能被400正除,或能被4整除但不能被100整除。每年1、3、5、7、8、10为大月)
#include<iostream.h>
void main()
{
int i,sum=0,year,month,day;
const int num[12]={31,28,31,30,31,30,31,31,30,31,30,31};
cout<<"请输入年月日:";
cin>>year>>month>>day;
for(i=0;i<month-1;i++)
sum+=num;
sum+=day;
if((year%4==0&&year%100!=0)||year%400==0)
sum+=1;
cout<<year<<"年"<<month<<"月"<<day<<"日是"<<year<<"年的第"<<sum<<"天"<<endl;
}
2004年
第一题是建立一个角类,在这个类中重载减号运算符,并实现求出角度的正弦值的函数。
#include<iostream.h>
#include<math.h>
const double PI=3.1416;
class angle
{
public:
angle() {}
angle(int x) {X=x;}
angle operator - (angle c);
void xsin();
private:
int X;
};
#include"angle.h"
void angle::xsin()
{
double x;
x=(X*PI)/180;
cout<<"正弦值为:"<<sin(x)<<endl;
}
angle angle: perator - (angle c)
{
return angle(X-c.X);
}
#include"angle.h"
void main()
{
angle c1(90),c2(60),c3;
c3=c1-c2;
c1.xsin();
c2.xsin();
c3.xsin();
}
第二题是建立一个求一元二次方程解的类(a*x^2+b*x+c=0),输入系数a,b,c的值后打印出这个方程的解来,也比较简单。需要注意的是系数a不能为零以及方程有无解,单解还是双解的情况。
#include<iostream.h>
class equation
{
public:
equation(float a,float b,float c):a(a),b(b),c(c) {}
float D() {return b*b-4*a*c;}
void fun();
private:
float a,b,c;
};
#include<math.h>
#include"equation.h"
void equation::fun()
{
float d=D();
if(d==0)
cout<<"单解为:"<<-b/(2*a)<<endl;
else if(d>0)
cout<<"双解为:"<<(-b+sqrt(d))/(2*a)<<","<<(-b-sqrt(d))/(2*a)<<endl;
else
cout<<"复数解为:"<<-b/(2*a)<<"+"<<sqrt(-d)/(2*a)<<"i,"<<-b/(2*a)<<"+"<<-sqrt(-d)/(2*a)<<"i"<<endl;
}
#include"equation.h"
void main()
{
float a,b,c;
cout<<"请输入(a b c):";
cin>>a>>b>>c;
while(a==0)
{
cout<<"非法,请重新输入(a b c):";
cin>>a>>b>>c;
}
equation e(a,b,c);
e.fun();
}
第三道题是实现一个多项式的类(a+b*x+c*x^2+d*x^3+...+),要求输入该多项式的系数和x的值后打印出这个多项式的值。这道题本身并不难,但他要求用好的算法(实际上就是递归)。
#include<iostream.h>
float fun(float s[],float x,int n,int N)
{
if(n==0)
return s[N];
else
return s[N-n]+x*fun(s,x,n-1,N);
}
void main()
{
int i,N;
float num[60],x;
cout<<"请输入最高项次数:";
cin>>N;
cout<<"请依次输入系数:";
for(i=0;i<=N;i++)
cin>>num[ i ];
cout<<"请输入x:";
cin>>x;
cout<<"结果为:"<<fun(num,x,N,N)<<endl;
}
2005年
第一题是给定一个程序,关于字符串的,要求输入并调试,说出此程序的意图。意图是按字母顺序对两个字符串比较排序。第二问要求用尽可能少的语句对该程序进行修改,使其能够对两个字符串比较长度排序。本题满分20。
第二题是要求编写一个日期类,要求按xxxx-xx-xx的格式输出日期,实现加一天的操作,不考虑闰年问题,所有月份设为30天。本题黑盒测试时,输入2004年3月20日,得到加一天后时间为2004-3-21,能得一部分分数。输入2004年3月30日,得到加一天后时间为2004-4-1,能得一部分分数。输入2004年12月30日,得到加一天后时间为2005-1-1,且有时间越界处理,能得全部分数。本题满分30。
#include<iostream.h>
class date
{
public:
date(int y,int m,int d):year(y),month(m),day(d){}
void display();
void addDay();
private:
int year;
int month;
int day;
};
#include"date.h"
void date::display()
{
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
void date::addDay()
{
day++;
if(day>30)
{
day%=30;
month++;
if(month>12)
{
month%=12;
year++;
}
}
}
#include"date.h"
void main()
{
int y,m,d;
cout<<"请输入(年 月 日):";
cin>>y>>m>>d;
while(y<0||m<0||m>12||d<0||d>30)
{
cout<<"非法,请重新输入(年 月 日):";
cin>>y>>m>>d;
}
date d1(y,m,d);
d1.addDay();
d1.display();
}
第三题要求编写一个复数类,要求有4条。一是有构造函数,能对复数初始化。二是对复数c1,c2,c3.....能实现连加运算,令c=c1+c2+c3+.....此处可以重载加法操作符。三是有函数实现两个复数相加,并按照a+ib的形式输出。四是能实现对一个复数c=a+ib,定义double x=c有效,使x的值为实部和虚部之和。本题满分50。”
#include<iostream>
using namespace std;
class complex
{
public:
complex(double r=0.0,double i=0.0) {real=r;imag=i;}
complex operator + (complex c2);
void display();
operator double () //重载类型转换操作
{
return (real+imag);
}
private:
double real;
double imag;
};
#include"complex.h"
complex complex::operator + (complex c2)
{
return complex(real+c2.real,imag+c2.imag);
}
void complex::display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
#include"complex.h"
void main()
{
complex c1(5,4),c2(2,10),c3(6,9),c4;
c4=c1+c2+c3;
c4.display();
double x=c4;
cout<<x<<endl;
}
2006年
1.写一个程序判断字符串中数字的位置(不限制使用面向对象编程)
例如: 输入 a3b4c5
输出 2 4 6
#include<iostream>
#include<ctype.h>
#include<string>
using namespace std;
void main()
{
string str;
int i;
cout<<"请输入字符串:";
getline(cin,str);
for(i=0;str[ i ]!='\0';i++)
if(isdigit(str[ i ]))
cout<<i+1<<" ";
cout<<endl;
}
2.写一个类,能接受int型的变量,接收变量后能存储原变量(譬如12345)和其反向变量(54321),最多处理数量为10个,当输入达到10个或者输入变量为0的时候停止。并且在类销毁前输出存储的所有变量。
例如: 输入:12345,2234,0
输出:12345 54321
2234 4322
#include<iostream.h>
#include<stdlib.h>
class CInverse
{
public:
CInverse();
void inverse();
~CInverse();
private:
int num[10];
int inverseNum[10];
int countNum;
};
#include"CInverse.h"
CInverse::CInverse()
{
int t,i;
cout<<"请输入整数,以0停止:";
cin>>t;
for(i=0;i<10&&t!=0;i++)
{
num[ i ]=t;
cin>>t;
}
countNum=i;
}
void CInverse::inverse()
{
char str[10];
int t,i,j,x;
for(i=0;i<countNum;i++)
{
x=num;
j=0;
while(x)
{
t=x%10;
str[j++]=t+48; //加48
x/=10;
}
str[j]='\0';
inverseNum=atoi(str);
}
}
CInverse::~CInverse()
{
int i;
for(i=0;i<countNum;i++)
cout<<num<<" "<<inverseNum<<endl;
}
#include"CInverse.h"
void main()
{
CInverse c1;
c1.inverse();
}
3.写一个CTriangle类,要求可以接受 CTriangle(y,x)形式的构造,创建在坐标系中的直角三角形样子如下
三点的坐标分别是A(0,y) B(0,0) C(x,0)
实现+运算,并且能够处理键盘连续输入若干个(少于十个)三角形,并且连加(相加时候三角形边长长度相加,方向同第一个三角形)。输入0后结束并输出最后得出的三角形的三个坐标值。
#include<iostream.h>
class CTriangle
{
public:
CTriangle(int y=0,int x=0) {Ay=y;Cx=x;}
CTriangle operator + (CTriangle c2);
void set(int y=0,int x=0) {Ay=y;Cx=x;}
void display();
private:
int Ay;
int Cx;
};
#include"CTriangle.h"
CTriangle CTriangle::operator + (CTriangle c2)
{
return CTriangle(Ay+c2.Ay,Cx+c2.Cx);
}
void CTriangle::display()
{
cout<<"A(0,"<<Ay<<") B(0,0) C("<<Cx<<",0)"<<endl;
}
#include"CTriangle.h"
void main()
{
int y,x,i,N;
CTriangle c[10],sum;
cout<<"请输入坐标:";
cin>>y;
for(i=0;y!=0;i++)
{
cin>>x;
c[ i ].set(y,x);
cin>>y;
}
N=i;
sum.set(); //置0
for(i=0;i<N;i++)
sum=sum+c;
sum.display();
}
2007年
1。一个小球,从高为H的地方下落,下落弹地之后弹起高度为下落时的一半,
比如第一次弹起高度为H/2,如此往复,计算从小球H高度下落到第n次弹地
往返的总路程。
要求:1。用递归的方法实现
2。输入H和n,输出结果
3。注意程序的健壮性
4。可以用C或C++实现
#include<iostream.h>
#include<math.h>
float fun(int n)
{
if(n==1)
return 1;
else
return fun(n-1)+1/pow(2,n-2);
}
void main()
{
float H;
int n;
cout<<"请输入H和n:";
cin>>H>>n;
cout<<"小球从"<<H<<"高度下落到第"<<n<<"次弹地往返的总路程为:"<<fun(n)*H<<endl;
}
2。创建一个CPoint类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数,
运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。可以根据需要
加入自己的成员变量或成员函数
要求:1。输入两个点的坐标,输出两个点之间的距离
2。重载运算符为“-”
#include<iostream.h>
class CPoint
{
public:
CPoint(int x=0,int y=0) {X=x;Y=y;}
float operator - (CPoint c2);
private:
int X;
int Y;
};
#include"CPoint.h"
#include<math.h>
float CPoint::operator - (CPoint c2)
{
return sqrt((X-c2.X)*(X-c2.X)+(Y-c2.Y)*(Y-c2.Y));
}
#include "CPoint.h"
void main()
{
int x1,y1,x2,y2;
cout<<"请输入两个点的坐标:";
cin>>x1>>y1>>x2>>y2;
CPoint c1(x1,y1),c2(x2,y2);
cout<<"两点间的距离为:"<<c1-c2<<endl;
}
3。创建一个CTriangle类,需要用到第二题中创建的类,即用3点来代表一个三角形,
输入三个点的坐标,实现判断此三角形是不是直角三角形,并输出此三角形的周长。
可以根据需要加入自己的成员变量或成员函数
要求:1。输入三个点的坐标,输出周长并给出是否直角三角形的信息
2。注意程序的健壮性
#include<iostream.h>
class CPoint
{
public:
CPoint(int x=0,int y=0) {X=x;Y=y;}
float operator - (CPoint c2);
private:
int X;
int Y;
};
class CTriangle
{
public:
CTriangle(CPoint a,CPoint b,CPoint c):A(a),B(b),C(c)
{
AB=A-B;
BC=B-C;
AC=A-C;
}
void display();
bool fun();
private:
CPoint A,B,C;
float AB,BC,AC;
};
#include"CTriangle.h"
#include<math.h>
float CPoint::operator - (CPoint c2)
{
return (float)sqrt((X-c2.X)*(X-c2.X)+(Y-c2.Y)*(Y-c2.Y));
}
void CTriangle::display()
{
cout<<"周长为:"<<AB+BC+AC<<endl;
}
bool CTriangle::fun()
{
float a=AB,b=BC,c=AC,t;
if(a>c)
{
t=a;
a=c;
c=t;
}
if(b>c)
{
t=b;
b=c;
c=t;
}
if(fabs(a*a+b*b-c*c)<10e-6)
return true;
else
return false;
}
#include"CTriangle.h"
void main()
{
int x1,y1,x2,y2,x3,y3;
cout<<"请输入三个顶点的坐标:";
cin>>x1>>y1>>x2>>y2>>x3>>y3;
CPoint p1(x1,y1),p2(x2,y2),p3(x3,y3);
CTriangle c(p1,p2,p3);
if(c.fun())
{
cout<<"是直角三角形"<<endl;
c.display();
}
else
cout<<"不是直角三角形"<<endl;
}
2008年
1、存储一组姓名,如 Apple,Tom,Green,Jack 要求能排序、按字母顺序插入、并显示。
#include<iostream>
#include<string>
using namespace std;
void main()
{
const int N=10;
string s[N],temp;
int i,j,K;
cout<<"请输入字符串个数:";
cin>>K;
cout<<"请输入"<<K<<"个字符串:"<<endl;
for(i=0;i<K;i++) //插入排序
{
cin>>temp;
for(j=i;j>0;j--)
if(temp<s[j-1])
s[j]=s[j-1];
else
break;
s[j]=temp;
}
cout<<"排序后的结果为:"<<endl;
for(i=0;i<K;i++)
cout<<s<<endl;
} |
评分
-
查看全部评分
|