欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-09 21:25:42
...

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

题目详情

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

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

解答

C语言版

#include<stdio.h>
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 求特殊方程的正整数解

C++版

#include<iostream>
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 求特殊方程的正整数解

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");

	}

}

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