HDU 2113 Secret Number

http://acm.hdu.edu.cn/showproblem.php?pid=2113

Problem Description
有一天, KIKI 收到一张奇怪的信, 信上要KIKI 计算出给定数各个位上数字为偶数的和.
eg. 5548
结果为12 , 等于 4 + 8

KIKI 很苦恼. 想请你帮忙解决这个问题.

 
Input
输入数据有多组,每组占一行,只有一个数字,保证数字在INT范围内.
 
Output
对于每组输入数据,输出一行,每两组数据之间有一个空行.
 
Sample Input
415326
3262
 
Sample Output
12
 
10
 
代码:
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
char s[maxn];

int main() {
    int n;
    int cnt = 0;
    while(~scanf("%d", &n)) {
        cnt ++;
        if(cnt != 1) printf("\n");
        itoa(n, s, 10);
        int len = strlen(s);
        int sum = 0;

        for(int i = 0; i < len; i ++) {
            if(s[i] % 2 == 0)
                sum += (s[i] - '0');
        }

        printf("%d\n", sum);
    }

    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/zlrrrr/p/9417378.html