|
|
вернуться в форумeasy search var i,j,k,n:longint; begin read(n); for i:=1 to 250 do if sqr(i)=n then begin write(1); exit; end; for i:=1 to 250 do for j:=1 to 250 do if sqr(i)+sqr(j)=n then begin write(2); exit; end; for i:=1 to 250 do for j:=1 to 250 do for k:=1 to 250 do if sqr(i)+sqr(j)+sqr(k)=n then begin write(3); exit; end; write(4); end. Re: easy search Послано evjava 23 окт 2009 20:20 If N was bigger, you would have time limit. Re: easy search I don't think so. My code got AC. #include<stdio.h> int i,j,k; long int n; int main() { scanf("%d",&n);
for(i=1;(i*i)<=n;i++) if((i*i)==n) { printf("%d",1); return 0; } for(i=1;i*i<=n;i++) for(j=1;j*j<=n;j++) if((i*i+j*j)==n) { printf("%d",2); return 0; }
for(i=1;i*i<=n;i++) for(j=1;j*j<=n;j++) for(k=1;k*k<=n;k++) if((i*i+j*j+k*k)==n) { printf("%d",3);
return 0; }
printf("%d",4); return 0;
} But this solution is bad. Re: easy search I think it's DP in this problem O(n*sqrt(n)); Re: easy search this is the most epic answer I've read....hhhh :) u are awesome thanks dud and good luck I don't think so. My code got AC for(i=1;(i*i)<=n;i++) if((i*i)==n) { printf("%d",1); return 0; } you can just write if (sqrt(n) * sqrt(n) == n) |
|
|