当前位置: 代码迷 >> 综合 >> That the positive number which is dividable by 3 or 5
  详细解决方案

That the positive number which is dividable by 3 or 5

热度:71   发布时间:2024-01-12 16:08:50.0

题目:

Description
Mike is very lucky, as he has two beautiful numbers, 3 and 5. But he is so greedy that he wants infinite beautiful numbers. So he declares that any positive number which is dividable by 3 or 5 is beautiful number. Given you an integer N (1 <= N <= 100000), could you please tell mike the Nth beautiful number?
Input
The input consists of one or more test cases. For each test case, there is a single line containing an integer N.
Output
For each test case in the input, output the result on a line by itself.
Sample Input
1
2
3
4


Sample Output
3
5
6
9

解题思路:

采用预处理的方法,将可以被3和5整除的数按顺序放入数组中,下标便是顺序,对于输入的每个test case 输出对应的数组中的元素

细节处理:

预处理时,要处理大于100000个元素,

代码:

#include <iostream>
#include<cmath>
#include<vector>
using namespace std;
int main()
{
    int x[250000];
    int i,num=1;
    for(i=1;i<500000;i++)
    {
        if(i%3==0||i%5==0) { x[num]=i; num++;}
        if(num==199999) break;
    }
    int c;
    while(cin>>c)
    {
        cout<<x[c]<<endl;
    }
    return 0;
}

  相关解决方案