1103. Destiny Score(20 points)

The so-called fate score refers to such a pair of positive integers a and b, in which the difference between the cubes of a and its younger brother a−1 is exactly the square of another integer c, and c is exactly the sum of the squares of b and its younger brother b−1 . For example, 83−73=169=132, and 13=32+22, so 8 and 3 are a pair of fate scores.

Given the interval [m,n] in which a lies, is there a fate score?

Input format:

Enter the two endpoints of the given interval 0<m<n≤25000, separated by spaces.

Output format:

According to the order of a from small to large, each line outputs a pair of fate scores, and the numbers are separated by spaces. Output if there is no solution No Solution.

Input sample 1:

8 200

Output sample 1:

8 3
105 10

Input sample 2:

9 100

Output sample 2:

No Solution

This topic buried a big hole, "where the cube difference between a and its younger brother a−1 is exactly the square of another integer  c", which implies a condition that a and c are not equal. I looked at the code of other bloggers, and many of them were hit by mistake.

The last test point is 1 1, output No Solution 

#include<cstdio>
#include<set>
#include<map>
#include<cmath> 
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int a,b;
	int flag=1;
	cin>>a>>b;
	for(int i=a;i<=b;i++){
		int result = pow(i,3)-pow(i-1,3);
		int c = sqrt(result);
		if(c*c == result){
			//这里是想优化查找 因为 c = b^2 + (b-1)^2 ,所以b肯定小于等于c的开根号结果  题目又说了b是正整数 所以b >= 1
			//如果不优化 直接在循环中 写 j<=c即可  写 j<c  可以通过最后一个测试点,但是并不合理,属于误打误撞 
			int n = sqrt(c);
			//从 1 到 n 查找 j 和 j-1也可以 
			for(int j=0;j<=n;j++){
				if(pow(j,2)+pow(j+1,2) == c){
					//针对题干隐藏条件 单独加一个判断 
					if(i!=j+1){
						flag = 0;
						cout<<i<<" "<<j+1<<endl; 
					}
				}
			}
		}
	}
	if(flag)
		cout<<"No Solution"<<endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_16382227/article/details/124044214