3 Star 61 Fork 5

programmercarl / kamacoder-solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0009.奇怪的信.md 2.25 KB
一键复制 编辑 原始数据 按行查看 历史

9. 奇怪的信

题目链接

C++

#include<iostream>
using namespace std;
int main() {
    int n, a;
    while (cin >> n) {
        int result = 0;
        while (n != 0) {
            a = (n % 10);
            n = n / 10;
            if (a % 2 == 0) result += a;
        }
        cout << result << endl;
        cout << endl;
    }
}

Java

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            int res = 0;
            while (n > 0) {
                int tmp = n % 10;
                if (tmp % 2 == 0) {
                    res += tmp;
                }
                n /= 10;
            }
            System.out.println(res);
            System.out.println();
        }
    }
}

python

while 1:
    try:
        n=input()
        s=0
        for i in n:
            if int(i)%2 == 0:
                s += int(i)
        print(s)
        print()
    except:
        break

Go

package main

import (
	"fmt"
)

func main() {
	var n, a, result int
	for {
		_, err := fmt.Scanf("%d", &n)
		if err != nil || n == 0 {
			break
		}
		result = 0
		for n != 0 {
			a = n % 10
			n = n / 10
			if a%2 == 0 {
				result += a
			}
		}
		fmt.Println(result)
		fmt.Println()
	}
}

Js

const readline = require("readline");
const r1 = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const iter = r1[Symbol.asyncIterator]();

const read_line = async () => (await iter.next()).value;

let line = null;

(async function () {
  while ((line = await read_line())) {
    const arr = line.split("").map((item) => Number(item));
    let sum = 0;
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] % 2 === 0) {
        sum += arr[i];
      }
    }
    console.log(sum, "\n");
  }
})();

C

#include <stdio.h>

int main() {
    int n, a;
    while (scanf("%d", &n) == 1) {
        int result = 0;
        while (n != 0) {
            a = n % 10;
            n = n / 10;
            if (a % 2 == 0) result += a;
        }
        printf("%d\n\n", result);
    }
    return 0;
}
1
https://gitee.com/programmercarl/kamacoder-solutions.git
git@gitee.com:programmercarl/kamacoder-solutions.git
programmercarl
kamacoder-solutions
kamacoder-solutions
main

搜索帮助