简答题
偏难
#7451
求满足条件的3位数(2024.3一级) 编写程序,按从小到大的顺序寻找同时符合条件1和2的所有3位数,条件为: 1.该数为完全平方数 2.该数至少有2位数字相同…
求满足条件的3位数(2024.3一级)
编写程序,按从小到大的顺序寻找同时符合条件1和2的所有3位数,条件为:
1.该数为完全平方数
2.该数至少有2位数字相同
例如,100同时满足上面两个条件。
输入
输入一个数n,n的大小不超过实际满足条件的3位数的个数。
输出
输出为第n个满足条件的3位数(升序)
样例输入
1
样例输出
100
答案与解析
正确答案:<pre class="brush:cpp;toolbar:false">// 参考答案1
#include <bits/stdc++.h>
using namespace std;
int a[1005];
int s = 1;
int main() {
int x;
cin >> x;
if (x == 1)
cout << 100;
if (x == 2)
cout << 121;
if (x == 3)
cout << 144;
if (x == 4)
cout << 225;
if (x == 5)
cout << 400;
if (x == 6)
cout << 441;
if (x == 7)
cout << 484;
if (x == 8)
cout << 676;
if (x == 9)
cout << 900;
}</pre><pre class="brush:cpp;toolbar:false">// 参考答案2
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,a;
cin>>n;
a=0;
for(int i=100,x;i<=999;i++){
x=sqrt(i);
if(x*x==i&&(i%10==i/10%10 || i/10%10==i/100 ||i%10==i/100)){
a++;
if(a==n){
cout<<i;return 0;
}
}
}
return 0;
}</pre>
这道题暂未补解析。