在C中打印Pythagorean Triple的代表 [英] Print a representation of the Pythagorean Triple in C
问题描述
我试图创建一个程序,用于打印在C中找到的pythagorean三元组的映射数据。到目前为止,我已经编写了程序以便能够找到三元组。
#include< stdio.h>
#include< math.h>
int main(int argc,char * argv []){
int a,b,c;
int a2,b2,c2;
int limit = 60; (a = 1; a< = limit; a ++){
a2 = a * a;
; (b = 1; b <=极限; b ++){
b2 = b * b; (c = 0; c <=((int)sqrt(a2 + b2)); c ++){
c2 = c * c;
if(a printf(triple:%d%d%d \ n,a,b, C);
}
}
}
}
}
预期输出格式为:
123456789012345678901234567890123456789012345678901234567890
1 \
2 \
3 \
4 * \
5 \
6 \
7 \
8 * \
9 \
0 \
1 \
2 * * \
我正在尝试写一个这样的循环,但无法想象如何以这种方式进行打印。任何建议?
更新:我设法打印x和y轴(x = a和y = b)。
for(int x = 0; x printf(%d,x);
}
printf(\\\
);
for(int y = 0; y
printf(%d \ n,y) ;
更新:修改代码,输出是打印,但打印空间有问题。尝试手动为\和*添加空格,但这只是扭曲了整个图像。
#include< stdio.h>
#include< math.h>
#include< stdbool.h>
bool is_perfect_square(int num);
int main(int argc,char * argv []){
int a,b,c;
int a2,b2,c2;
int limit = 60;
bool flag = false; (a = 1; a< = limit; a ++){
a2 = a * a;
; (b = 1; b <=极限; b ++){
b2 = b * b; (c = 0; c <=((int)sqrt(a2 + b2)); c ++){
c2 = c * c;
if(a // printf(triple:%d%d%d \\\
,a, b,c); (int x = 0; x for(int)
$ b y = 0; y if(x == 0){
printf(%d,((y + 1)%10));
} else if(y == 0){
printf(%d,(x%10));
} else if(x == y){
printf(\\);
} else if(is_perfect_square((x * x)+(y * y))){
printf(*);
}
}
printf(\\\
);
bool is_perfect_square(int num){
int root =(int)(sqrt(num));
if(num == root * root){
return true;
} else {
return false;
}
}
仍在研究可能的解决方案。
提示:
i,j;
for i 0 .. limit {
for j 0 .. limit {
/ *在这里实现下面的算法!! * /
}
printf(\ n)
}
要在循环中使用的算法:
- if
i == 0
通过打印打印x轴值(j + 1)%10
[见注释] - else if
j == 0
通过打印打印y轴值i%10
- else if
i == j
print'\'
- else if is_perfect_square(
(i * i)+(j * j)
)返回1,print'*'$ c $
- else $ print b
$ bis_perfect_square函数的规范:函数返回1如果输入t是一个完美的正方形,否则为0。例如:
-
is_perfect_square(25)
shouldreturn 1
-
is_perfect_square(7) should
return 0
-
is_perfect_square(49)
shouldreturn 1
注意:
i == 0
case should printj%10
以0开始输出以表示原点。但是提供的输出从1开始。因此使用(j + 1)%10
您可能需要来处理一些角落案例,一旦这个算法在代码中实现,应该是直截了当的。
I am trying to create a program that prints the mapping data for found pythagorean triples in C. So far, I have coded the program to be able to find the triples.
#include <stdio.h> #include <math.h> int main (int argc, char * argv[]) { int a, b, c; int a2, b2, c2; int limit = 60; for (a = 1; a <= limit; a++) { a2 = a * a; for (b = 1; b <= limit; b++) { b2 = b * b; for (c = 0; c <= ((int)sqrt(a2+b2)); c++) { c2 = c * c; if (a < b && (c2 == (a2 + b2))) { printf("triple: %d %d %d\n", a, b, c); } } } } }
The intended output is expected in the format:
123456789012345678901234567890123456789012345678901234567890 1\ 2 \ 3 \ 4 *\ 5 \ 6 \ 7 \ 8 * \ 9 \ 0 \ 1 \ 2 * * \
I am trying to write a loop that does this, but cannot think of how to print in this way. Any suggestions?
UPDATE: I managed to print the x and the y axis (x = a and y = b). The values are correct, now the mapping part is left.
for (int x = 0; x < a; x++) { // x-axis = a printf("%d ", x); } printf("\n"); for (int y = 0; y < b; y++) { // y-axis = b printf("%d\n", y); }
UPDATE: Modified the code, the output is printing, but having problems printing spaces. tried manually adding spaces to the " \ " and " * " but that just distorts the whole image.
#include <stdio.h> #include <math.h> #include <stdbool.h> bool is_perfect_square(int num); int main (int argc, char * argv[]) { int a, b, c; int a2, b2, c2; int limit = 60; bool flag = false; for (a = 1; a <= limit; a++) { a2 = a * a; for (b = 1; b <= limit; b++) { b2 = b * b; for (c = 0; c <= ((int)sqrt(a2+b2)); c++) { c2 = c * c; if (a < b && (c2 == (a2 + b2))) { // printf("triple: %d %d %d\n", a, b, c); } } } } for (int x = 0; x < a; x++) { for (int y = 0; y < b; y++) { if (x == 0) { printf("%d ", ((y+1)% 10)); } else if (y == 0) { printf("%d ", (x % 10)); } else if (x == y) { printf("\\"); } else if (is_perfect_square((x*x) + (y*y))) { printf("*"); } } printf("\n"); } } bool is_perfect_square (int num) { int root = (int)(sqrt(num)); if (num == root * root) { return true; } else { return false; } }
Still working on possible solution.
解决方案Hint :
Have a nested loop with index i,j;
for i 0.. limit { for j 0 .. limit { /*Implement the below algorithm here!!*/ } printf("\n") }
Algorithm to be used inside the loop:
- if
i == 0
print x axis values by printing(j+1)% 10
[see note at end] - else if
j == 0
print y axis values by printingi % 10
- else if
i == j
print'\'
- else if is_perfect_square(
(i*i) + (j*j)
) returns 1, print'*'
- else print a space.
Specifications for is_perfect_square function: A function that returns 1 if the input is a perfect square and 0 otherwise. For example:
is_perfect_square(25)
shouldreturn 1
is_perfect_square(7)
shouldreturn 0
is_perfect_square(49)
shouldreturn 1
Note:
i == 0
case should printj%10
to start the output with 0 to represent origin. But the output provided in question starts with 1. Hence using(j+1)%10
You might need to handle some corner cases, which should be straight forward once this algorithm is implemented in code.
这篇关于在C中打印Pythagorean Triple的代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!