资源描述
(word完整版)50道C++编程练习题及解答
50道C/C++编程练习题
15
1、输入3个数,求最大值
int main()
{ int a,b,c,m;
cin〉>a〉〉b〉>c;
m=a;
if(b〉m) m=b;
if(c>m) m=c;
cout〈<m;
}
2、编程序,求方程ax2+bx+c=0的根
#include〈iostream>
#include<cmath>
using namespace std;
int main()
{
double a,b,c,d,x1,x2;
cin>>a>〉b〉>c;
if(a==0)
if(b==0) cout<<"error\n";
else cout<< "x=”〈〈—c/b〈<endl;
else
{ d=b*b-4*a*c;
if(fabs(d)<=1e—6)
cout<<”x1=x2="<〈—b/(2*a)<〈endl;
else if(d>1e—6)
{ x1=(-b+sqrt(d))/(2*a);
x2=(-b—sqrt(d))/(2*a);
cout〈<"x1="〈〈x1<〈”,x2="<<x2〈<endl;
}
else cout〈〈”方程无实根\n”;
}
}
3、输入一个成绩,打印相应的等级
int main()
{ int a;
cin 〉> a;
if(a>=90) cout〈<"A";
else if(a>=80) cout〈<”B";
else if(a〉=70) cout〈<”C”;
else if(a>=60) cout<<”D";
else cout〈〈”E”;
}
4、输入3个double类型的值,判断这3个值是否可以表示一个三角形的三条边。
int main()
{
double a,b,c;
cin〉〉a〉〉b>〉c;
if(a+b〉c && b+c〉a && c+a〉b)
cout〈〈”可以构成三角形";
else
cout〈〈"不可以构成三角形";
}
5、输入20个数,求其最大、最小和平均值
int main()
{
int i;
int a,max,min,s;
cin〉〉a;
max=min=a; s=a;
for(i=1;i<20;i++)
{ cin>〉a;
if(a〉max) max=a;
if(a<min) min=a;
s=s+a;
}
cout<<max〈<","<<min〈<”,"<〈s/20.0〈<endl;
}
6、输入若干个数,设输入的第一个数为后面要输入的数的个数,求平均值及最大值.
int main()
{
int a,m,s=0;
cin>>n;
cin〉>a;
m=a; s=a;
for(int i=1; i〈n; i++)
{ cin〉〉a;
s +=a;
if(a>m) m=a;
}
cout<<"平均值:”<<(double)s/n〈〈”,最大值:"<〈m<〈endl;
}
7、输入若干个数,输入-999表示结束,求平均值及最大值。
#include<iostream>
#include<iomanip〉
#include〈cstdlib>
using namespace std;
int main()
{ int n, count, sum, max;
double ave;
count = 0;
cin >> n;
sum = 0; max = n;
while( n != —999 )
{ sum = sum + n;
if( n 〉 max ) max = n;
count++;
cin >〉 n;
}
if( count != 0 )
{ ave=static_cast<double〉(sum) / count;
cout<<setiosflags(ios::fixed) 〈<setprecision(2);
cout<<"平均值为:”〈<ave<〈” 最大值为:"〈<max〈〈endl;
}
}
8、求和 s=1*1 + 2*2 + 3*3 +。。。+ 100*100
int main()
{ int i,t;
double s=0;
for(i=1; i<=100; i++)
{ t=i*i;
s=s+t;
}
}
9、印度国王的奖励,求和 s=20 + 21 + 22 +。.。+ 263
int main()
{ double t=1,s=0;
for(int i=0; i〈=63; i++)
{ s=s+t;
t=2*t;
}
cout〈<s/1。4e8<〈endl;
}
10、求和 s=1! + 2! + 3! +.。。+ 10!
int main()
{ int i;
long t,s;
t=1; s=0;
for(i=1; i<=10; i++)
{ t=t*i;
s=s+t;
}
}
11、求 e=1 + 1/1! + 1/2! + 1/3! + .。。
int main()
{ int i;
double t,e;
i=1; t=1; e=1;
while(t〉=1e-7)
{ t=t/i;
e=e+t;
i=i+1;
}
cout<〈e;
}
12、求PI值,PI/4 = 1 - 1/3 + 1/5 — 1/7 + .。.
int main()
{ int i,k;
double pi,t;
i=1; t=1; pi=0; k=1;
while(fabs(t)〉1e-8)
{ pi=pi+t;
i=i+2;
k=—k;
t=double(k)/i;
}
cout<<4*pi;
}
13、求PI值,PI/2 = 1 + 1/3 + 1/3*2/5 + 1/3*2/5*3/7 + 。。.
#include〈iostream〉
#include<cstdlib〉
int main()
{
int i,j;
double pi,t;
i=0; j=1; t=1; pi=0;
while(t>1e—18)
{ pi=pi+t;
i=i+1;
j=j+2;
t=t*i/j;
}
cout〈〈setprecision(17)〈<2*pi;
}
14、输入20个数,统计其中正数、负数和零的个数。
int main()
{
int a,n=0,m=0,s=0;
for(int i=1; i<=20; i++)
{ cin >> a;
if(a>0) n++;
else if(a<0) m++;
else s++;
}
cout〈〈n〈〈” "<<m<<” "<<s;
}
15、输入若干个整数,计算其中的奇数之和与偶数之和,假设输入0表示结束。
int main()
{ int a,n=0,m=0;
cin>>a;
while(a!=0)
{ if(a%2 == 0) n += a;;
else m += a;
cin 〉〉 a;
}
cout〈<n<<" "<<m;
}
16、写一函数,计算x的y次方(假设x、y都为正整数)。
int pow(int x, int y)
{ int s=1;
for(int i=1; i〈=y; i++)
s = s * x;
return s;
}
17、求水仙花数(一个三位数,其各位数字立方和等于该数字本身)
int main()
{ int i,a,b,c;
for(i=100;i<=999;i++)
{ a=i/100;
b=i/10%10;
c=i%10;
if(i==a*a*a+b*b*b+c*c*c)
cout<〈i<〈endl;
}
}
int main()
{ int i,a,b,c;
for(a=1;a〈=9;a++)
for(b=0;b〈=9;b++)
for(c=0;c〈=9;c++)
{ i=a*100+b*10+c;
if(i==a*a*a+b*b*b+c*c*c)
cout<<i<<endl;
}
}
18、编写一个函数,确定一个整数是否为完全数(一个数,等于他的因子之和)。用这个函数确定和打印1到1000之间的所有完全数。
int perfect(int n)
{ int i,s=1;
for(i=2;i〈=n/2;i++)
if(n%i==0) s=s+i;
if(s==n) return 1;
else return 0;
}
int main()
{ int n;
for(n=2;n〈=1000;n++)
if perfect(n)
cout<<n<<endl;
}
19、写一函数,求斐波那契数列的第n项。
int fib(int n)
{
int i,f1,f2,f;
if(n==1||n==2) return 1;
f1=1; f2=1;
for(i=3; i〈=n; i++)
{ f=f1+f2;
f1=f2;
f2=f;
}
return f;
}
20、写一个函数,取一个整数值并返回将此整数的各数字反序的数值
int reverse(int n)
{ int s=0;
while(n)
{ s = s * 10 + n % 10;
n /= 10;
};
return s;
}
21、写一个函数,将一个整数的各位数字的反序打印
void show(int n)
{ while(n)
{ cout << n % 10 <〈 " ";
n /= 10;
};
}
void show(int n)
{ if(n < 10) cout << n;
else
{ cout <〈 n % 10 <〈 ” ”;
show(n / 10);
}
}
22、写一个函数,将一个整数的各位数字的按顺序打印出来
void show(int n)
{
int k = 1, m = n;
while(m > 10)
{ k *= 10; m /= 10; }
while(n)
{ cout 〈< n / k 〈< ” ";
n %= k;
k /= 10;
};
}
void show(int n)
{ int a[10], i=0;
while(n)
{ a[i] = n % 10;
n /= 10;
i++;
}
for(int j=i—1; j>=0; j—-)
cout<〈a[j]〈<” ";
}
void show(int n)
{ if( n < 10 ) cout << n;
else
{ show( n / 10 );
cout 〈〈 " " 〈< n % 10;
}
}
23、求一个整数的各位数之和的函数
int sum(int n)
{ int s = 0;
while(n)
{ s += n % 10;
n /= 10;
};
return s;
}
24、写一函数,判断某个数是否素数,以及求1-1000之内的素数
#include<iostream〉
#include〈cmath〉
#include〈stdlib.h〉
using namespace std;
bool isprime(int n)
{ float k=sqrt(float(n));
for(int i=2; i<=k; i++)
if(n%i==0) return false;
return true;
}
int main()
{ for(int n=2; n<=1000; n++)
if(isprime(n)) cout<<setw(5)〈<n;
}
25、用筛法求1-1000之内的素数
#include〈iostream〉
#include〈cmath〉
#include〈stdlib。h>
#include<iomanip>
using namespace std;
int main()
{
int i,k,a[1001];
for(i=2; i<=1000; i++) a[i]=1;
float s=sqrt(float(1000));
for(i=2; i〈=s; i++)
if(a[i]==1)
{ k=2*i;
while(k<=1000)
{ a[k]=0;
k=k+i;
}
}
for(i=2; i<=1000; i++)
if(a[i]==1) cout<〈setw(5)<<i;
}
26、判断某一年是否闰年的函数
bool IsLeapYear(int y)
{ return (y%4 == 0 && y%100 != 0)||(y%400 == 0);
}
27、写一个函数,交换两个整型变量的值
void swap(int *p, int *q)
{ int t;
t=*p; *p=*q; *q=t;
}
void swap(int &a, int &b)
{ int t;
t=a; a=b; b=t;
}
28、求两个数的最大公约数,欧几里德算法(辗转相除法)
int gcd(int m, int n)
{ int k;
while(n!=0)
{ k=m%n; m=n; n=k; }
return m;
}
int gcd(int m, int n)
{ int k;
while((k=m%n)!=0)
{ m=n; n=k; }
return n;
}
int gcd(int m, int n)
{ while(m!=n)
{ if(m〉n) m=m-n;
else n=n—m;
}
return m;
}
29、求两个数的最小公倍数
int lcm(int m, int n)
{ int t,s;
if(m〈n) { t=m; m=n; n=t; }
s=m;
while(s%n != 0) s=s+m;
}
int lcm(int m, int n)
{
return m*n/gcd(m,n);
}
30、百钱买百鸡问题:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一,百钱买百鸡,问鸡翁、母、雏各几何?
int main()
{
int cock,hen,chick;
for(cock=0; cock〈=20; cock++)
for(hen=0; hen<=33; hen++)
{ chick=100-cock—hen;
if(5*cock+3*hen+chick/3。0==100)
cout<<setw(4)<<cock〈〈setw(4)<〈hen <<setw(4)〈<chick〈<endl;
}
}
31、编一程序,输入一行字符串,统计其中的小写英文字母的个数。
int main()
{ char s[100];
cin。getline(s,100);
int i=0,count=0;
while(s[i]!=’\0’)
{ if(s[i]〉='a’ && s[i]<='z')
count++;
i++;
}
cout〈〈count<<endl;
}
32、编一程序,输入一行字符串,将其中的大写英文字母改为小写,再输出。
int main()
{ char s[100];
int i;
cin。getline(s,100);
i=0;
while(s[i]!='\0’)
if(s[i]>='A' && s[i]〈='Z’)
s[i]=s[i]+32;
cout<<s〈<endl;
}
33、打印杨辉三角形(帕斯卡三角形),打印10行。
#include<iostream〉
#include<iomanip〉
using namespace std;
int main()
{ int a[10][10]={0};
for(int i=0; i<10; i++)
{ a[i][0]=1;
a[i][i]=1;
}
for(int i=1; i<10; i++)
for(int j=1; j<i; j++)
a[i][j] = a[i—1][j-1] + a[i—1][j];
for(int i=0; i〈10; i++)
{ for(int j=0; j<=i; j++)
cout〈<setw(4)〈<a[i][j];
cout<<endl;
}
}
34、打印一个九九乘法表
#include〈iostream〉
#include<iomanip〉
using namespace std;
int main()
{
for(int j=1; j〈=9; j++)
{ for(int i=1; i<=j; i++)
cout〈<i〈〈"*”<〈j〈<"="〈<setw(2)〈〈i*j<<" ";
cout〈<endl;
}
}
35、掷骰子10000次,统计得到各点数的次数。
int main()
{ int a[7]={0};
srand(time(0));
for(int i=1; i 〈= 10000 ; ++i)
++a[ 1 + rand()%6 ];
for(int i=1; i <= 6 ; ++i)
cout〈〈i<〈": ”〈〈a[i]〈<endl;
}
36、编写函数distance,计算两点(x1,y1)和(x2,y2)之间的距离。
double distance(double x1, double y1, double x2, double y2)
{
return sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1—y2) );
}
37、写一个程序,进行体操评分,依次输入10名评委所评分数,去除一个最高分和一个最低分,再算出平均分作为选手的得分.
int main()
{ int i;
float max,min,s,x;
max = 0; min = 10; s=0;
for(i=1;i〈=10;i++)
{ cin 〉> x;
s = s + x;
if(x<min) min = x;
if(x〉max) max = x;
}
s = s - min — max;
cout 〈< s/8;
}
38、写一函数,将一数组中的元素反转。
void reverse(int a[], int n)
{ for(int i=0; i〈n/2; i++)
swap(a[i],a[n-i-1]);
}
39、写一函数,在一个数组中找出最大元素的位置
int SearchMax(int a[], int n)
{ int k = 0;
for(int i=1; i〈n; i++)
if(a[i]〉a[k]) k = i;
return k;
}
40、找出一个二维数组中的鞍点,即该元素在该行上最大,在该列上最小。
41、写一个字符串拷贝函数
void strcpy(char *p, const char *q)
{
while(*p++=*q++);
}
char *strcpy(char *str1, const char *str2)
{
char *p=str1;
while(*str1++=*str2++);
return p;
}
42、写一个字符串比较函数
int strcmp(char *str1, const char *str2)
{
while(*str1 && * str2 && *str1==*str2)
{ str1++; str2++; }
return *str1-*str2;
}
int strcmp(char *str1, const char *str2)
{
while(*str1==*str2)
{ if(*str1==’\0') return 0;
str1++; str2++;
}
return *str1-*str2;
}
43、写一个字符串连接函数
char *strcat(char *str1, char *str2)
{
char *p=str1;
while(*str1!=0) str1++;
while(*str1++=*str2++);
return p;
}
44、写一个求字符串长度函数
int strlen(char *str)
{
int n=0;
while(*str!=’\0’)
{ n++; str++; }
return n;
}
45、写一函数,在一数组里查找某个值。
int search(int a[], int n, int key)
{ for(int i=0; i<n; i++)
if(a[i]==key)
return i;
return -1;
}
46、编一程序,求两个矩阵的乘积
47、计算某日是某年的第几天
bool isLeapYear(int y) //判断某一年是否闰年
{
return (y%4 == 0 && y%100 != 0)||(y%400 == 0);
}
int main()
{
int year,month,day,i,s=0;
int a[13]=(0,31,28,31,30,31,30,31,31,30,31,30,31};
cin〉>year>〉month〉>day;
for(i=1; i<month; i++)
s= s + a[i];
s = s + day;
if(isLeapYear(year) && month〉2) s++;
cout << s;
}
48、编写一个帮助小学生学习加法的程序,随机产生2个数,让学生输入答案。
#include〈iostream>
#include〈cstdlib>
using namespace std;
int main()
{
int x,y,z;
srand( time(0) );
x = rand() % 1000;
y = rand() % 1000;
cout <〈 x 〈< ” + " 〈〈 y 〈〈 " = ";
cin 〉〉 z;
while( z != 0 )
{
while( z != x+y )
{
cout〈〈” × 错误!请重做\n” ;
cout〈<x<<" + ”〈<y〈〈” = ";
cin〉>z;
}
cout<<" √ 正确!\n” ;
x = rand() % 1000;
y = rand() % 1000;
cout<〈x<<" + "〈<y〈<" = ”;
cin>>z;
}
}
49、从52个数里选13个数
int main()
{ int i,k,a[52],b[13];
for(i=0; i〈52; i++) a[i]=i+1;
srand(time(0));
for(i=0; i〈13; i++)
{ k = rand() % (52-i);
b[i] = a[k];
swap(a[k],a[51—i]);
}
for(i=0; i〈13; i++) cout〈〈b[i]〈〈” ";
}
50、求100!
#include 〈fstream〉
#include 〈cmath〉
#include〈iomanip>
using namespace std;
const long MOD = 10000;
int main()
{
int t,t0=time(0);
int len,n=100000;
unsigned long a[200000];
a[1]=1;
len=1;
for(int k=2; k〈=n; k++)
{
long carry=0;
for(int i=1; i<=len; i++)
{ unsigned long s = a[i]*k + carry;
carry = s / MOD;
a[i] = s % MOD;
}
while(carry 〉 0)
{ len++;
a[len] = carry % MOD;
carry = carry / MOD;
}
}
t=time(0);
int w=(len—1)*4 + int(log10(double(a[len]))) + 1;
ofstream fout(”factorial.txt”);
fout〈<n〈〈”! = \n"<〈a[len];
fout〈<setfill(’0’);
for(int i=len—1; i〉=1; i--) fout〈<setw(4)<<a[i];
fout〈<endl;
fout〈<”用时:"〈<t—t0〈〈"秒"<〈endl;
fout〈〈"数组元素个数:”<〈len<〈" 阶乘值位数:”〈<w〈〈endl;
return 0;
}
展开阅读全文