24h購物| | PChome| 登入
2012-12-23 22:22:50| 人氣1,163| 回應0 | 上一篇 | 下一篇

[UVA] 1189 - Find The Multiple

推薦 0 收藏 0 轉貼0 訂閱站台

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.

Input 

The input file may contain multiple test cases. Each line contains a value of n ( 1 $ leq$ n $ leq$ 200). A line containing a zero (0) terminates the input.

Output 

For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

Sample Input 

2
6
19
0

Sample Output 

10
100100100100100100
111111111111111111

離散課本中有教過,一定可以由 0 1 構成的十進制數字構成 n 個倍數,
那麼要怎麼找呢?先假是 a 是由 1 構成的十進制數,那麼長度先不管,
逐漸將長度從 1 ... m 討論所有的 a,一定有存在重覆的餘數,此時,
兩數相減不會發生借位問題,就是答案了。


#include <stdio.h>

int main() {
int n;
while(scanf("%d", &n) == 1 && n) {
int C[205] = {}, t = 1%n, i, j;
for(i = 1; ; i++) {
if(C[t]) {
for(j = C[t]; j < i; j++)
putchar('1');
for(j = C[t]-1; j >= 0; j--)
putchar('0');
puts("");
break;
}
C[t] = i;
t = (t*10+1)%n;
}
}
return 0;
}
 

台長: Morris
人氣(1,163) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][parse] 11878 - Homework Checker
此分類上一篇:[UVA][二分搜+KMP] 12467 - Secret Word

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文