我是個對電腦科學有興趣的學生,我會貼上我的學習歷程及生活心情,也請大大們多多指教。 :)

2015年4月4日 星期六

[UVa] 10127 - Ones

題目網址: http://goo.gl/6Ym1nG

題意: 給一個數 n ( 0 <= n <= 10000 ),求一最小 x > 0,滿足 x 的每個位數都是 1 ,且 x 能被 n 整除。


解法: 同餘定理,從 x = 1 開始,每次都把 x mod n ,如果結果不等於零,就把結果乘上10再加 1,一直做到 x mod n 等於零,中間累計加了幾次 1 ,就是答案。

TAG: Math

注意:

程式碼:
/**
* Tittle: 10127 - Ones
* Author: Cheng-Shih, Wong
* Date: 2015/04/04
*/
// include files
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
// definitions
#define FOR(i,a,b) for( int i=(a),_n=(b); i<=_n; ++i )
#define clr(x,v) memset( x, v, sizeof(x) )
// declarations
int n;
// functions
// main function
int main( void )
{
int start;
int cnt;
// input
while( scanf( "%d", &n )==1 ) {
// init
cnt = start = 1;
// solve
while( start%n ) {
start = (start%n)*10+1;
++cnt;
}
// output
printf( "%d\n", cnt );
}
return 0;
}

沒有留言:

張貼留言

任何意見都樂意傾聽