一个正整数如果能表示成两个正整数的平方差,则称这个数为一个“智慧数”,比如 16 就等于 5的平方减去 3 的平方,所以 16 就是一个智慧数,从 1 开始的自然数列中,将“智慧数”从小到大编号为 1,2,3,…,n。现输入一个正整数 n,输出第 n 个“智慧数”。
输入
输入仅包含一个正整数 n(1≤n≤100)。
输出
输出仅包含一个正整数,表示编号为 n 的“智慧数”。
输入样例
3
输出样例
7
解题思路
创建一个函数列举出所有智慧数放到set容器里,然后用迭代器输出第n个
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
#define INT_MAX 0x7fffffff
#define INT_MIN 0x80000000
using namespace std;
set<int>a;
int n;
void f()
{
for(int i = 2; ; i++){
for(int j = i-1; j >= 1; j--){
a.insert(i*i-j*j);
if(a.size() == 10000){
return;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
f();
cin >> n;
int x = 0;
for(set<int>::iterator it = a.begin(); it!= a.end(); it++){
x++;
if(x == n){
cout << *it << endl;
}
}
return 0;
}