[SPOJ-CANTON]
找规律并输出…
#include <cstdio> #include <algorithm> using std::lower_bound; #define MAXN 4472 int accu[MAXN]; void init() { int i; accu[0]=1; for(i=1;accu[i-1]<10000000;i++) accu[i]=accu[i-1]+(i+1); } void solver() { int x; int *p; scanf("%d",&x); p=lower_bound(accu,accu+MAXN,x); //printf("%d in level %d with d=%d\n",x,p-accu,x-accu[p-accu-1]); if((p-accu)%2==0) { printf("TERM %d IS %d/%d\n",x,(2+p-accu)-(x-accu[p-accu-1]),x-accu[p-accu-1]); } else { printf("TERM %d IS %d/%d\n",x,x-accu[p-accu-1],(2+p-accu)-(x-accu[p-accu-1])); } } int main(void) { int N; scanf("%d",&N); init(); while(N--) { solver(); } return 0; }