题意:给出n根火柴,求其能拼成的最小正整数和最大正整数。求最大正整数时,尽量让位数多一点,而数字“1”消耗的火柴数最少,所以尽量尽量用“1”,求最小正整数时,尽量让位数少一点,而数字“8”消耗的火柴数最少,所以尽量用“8”,不过有许多特殊情况需要判断:
#include<bits/stdc++.h> using namespace std; string MIN(int n){ string s = ""; while(n){ if(n == 17){ s = "200"+s; n -= 17; } else if(n == 11){ s ="20" + s; n -= 11; } else if(n == 10){ s = "22" + s; n -= 10; } else if(n >= 9 || n == 7){ s = "8" + s; n -= 7; } else if(n == 8){ s = "0"+s; n -= 6; } else if(n == 6){ // if(s == "")s = "0";//一开始以为6的时候是0.被坑惨了,好好读题! // else s = "6" + s; s = "6" + s; n -= 6; } else if(n == 5){ s = "2"+s; n -= 5; } else if(n == 4){ s = "4" + s; n -= 4; } else if(n == 3){ s = "7" + s; n -= 3; } else if(n == 2){ s = "1" + s; n -= 2; } } return s; } string MAX(int n){ string s = ""; while(n > 3){ s = "1" + s; n -= 2; } if(n == 3)return "7"+s; else if(n == 2)return "1"+s; } int main(){ int n; cin >> n; for(int i = 0; i < n; i++){ int num; cin >> num; cout << MIN(num) << ' ' << MAX(num) << endl; } // for(int i = 2; i < 101; i++){//可以把数字都打出来看看 // cout << i << ' ' << MIN(i) << ' ' << MAX(i) << endl; // } }