51Nod - 1035 最长的循环节
生活随笔
收集整理的這篇文章主要介紹了
51Nod - 1035 最长的循环节
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
51Nod -?1035?最長的循環節
正整數k的倒數1/k,寫為10進制的小數如果為無限循環小數,則存在一個循環節,求<=n的數中,倒數循環節長度最長的那個數,假如存在多個最優的答案,輸出所有答案中最大的那個數。 1/6= 0.1(6) 循環節長度為1 1/7= 0.(142857) 循環節長度為6 1/9= 0.(1) ?循環節長度為1 Input 輸入n(10?<=?n?<=?1000) Output 輸出<=n的數中倒數循環節長度最長的那個數 Input示例 10 Output示例 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; }
?
轉載于:https://www.cnblogs.com/zhang-yd/p/6818686.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的51Nod - 1035 最长的循环节的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 巴登的故事
- 下一篇: Linux下使用Eclipse搭建ARM