Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
递归+备忘录,0ms#include <stdio.h> #define SIZE 10020 //SIZE开到10010都WA,10020就AC…… int cycle[SIZE] = {0}; int get_cycle(int n){ if(n >= SIZE) return (n%2 == 1) ? get_cycle((3*n+1)/2)+2 : get_cycle(n/2)+1; //n超出SIZE范围时直接递归,避免越界 else if(cycle[n] == 0) cycle[n] = (n%2 == 1) ? get_cycle((3*n+1)/2)+2 : get_cycle(n/2)+1; return cycle[n]; } int main(){ int i, j, ii, jj; cycle[1] = 1; for(int k = 2; k < SIZE; k++) get_cycle(k); while(EOF != scanf("%d%d", &ii, &jj)){ i = (ii < jj) ? ii : jj; j = ii+jj-i; int max = cycle[i]; for(int k = i+1; k <= j; k++) max = (cycle[k] > max) ? cycle[k] : max; printf("%d %d %d\n", ii, jj, max); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator