利用餘數的特性來解此題。
Ex.
- 1%3 = 1 (先做最高位去餘數)
- 11%3 = 2 (再放入一位的1)
- 21%3 = 0 (再放入一位的1)
得到答案是111是3的倍數,所以digits是3。
C++(0.008)
/*******************************************************/ /* UVa 10127 Ones */ /* Author: LanyiKnight [at] knightzone.studio */ /* Version: 2012/04/13 */ /*******************************************************/ #include<iostream> #include<cstdio> using namespace std; int main(){ int n; int ones, count; while( scanf( "%d", &n ) != EOF ){ ones = 1%n; count = 1; while( ones ){ ones *= 10; ones++; ones %= n; count++; } printf( "%d\n", count ); } return 0; }
回應