列表

详情


NC222897. Jerry

描述

Although Jerry is a mouse, he likes perfect square numbers. If a number can be expressed as the square of an integer, it is called perfect square numbers. Jerry can use magic to move forward or backward a distance of any perfect square numbers, which costs a grain of rice each time. And the position after each use of magic can not be behind the initial position or more than  distance in front of the initial position. Next, there are  questions, each time asking how much rice at least it takes for Jerry to move forward .Note that each question is an independent one.

输入描述

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;
	}
}

上一题