NC222897. Jerry
描述
输入描述
The first line contains an integer ,which represents the number of questions you need to solve.
Each of the next lines contains an integer ,which represents the distance Jerry want to move forward.
输出描述
For each query, print an integer to represent the minimum cost of rice.
示例1
输入:
2 8 7
输出:
2 2
说明:
In the first query: 0 -> 4 -> 8.
In the second query: 0 -> 16 -> 7.
C++(clang++ 11.0.1) 解法, 执行用时: 180ms, 内存消耗: 1324K, 提交时间: 2023-07-14 20:42:08
#include<iostream> using namespace std; const int N=1e5+10; int a[N],b[N]; int main() { int n; cin>>n; for(int i=1;i*i<=N;i++) for(int j=i;j*j<=N;j++) { if(i*i+j*j<=N) a[i*i+j*j]=1; a[j*j-i*i]=1; } for(int i=1;i*i<=N;i++) b[i*i]=1; for(int i=1;i<=n;i++) { int t; cin>>t; if(b[t]) cout<<1<<endl; else if(a[t]) cout<<2<<endl; else cout<<3<<endl; } }