UVa 10127 - Ones

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址
中文網址

Solution

直接硬除會溢位,為了避免使用大數,從 1 開始,判斷 n 是否可整除它,
如不行就將餘數乘10再加上1,然後繼續判斷,如此一來每次都只要處理2位數。

Code

UVa 10127UVa 10127 - Ones
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include<cstdio>

int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
int ans, now;
int remainder = n == 1 ? 0 : 1;//每一次的餘數

for (ans = 1; remainder; ans++)
{
now = remainder * 10 + 1;
remainder = now%n;
}

printf("%d\n", ans);
}

return 0;
}