Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

递归+备忘录,0ms

Posted by taesimple at 2011-08-09 00:20:44 on Problem 1207
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator