您的位置:首页 > 其它

SDUT 2543 整除

2017-03-19 12:27 204 查看
整除

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

求1到n范围内能被 5 或 6 或 8 整除的数的个数。

Input

多组数据,处理到文件结尾。

每行输入一个n;

Output

输出结果,每个结果占一行。

Example Input

1000

Example Output

400

Hint

1到n被6整除数的个数为n/6(取整)。

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
while(cin >> n)
{
int num = 0;
int sum = 0;
num = n / 5 + n / 6 + n / 8; // 同样的数,记录了两次,所以得减去同样的数
sum = n / 30 + n / 40 + n / 24; // 分别是5 6 , 5 8 , 6 8, 的最小公倍数
num -= sum; //因为sum里面多减了 一些数
num += n / 120;
cout << num << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: