C++项目参考解答:小试循环

【项目-小试循环】

  写出实现下面求解任务的程序【提示:m是一个变量,在程序中输入】
  (1)求1到m的平方和
  (2)求1到m间所有奇数的和
  (3)求1到m的倒数和,即

1+12+13+14+...+1m

  (4)求值:
112+1314+...+(1)(m+1)×1m

  (5)求m!,即
1×2×3×...×m


【参考解答】

  写出实现下面求解任务的程序【提示:m是一个变量,在程序中输入】
  (1)求1到m的平方和

#include <iostream>
using namespace std;
int main( )
{
    int n,m,total;
    cin>>m;
    n=1;
    total=0;
    while(n<=m)
    {
        total+=(n*n);
        n++;
    }
    cout<<"total="<<total<<endl;
    return 0;
}

或用for循环:

#include <iostream>
using namespace std;
int main( )
{
    int n,m,total;
    cin>>m;
    total=0;
    for(n=1;n<=m;n++)
    {
        total+=(n*n);
    }
    cout<<"total="<<total<<endl;
    return 0;
}

  (2)求1到m间所有奇数的和

#include <iostream>
using namespace std;
int main( )
{
    int n,m,total;
    cin>>m;
    n=1;
    total=0;
    while(n<=m)
    {
        total+=n;
        n+=2;
    }
    cout<<"total="<<total<<endl;
    return 0;
}

或用for循环:

#include <iostream>
using namespace std;
int main( )
{
    int n,m,total;
    cin>>m;
    total=0;
    for(n=1;n<=m;n+=2)
    {
        total+=n;
    }
    cout<<"total="<<total<<endl;
    return 0;
}

  (3)求1到m的倒数和,即

1+12+13+14+...+1m

#include <iostream>
using namespace std;
int main( )
{
    int n,m;
    double total;
    cin>>m;
    n=1;
    total=0;
    while(n<=m)
    {
        total+=(1.0/n); //注意1.0引发的类型转换,非常重要!
        n++;
    }
    cout<<"total="<<total<<endl;
    return 0;
}

或用for循环:

#include <iostream>
using namespace std;
int main( )
{
    int n,m;
    double total;
    cin>>m;
    n=1;
    total=0;
    for(n=1;n<=m;n++)
    {
        total+=(1.0/n); //注意1.0引发的类型转换,非常重要!
    }
    cout<<"total="<<total<<endl;
    return 0;
}

  (4)求值:

112+1314+...+(1)(m+1)×1m

#include <iostream>
using namespace std;
int main( )
{
    int n,m,sign;
    double total;
    cin>>m;
    n=1;
    total=0;
    sign=1; //用sign代表累加项的符号,这是处理一正一负累加的技巧
    while(n<=m)
    {
        total+=(sign*(1.0/n)); 
        n++;
        sign*=-1; //sign变号
    }
    cout<<"total="<<total<<endl;
    return 0;
}

或用for循环:

#include <iostream>
using namespace std;
int main( )
{
    int n,m,sign;
    double total;
    cin>>m;
    n=1;
    sign=1; //用sign代表累加项的符号,这是处理一正一负累加的技巧
    total=0;
    for(n=1; n<=m; n++)
    {
        total+=(sign*(1.0/n)); //注意1.0引发的类型转换,非常重要!
        sign*=-1; //sign变号
    }
    cout<<"total="<<total<<endl;
    return 0;
}

  (5)求m!,即

1×2×3×...×m

#include <iostream>
using namespace std;
int main( )
{
    int n,m;
    long fact; //阶乘值很大,数据类型方面考虑一些
    cin>>m;
    n=1;
    fact=1;
    while(n<=m)
    {
        fact*=n;
        n++;
    }
    cout<<m<<"! = "<<fact<<endl;
    return 0;
}

或用for循环:

#include <iostream>
using namespace std;
int main( )
{
    int n,m;
    long fact; //阶乘值很大,数据类型方面考虑一些
    cin>>m;
    fact=1;
    for(n=1;n<=m;n++)
    {
        fact*=n;
    }
    cout<<m<<"! = "<<fact<<endl;
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

迂者-贺利坚

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

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

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

打赏作者

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

抵扣说明:

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

余额充值