c++循环语句题目告急:

来源:百度知道 编辑:UC知道 时间:2024/05/31 03:58:19
输入n个正整数,输出其中偶数的和,以及奇数的个数
请各位帮帮忙吧!谢谢了
不好意思,你们用的东西我还没有学到呢,可不可以用c的库函数编呢?

这个问题还是比较简单的,呵呵。
下面的代码是在VC2005中执行成功了的,在VC6中也可以。

// test1.cpp : function:输入n个正整数,输出其中偶数的和,以及奇数的个数。
//by Maryland

#include "stdafx.h"
#include<iostream>
#include<process.h>
//#include<cmaths.h>
using namespace std;

void main()
{
int n(0),m(0),sum(0);
cout<<"请输入整数个数n:"<<endl;
cin>>n;
int data=0;
cout<<"请依次输入n个正整数:"<<endl;
for(int i=0;i<n;i++)
{
cin>>data;
if(0==data%2)
sum+=data;
else
m++;
}

cout<<"偶数和sum="<<sum<<endl;
cout<<"奇数个数m="<<m<<endl;
system("pause");
}

#include <iostream>
using namespace std;
static int num=0;
void main()
{
int n;
int sum=0;
cout<<"Enter the int"<<endl;