qfedu-basic-level/day8/homework/h16.cpp

35 lines
776 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 输入一个整数 n然后输入 n 个整数,输出其中所有能被 3 或 5 但不能被同时整除的数的个数。
#include <iostream>
using namespace std;
int main()
{
int n;
int *nums = new int[n];
int i = 0;
int cnt = 0; // 计数器
cout << "请输入整数 n: ";
cin >> n;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
i++;
}
while (--n >= 0)
{
if (nums[n] % 3 == 0 && nums[n] % 5 == 0)
continue;
else if (nums[n] % 3 == 0 || nums[n] % 5 == 0)
cnt++;
else
continue;
}
cout << "输入的整数中所有能被 3 或 5 但不能被同时整除的数的个数: " << cnt << endl;
return 0;
}