菜鸟问求任意两个数之间的偶数和?
求任意两数之间的偶数和,要考虑那两个数的奇偶,我搞不清楚了。。。。谁能给我讲解一下啊,有没有大致的轮廓啊
#include <stdio.h> #define MAX_NUM 4294967296 //2的32次幂 //该整数是否为偶数 #define ISEVENNUM(NUMBER) ((0 == (NUMBER) % 2) && ((NUMBER) > 0))?true:false int main() { unsigned int nBeginNum = 0; //起始值 unsigned int nEndNum = 0; //结束值 unsigned int nTemp = 0; unsigned int i = 0; //循环变量 unsigned int nSum = 0; //记录所有偶数之和 printf("Please enter starting value and end value:\n"); scanf("%d,%d",&nBeginNum,&nEndNum); //若两者相等,则无需计算了两数间的偶数和了,直接返回(这里其实需要澄清是否需要包含边界值。注:这里不考虑边界值)。 if (nBeginNum == nEndNum) { printf("起始值和结束值相等,其和等于0.\n"); return nSum; } //先获取起始值和结束值,保证nBeginNum为起始值,nEndNum为结束值 if (nBeginNum > nEndNum) { nTemp = nBeginNum; nBeginNum = nEndNum; nEndNum = nTemp; } //开始累加偶数之和(这里不包括边界值) for (i = nBeginNum+1; i < nEndNum; i++) { //若为偶数,则将其和累加 if (ISEVENNUM(i)) { nSum += i; } } printf("偶数总和=%d\n",nSum); return nSum; }