2015年10月2日 星期五

主要解題技巧就是利用greedy 從最大的數(2~9)開始除

所以代表N的因數一定都落於2~9之間

一旦從9除到2(有重複因數,可以重複除), 最後得到結果不為1代表沒有存在的Q

中間有除到的因數 組起來就是答案(先除的放到後面)

基本上只有一位數的解答就是自身
#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>
using namespace std;
int main(){
for(
std::istream_iterator<int> iiter(std::cin), eof;
++iiter != eof;){
int n = *iiter;
if( n < 10 ){
cout << n << endl;
}
else {
std::string result;
for(int i = 10 ; --i > 1 ; )
for( ;
n % i == 0 ;
n /= i,
result = ::to_string(i) + result
);
cout << (n ^ 1 ? -1 : ::stoi(result)) << endl;
}
}
}
view raw pod.cpp hosted with ❤ by GitHub
Next
This is the most recent post.
較舊的文章

0 意見:

張貼留言

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.