zoukankan      html  css  js  c++  java
  • 20200105-输入若干整数,求其中的奇数之和及偶数之和

    1. #include "stdafx.h"

    2. #include "iostream"

    3. #include "cstring"

    4. #include "string"

    5. using namespace std;

    6. int a,b,c,d;

    7. int main()

    8. {for (c=0;c>=0;c++)  //此处可用替换为while(1)

    9.   {cin>>d;

    10.    if(d==0)

    11.      break;

    12.     if(d%2==0)

    13.       a=a+d;

    14.     if(d%2==1)

    15.       b=b+d;

    16.   }

    17.       cout<<"奇数和: "<<b<<endl;

    18.       cout<<"偶数和:"<<a<<endl;

    19. }

    20.  

    也可以用do while来写:

    #include "stdafx.h"

    #include "iostream"

    #include "cstring"

    #include "string"

    using namespace std;

    int a,b,c,d;

    int main()

    {do

      {

       cin>>d;

       if(d==0)

         break;

        if(d%2==0)

          a=a+d;

        if(d%2==1)

          b=b+d;

    }

    while(d!=0);

          cout<<"奇数和: "<<b<<endl;

       cout<<"偶数和:"<<a<<endl;

    }

    出结果:

    这是一道2018年奥赛初赛题,知识点在于乘方和取余。

  • 相关阅读:
    NPTL 线程同步方式
    mysql事物处理
    DHCP服务器-DNS服务器-Samba服务器
    NTP服务器
    wsgiref 源代码分析
    集群负载均衡LVS
    百万数据查询优化技巧三十则
    Shell 基本运算符
    Shell 数组
    Shell 传递参数
  • 原文地址:https://www.cnblogs.com/whcsrj/p/12928210.html
Copyright © 2011-2022 走看看