PAT基础编程题目-7-21 求特殊方程的正整数解

PAT基础编程题目-7-21 求特殊方程的正整数解

题目详情

PAT基础编程题目-7-21 求特殊方程的正整数解_第1张图片

题目地址:https://pintia.cn/problem-sets/14/problems/801

解答

C语言版

#include
int main() {
	int N, flag=1;
	scanf("%d", &N);
	for (int x = 1; x <= 100; x++) {  // 正整数解,x,y都要大于0
		for (int y = 1; y <= 100; y++) {
			if (x <= y && x * x + y * y - N == 0) {
				printf("%d %d\n", x, y);
				flag = 0;
			}
		}
	}
	if (flag)
		printf("No Solution");
	return 0;
}

PAT基础编程题目-7-21 求特殊方程的正整数解_第2张图片

C++版

#include
using namespace std;
int main() {
	int N, flag = 1;
	cin >> N;
	for (int x = 1; x <= 100; x++) {  // 正整数解,x,y都要大于0
		for (int y = 1; y <= 100; y++) {
			if (x <= y && x * x + y * y - N == 0) {
				cout << x << " " << y << endl;
				flag = 0;
			}
		}
	}
	if (flag)
		cout << "No Solution";
	return 0;
}

PAT基础编程题目-7-21 求特殊方程的正整数解_第3张图片

Java版

import java.util.Scanner;
public class Main{

	public static void main(String[] args) {
		int N = 0;
		boolean flag = true;
		Scanner scanner = new Scanner(System.in);
		if (scanner.hasNext()) {
			N = scanner.nextInt();
		}
		scanner.close();
		for (int x = 1; x <= 100; x++) {  // 正整数解,x,y都要大于0
			for (int y = 1; y <= 100; y++) {
				if (x <= y && x * x + y * y - N == 0) {
					System.out.println(x+" "+y);
					flag = false;
				}
			}
		}
		if (flag)
			System.out.println("No Solution");

	}

}

创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!

你可能感兴趣的:(PAT基础编程题目-7-21 求特殊方程的正整数解)