請你寫一個程式,輸入正整數 n 求 1 < x ≤ n﹐滿足 1 / x 是有限小數的 x 值共有多少個?
輸入說明 :
有m(1≤m≤6)組測試資料且1≤n≤100000000,每組測試資料均為一行
接下來共有m行,每行有1個整數。
輸出說明 :
對於每一組測試資料,輸出一行一個數字,代表著這個個數。
範例輸入 :
2
3
5
範例輸出 :
1
3
提示 :
如果有包含2和5以外的因數,就是無限小數
程式碼 :
#include<stdio.h> int main() { int i,j,m,n,two,five,two_p,five_p,record_p,total; int record[500001]; total = 0; record[0] = 1; record_p = 1; two_p = 0; five_p = 0; two = record[two_p] * 2; five = record[five_p] * 5; while(two <= 100000000 || five <= 100000000) { if(two == five) { record[record_p] = two; record_p++; if(two < 100000000) { two_p++; two = record[two_p] * 2; } else two = 100000001; if(five < 100000000) { five_p++; five = record[five_p] * 5; } else five = 100000001; } else if(two < five) { record[record_p] = two; record_p++; if(two < 100000000) { two_p++; two = record[two_p] * 2; } else two = 100000001; } else { record[record_p] = five; record_p++; if(five < 100000000) { five_p++; five = record[five_p] * 5; } else five = 100000001; } total++; } record[record_p] = 100000001; total++; scanf("%d",&m); for(i=0; i<m; i++) { scanf("%d",&n); for(j=1; j<total; j++) if(n < record[j]) break; printf("%d\n",j-1); } return 0; }
http://zerojudge.tw/ShowProblem?problemid=a241
沒有留言:
張貼留言