-
#include "stdafx.h"
-
#include "iostream"
-
#include "cstring"
-
#include "string"
-
using namespace std;
-
int a,b,c,d;
-
int main()
-
{for (c=0;c>=0;c++) //此处可用替换为while(1)
-
{cin>>d;
-
if(d==0)
-
break;
-
if(d%2==0)
-
a=a+d;
-
if(d%2==1)
-
b=b+d;
-
}
-
cout<<"奇数和: "<<b<<endl;
-
cout<<"偶数和:"<<a<<endl;
-
}
-
也可以用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年奥赛初赛题,知识点在于乘方和取余。