本文主要是介绍四方定理代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <stdio.h> #include <math.h> int mode_1(int n)//开方表达式 {if ((int)sqrt(n) * (int)sqrt(n) == n){printf("%d * %d = %d \n", (int)sqrt(n), (int)sqrt(n), n);return 1;}else{return 0;} }int mode_2(int n)//求方二 {int x, y;for (x = 1; x < sqrt(n); x++)for (y = x; y < sqrt(n); y++){if (x * x + y * y == n){printf("%d^2 + %d^2 = %d\n", x, y, n);return 1;}}return 0; }int mode_3(int n)//求方三 {int x, y, z;for (x = 1; x < sqrt(n); x++)for (y = x; y < sqrt(n); y++)for (z = y; z < sqrt(n); z++){if (x * x + y * y + z * z == n){printf("%d^2 + %d^2 + %d^2 = %d\n", x, y, z, n);return 1;}}return 0; }int mode_4(int n)//求方4 {int x, y, z, t;for (x = 1; x < sqrt(n); x++)for (y = x; y < sqrt(n); y++)for (z = y; z < sqrt(n); z++)for (t = z; t < sqrt(n); t++)if (x * x + y * y + z * z + t * t == n){printf("%d^2 + %d^2 + %d^2 + %d^2 = %d\n", x, y, z, t,n);return 1;}return 0; }void proveFourSquares(int n) {if (mode_1(n))printf("it has verified one squares\n");if (mode_2(n))printf("it has verified two squares\n");if (mode_3(n))printf("it has verified three squares\n");if (mode_4(n))printf("it has verified four squares\n"); }int main(int argc, char *argv[]) {int n;printf("please input a natural number\n");scanf("%d", &n);printf("--------------------------\n");proveFourSquares(n);return 0; }
这篇关于四方定理代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!