下面图老师小编跟大家分享解析四方定理的应用,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~
【 tulaoshi.com - 编程语言 】
我们可以通过计算机验证其在有限范围的正确性。
代码如下:
#include stdio.h
#include math.h
int f(int n, int a[], int idx)
{
 if(n==0) return 1;  
 if(idx==4)  return 0;  //超出数组范围 出错
 for(int i=(int)sqrt(n); i=1; i--)
 {
  a[idx] = i;
  if(f(n-i*i, a, idx+1) == 1)  return 1;  // 递归调用
 }
 return 0;
}
int main(int argc, char* argv[])
{
 for(;;)
 {
  int number;
  printf("输入整数(1~10亿):");
  scanf("%d",&number);
  int a[] = {0,0,0,0};
int r = f(number, a, 0);
  printf("%d: %d %d %d %dn", r, a[0], a[1], a[2], a[3]);
 }
 return 0;
}
来源:http://www.tulaoshi.com/n/20160219/1590228.html