題意: 給一個數 n ( 0 <= n <= 10000 ),求一最小 x > 0,滿足 x 的每個位數都是 1 ,且 x 能被 n 整除。
解法: 同餘定理,從 x = 1 開始,每次都把 x mod n ,如果結果不等於零,就把結果乘上10再加 1,一直做到 x mod n 等於零,中間累計加了幾次 1 ,就是答案。
TAG: Math
注意:
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
沒有留言:
張貼留言
任何意見都樂意傾聽