Codeforces 96B-Lucky Numbers (easy)
題目原址
[http://codeforces.com/problemset/problem/96/B]
題意
給定一個數 n ,找出最小的只含 4 和 7 並且它們數量相等的大於等於 n的數。
題解
把它當作字符串處理,構造出符合條件的字符串,用 next_permutation 函數會簡單很多。
(C++靈活得要死,就是有時候慢)
實現
#include <iostream>
#include <algorithm>
using namespace std;
string a[5]={"47","4477","444777","44447777","4444477777"};
int i,j,k;
int main(){
string s;
cin >> s;
for(i=0;i<5;i++){
do
if((a[i]>=s&&a[i].length()==s.length())||a[i].length()>s.length())
goto a;//長度相同且字典序大,或長度大
while(next_permutation(a[i].begin(),a[i].end()));
}
a: cout<<a[i]<<endl;
}