《ACM程序设计》书中题目U(美丽数字)

来源:互联网 发布:js时间戳与php时间戳 编辑:程序博客网 时间:2024/06/07 09:18

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整除的正数;

思路如下:

1.建立美丽数字数据库;

2.输入n(第几个),查找;

#include<bits/stdc++.h>using namespace std;int main(){        vector<int> a;        int i,n,k;        for(i=1,k=0;k<=100000;i++)        {                if(i%3==0||i%5==0)                {                        a.push_back(i);                        k++;                }        }        while(cin>>n)                cout<<a[n-1]<<endl;}
题目毫无难度,但是有一个陷阱,要求是10 0000个以内的美丽数字,而不是10 0000以内的美丽数字,这一点没有注意的话,会导致越界的错误。

0 0
原创粉丝点击