C++程序设计练习二

1.  对运行中输入的x,计算级数。要求输出精度为 10的-8次方。

// 计算级数
#include <iostream>
#include <cmath>
using namespace std;
int main)
{
double X;
cin>>X;
double sum = 1;
double t = x;
for (int i = 2;abs(t) > le-8; ++)
{
sum += t:
1*=(-1*x/;
cout. precision(8);
cout<<fixed<<"×="<<X<<", sum="<<sum<<"In";
return 0;
}

2.  编程求 1!+2!+3!+4!+…+12!,并试着简化程序。

//阶乘和
#include <iostream>
using namespace std;
int main
int s=0;
forint i=1,item=1;1<13;++)
item*=i;
s+=item;
cout<<s<<endl;

3.  编程求所有的“水仙花数 (narcissus number)”。所谓“水仙花数”是指一个三位

数,其各位数字立方和等于该数本身。例如,153 是水仙花数,因为 153=1°+5°+3’。

//水仙花问题
#include<iostream>
#include<fstream>
using namespace std;
int main
{
ofstream fout("original.txt");
fout<<"水仙花问题"<<endl;
for(int i=100;<1000;i++)
{
int handred,ten,single; handred=i/100;
ten=(i-handred*100/10;
single=i%10;
if(handred* handred* handred+ ten *ten *ten+ single* single*single-i) continue; fout<<i<<endl;

4.  编程求 1000 之内的所有 “完数”。所谓 “完数”是指一个数恰好等于它的包括1

在内的所有不同因子之和。例如,6是完数,因为 6=1+2+3。

5.  编程求所有的对称数素数,且该数是对称的。所谓 “对称”是指一个数,倒过来

还是该数。例如,375不是对称数,因为倒过来变成了 573。

1/5-对数问题
#include iostream>
using namespace std;
int main
{
for(int i=100,handred,single;i<1000;i++)
{ handred=i/100;
single=i%10;
if(handred-single) continue; cout<<i<<endl;

6.  猴子吃桃问题。猴子第1天摘下若千桃子,当即吃了一半,还不过癮,又多吃了1个。第2天早上又将剩下的桃子吃掉一半,又多吃了1个。以后每天早上都吃了前1天剩下的一半,再多吃1个。到第10 天早上想再吃时,见只剩下 1个桃子了。试编程求第 1天共摘下多少桃子。

116—猴子吃桃问题
#includesiostream>
using namespace std;
int main
{
int s=1;
for(int i=1;¡<10;1++)
{
cout<<st<endl:/猴子有这么能吃么?把每
$=($+1)*2;

7.    用循环语向编程打印如下图案:

10.  编程求解母牛问题。若一头母牛,从出生起第四个年头开始每年生一头母牛,按

此规律,第么年时有多少头母牛?

#include <iostream> using namespace std;
int main)
int c = 1,n;
cout<<"please input a year:\n"; cin>>n;
for (int i = 4,a = 1,b = 1,temp;i <= n;i ++)
{
temp =a+c;
a = b;
b = C;
c = temp;
cout<<n<<" year later, there are cows: "<<<<endl; return 0;

11.一球从 100 米高度落下,每次落地后反跳回 原高度的一半,再落下。编程求它在

第10次落地时,共经过距离为多少米?第10次落地后的反弹有多高?

#include <iostream>
using namespace std;
int main
double h=100;
double s=100; int X;
cout<<"enter the times it falls:"<<endl; cin>>x;
if(x<=0) cout<<"it is not what we want."<<endl;
for(int i=2:i<=x:i++)
s+=h;
h=h/2;
cout<<s<<endl;
?

12. 将100 元钱兑换成 10元、5 元、1元,编程求不同的兑法数。要求每种兑法中都

要有 10元、5元和1元。

#include <iostream>
using namespace std;
int main(){
{
    for(int i=1:1<9:1++)
        for(int j=1:;j<19;++)
            for(int k=1;k<86;k++)
                ifa*10+j*5+k==100)
                    cout<<" (-0? Ja"<<i<<"§K?
                    a"<<k<<"张?"<<endl;
                    I "五?元a'<sj<<"张?" "о?
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~許諾~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值