PAT基础编程题目-7-21 求特殊方程的正整数解
程序员文章站
2022-06-09 21:25:42
...
PAT基础编程题目-7-21 求特殊方程的正整数解
题目详情
解答
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;
}
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;
}
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");
}
}
创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!