p19 problem 10 奇偶判断

    xiaoxiao2025-04-15  27

    p19 problem 10 奇偶判断 问题描述:已知一个正整数n,判断该数是否同时含有奇数数字和偶数数字。如果只有奇数数字则输出“odd number only!",如果只有偶数数字则输出“even number only!",如果都含有则输出”both!" Sample 1: Please input an integer:34 The result is:both! Sample 2: Please input an integer:357 The result is:even number only!

    #include<iostream> using namespace std; int main() { int x; cin >> x; int a[2] = { 0 };//利用数组记录奇数和偶数数字的个数 int m = 1; while (x > 0) { if (x % 2 == 0) a[0]++; else a[1]++; x = x / 10; } if (a[0] > 0 && a[1] > 0) cout << "both!"; else if (a[0] == 0) cout << "odd number only!"; else cout << "even number only!"; return 0; }
    最新回复(0)