|
|
вернуться в форумSimple solution We need to check whether the given number is of the form: k(k+1)/2+k+2 just consider the quadratic in k and check if 9-4*(4-2*N) is a perfect square. #include <stdio.h> #include <math.h> #include <string> using namespace std; char solve( int n ){ if ( n==1 ) return '1'; double w; double N=n; w=int( sqrt( double( 9.0-4.0*(4.0-2.0*N) ) )+0.0000001 ); if ( w*w==9.0-4.0*(4.0-2.0*N) ) return '1'; return '0'; } int main( void ){ int n,k; scanf( "%d", &n ); while ( n-- ){ scanf( "%d", &k ); putc( solve(k), stdout ); putc( ' ', stdout ); } } Re: Simple solution why my program dont work ? var a : array [1..65536] of longint; n,i,k : longint; begin read (n); for i := 1 to n do read (a[i]); for i := 1 to n do begin k := round(sqrt((a[i]-1)*2)); if (k*(k+1)) div 2 = a[i] - 1 then write ('1 ') else write ('0 '); end; end. Re: Why do you add 0.0000001? > We need to check whether the given number is of the form: > k(k+1)/2+k+2 > just consider the quadratic in k and check if > 9-4*(4-2*N) is a perfect square. > > #include <stdio.h> > #include <math.h> > #include <string> > > using namespace std; > > char solve( int n ){ > if ( n==1 ) return '1'; > > double w; > double N=n; > > w=int( sqrt( double( 9.0-4.0*(4.0-2.0*N) ) )+0.0000001 ); > if ( w*w==9.0-4.0*(4.0-2.0*N) ) return '1'; > return '0'; > } > > int main( void ){ > int n,k; > > scanf( "%d", &n ); > while ( n-- ){ > scanf( "%d", &k ); > putc( solve(k), stdout ); > putc( ' ', stdout ); > } > > } > No subject > #include <stdio.h> > #include <math.h> > #include <string> > > using namespace std; > > char solve( int n ){ > if ( n==1 ) return '1'; > > double w; > double N=n; > > w=int( sqrt( double( 9.0-4.0*(4.0-2.0*N) ) )+0.0000001 ); > if ( w*w==9.0-4.0*(4.0-2.0*N) ) return '1'; > return '0'; > } > > int main( void ){ > int n,k; > > scanf( "%d", &n ); > while ( n-- ){ > scanf( "%d", &k ); > putc( solve(k), stdout ); > putc( ' ', stdout ); > } > > } > Re: Simple solution Послано 王平 3 ноя 2004 22:20 /*My problem don't work too.WHY!! */ /*SOS!!!*/ #include<iostream> #include<vector> using namespace std; int main(){ long i,k,ar[500],t; vector<int> num; vector<int>::iterator it; for(i=0;i<500;i++) ar[i]=i*(i+1)/2+1; cin>>k; while(k--) {cin>>t;num.insert(num.end(),t); } it=num.begin(); while(it!=num.end()){ t=*it++;i=500; while(t<ar[--i]); if(t==ar[i]) cout<<'1'<<' '; else cout<<'0'<<' '; } return 0; } |
|
|