acm问题解决1.7

1.7 [description of the problem]


The fourth square sum theorem is also known as the Lagrange theorem: every positive integer can be expressed as the sum of squares of at most 4 positive integers. If 0 is included, it can be represented as the sum of squares of 4 numbers. For example, 5 = 0^2 + 0^2 + 1^2 + 2^2 7 = 1^2 + 1^2 + 1^2 + 2^2 (^ symbol means square meaning) for a given positive integer, there may be a variety of square sum representation. Ask you to sort 4 numbers: 0 < = a < = < = < = C < = D, and all possible representations are ascending in a, B, C, D as joint primary keys, and finally output the first representation

[input] a positive integer N (N<5000000)

[output] 4 non negative integers, sorted from small to large, separated by spaces in the middle.

[input case 1] 5

[output example 1] 0012

[input case 2] 773535

[output example 2] 11267838


solved:

#include<stdio.h>

int main(){

int n;

int a=0;

int b=0;

int c=0;

int d=0;

scanf("%d",&n);

for(a;a<=2237;a++){

if(a*a>n) continue;

for(b=a; b<=2237;b++){

if(a*a+b*b>n) break;

for(c=b; c<=2237;c++){

if(a*a+b*b+c*c>n) break;

for(d=c; d<=2237;d++){

if(a*a+b*b+c*c+d*d>n) break;

if(a*a+b*b+c*c+d*d==n) {

printf("%d,%d,%d,%d",a,b,c,d);

return 0;}

}

}

}

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ifHappyEveryDay@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值