1 / 50
文档名称:

C编程基础题训练答案.docx

格式:docx   大小:68KB   页数:50页
下载后只包含 1 个 DOCX 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

C编程基础题训练答案.docx

上传人:guoxiachuanyue 2021/2/17 文件大小:68 KB

下载得到文件列表

C编程基础题训练答案.docx

相关文档

文档介绍

文档介绍:1、输入 3 个数,求最大数。
#include <iostream>
using namespace std;
int main()
{int a,b,c,max;
cout<<" 请输入三个数字: "<<endl;
cin>>a>>b>>c;
max=(a>b)?a:b;
if(c>max)
max=c;
cout<<" 最大值: "<<max<<endl;
return 0;
}
2、韩信点兵:有一个数,用 3除余 2;用 5除余 3;用 7除余 2;求满足条 件的最小数。
#include <iostream>
using namespace std;
int main()
{
int a;
for(a=1;a<1000;a++)
if(a%3==2&&a%5==3&&a%7==2)
{cout<<a;
break;}
}
return 0;
}
3、 求 1+2+3+…+100
#include <iostream>
using namespace std;
int main()
{int s=0;
int a=1;
do
{
s=s+a;
a=a++;
}
while(a<101);
cout<<s<<endl;
return 0;
}
4、 求 1-2+3-4+ …-100
#include <iostream>
using namespace std;
int main()
{
int a,s=0,s1=0,s2=0;
for(a=1;a<101;a++,a++)
{
s1+=a;
}
for(a=-2;a>-101;a=a-2)
{
s2=s2+a;
}
s=s1+s2;
cout<<s<<endl;
return 0;
}
5、求 1 + 1/2+1/3+ …+1/100
#include <iostream>
using namespace std;
void main()
float a,m;
float s=0;
for(a=1;a<101;a++)
{m=1/a;
s=s+m;
}
cout<<"s="<<s<<endl;
}
6、求输入n求n!(需判断n的合法性)递归调用
#include <iostream>
using namespace std;
int fac(int n)
{
if(n==0) return 1;
else
return fac(n-1)*n;
}
int main()
{
while(1) {int x,y,n;
cout<<" 输入一个整数: \n"; cin>>x;
fac(x);
cout<<fac(x)<<endl;break;
}
return 0;
}
7、求 1 ! +2! +3! + …+ 10!
#include <iostream>
using namespace std;
int main()
{
int s=0;
int t=1;
int n;
for(n=1;n<11;n++)
{
t=t*n;
s=s+t;
}
cout<<"1!+2!+3!+...+10!="<<s<<endl;
return 0;
}
&求 1 + 1/2!+1/3!+ …1/n!,直到 1/n!<1E-5 为止 第一种方法:
#include <iostream> using namespace std;
void main()
{ double s=0;
double t=1;
double n=1;
do{
t=t*n;
s=s+1/t;
n++;
}
while (t<1e5);
cout<<"1/1!+1/2!+1/3!+...+1/n!="<<s<<endl;
}
第二种方法:
#include <iostream> using namespace std;
void main()
double sum=; // 结果
double now=; // 现在的 1/n!
double cnt=; // 现在的 n
while(now>=1e-5)
{
sum+=now;
now/=cnt;
cnt+=;
}
cout<<sum<<endl;}
9、用公式求 ex=1+x+x2/2!+x3/3!+ …+xn/n!。n 取 20。 #include<iostream> using namespace std; int pow(int x, int n);