51Nod - 1035 最长的循环节
输入n(10 <= n <= 1000)
输出<=n的数中倒数循环节长度最长的那个数
10
7
题解:
使用直接方法。用 map 来辅助计算循环节。
#include <iostream> 
#include <map>
using namespace std;  
const int MAXN = 1000 + 5; 
int cnt[MAXN]; 
int Find(int n){
	int num = 1, idx = 1, ans;  
	map<int, int> mp; 
	while(1){
		if(num == 0){
			return 0; 
		}
		while(num < n){
			num = num * 10; 
		}
		if(mp.find(num) == mp.end()){
			mp[ num ] = idx; 
			idx++; 
		}else{
			ans = idx - mp[ num ]; 
			break; 
		}
		num = num % n;
	}
	return ans; 
}
void init(){
	for(int i=2; i<=1000; ++i){
		cnt[i] = Find(i); 
	}
}
int main(){
	int n, ans, ans_tmp; 
	init(); 
	while(scanf("%d", &n) != EOF){
		ans = 0, ans_tmp = 0; 
		for(int i=1; i<=n; ++i){
			if(ans_tmp < cnt[i]){
				ans_tmp = cnt[i]; 
				ans = i;  
			}
		}
		printf("%d\n", ans ); 
	}
	return 0; 
}
原文:http://www.cnblogs.com/zhang-yd/p/6818686.html