Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5055
Question:
Give you n digits, each digit is 0 ~ Between 9. You make up n digits into an integer.
Requirements:
1. It must be an odd number.
2. There is no 0 before the integer
3. Find the Largest Integer
If the values 1, 2, and 3 are met, the number is output. If the value is not met, the value-1 is output.
Example
3
1 0 0
The odd number is 001, And there is 0 in front of this number. The value-1 should be output.
Solution:
Sort n numbers in ascending order.
Find the smallest one, locate an odd number, and place it on the leftmost.
Then determine whether the number meets the requirements. If the requirements are met, the output is displayed. Output-1 if not met
AC code:
1 # include <cstdio> 2 # include <algorithm> 3 using namespace STD; 4 5 # define maxn 110 6 7 int main () {8 int I, j, n, digit [maxn]; 9 While (~ Scanf ("% d", & N) {10 for (I = 0; I <n; ++ I) {11 scanf ("% d ", & digit [I]); 12} 13 14 sort (digit, digit + n); // ascending order of 15 16 for (I = 0; I <n; ++ I) {// start from the left and find an odd number 17 if (digit [I] & 1) {// If (digit [I] % 2 = 1) 18 int T = digit [I]; 19 for (j = I; j> 0; -- J) {// shift all the numbers to the right one digit 20 digit [J] = digit [J-1]; 21} 22 digit [J] = T; // place the odd number in the last 23 break; // if it is found, it jumps out of the loop 24} 25} 26 27 if (digit [n-1] & I <n) {// This integer has no leading 0 and is an odd 28 for (j = n-1; j> 0; -- j) 29 printf ("% d ", digit [J]); 30 printf ("% d \ n", digit [J]); 31} else {32 puts ("-1 "); 33} 34 35} 36 return 0; 37}
HDU 5055 Bob and math problem (simple greedy)