thinking process

i'm break down when i get this program. i don't solve this problem by myself.what i have written is other solution. i cite and refer to him to study from his code.
His whole process is very simple.

three states are illegal:

  1. While the number of times it's been seen is odd and the odd number excess 1, it's illegal.(because if have a digit that is seen oddly we have to put it to the middle)
  2. 0 digit occurs more than once and only one digit expect 0 occurs and only once
  3. only 0 digit exists and appears more than 1.

combination
if odd exists, put the number to the middle first. and when length > 1 find the smallist number and put it to the begin and end respectively, and do a[smallistnumber]=2a[smallist number] -= 2.next from zero to nine, combine digits step by step.

everything is done. let's code.

#include<stdio.h>
 
int a[20];
char s[110];
int main () {
    int odd = 0; // odd's number
    int odp = 0; // value of odd happenings
    int sum = 0; // length
    int valid = 0; // 出现非0数位的次数
    int vak = 0; // 出现次数不为0的数位
    for(int i = 0; i < 10; ++ i) {
        scanf("%d", &a[i]);
        sum += a[i];
        if(a[i] & 1) odd ++, odp = i;
        if(a[i] > 0 && i > 0) valid ++, vak = i;
    }
    if(valid == 0 && a[0] > 1) return puts("-1"), 0;
    else if(odd > 1 || (valid == 1 && a[vak] == 1 && a[0])) return puts("-1"), 0;
    else {
        if(odd) s[sum / 2] = odp + '0', a[odp] --;
        if(sum > 1) {
            int p = 1, L = 0, R = sum - 1;
            while(!a[p] && p < 10) p ++;
            s[L ++] = p + '0', s[R --] = p + '0', a[p] -=2;
            p = 0;
            while(p < 10) {
                for(int i = 0;i < a[p] / 2; i ++ ) {
                    s[L ++] = p + '0', s[R --] = p + '0';
                }
                p ++;
            }
        }
        puts(s); return 0;
    }
}