PTA 7-23 币值转换

作者: smatrcHendsa | 来源:发表于2019-03-17 10:01 被阅读0次

    https://pintia.cn/problem-sets/14/problems/803
    不太好搞 跟作者对读数的方法的理解有区别

    #include <stdio.h>
    int x;
    int ch[16];
    char val[16];
    int n = 0;
    
    int findnext(int pos) {
        for (int i = pos; i >= 0; i--) {
    //      printf("%d %d ", i, (int)'a' + ch[i]);
            if (ch[i])
                return i;
        }
        return -1;  
    }
    
    int findpre(int pos) {
        for (int i = pos + 1; i < n; i++) {
            if (ch[i])
                return i;
        }
        return 0;   
    }
    
    int main() {
        while (1) {
            n = 0;
            scanf("%d", &x);
            if (!x)  {
                printf("a\n");
                return 0;
            }
            while (x > 0) {
                int d = x % 10;
                x /= 10;
                ch[n] = d;
                n++;
            }
            
            val[0] = 'S';
            val[1] = 'B';
            val[2] = 'Q';
            val[3] = 'W';
            
            val[4] = 'S';
            val[5] = 'B';
            val[6] = 'Q';
            val[7] = 'Y';
            val[8] = 'S';
            val[9] = 'B';
            val[10] = 'Q';
            
            int aldy = 0;
            for (int i = n - 1; i >= 0; i--) {
                //1000010 一百万零十元 
                //100000010 一亿零十元 
                //100100110 一亿十万一百一十元 
                //20000010 2000010
                bool flag = false;
                
                if (ch[i]) {//ch[i] != 0
                    printf("%c", (int)'a' + ch[i]);
                    flag = true;
                }
                else if (i % 4 != 0 && i && !aldy){//ch[i] is 0, aldy means 0 is printed in this block, no need print again
                    int next = findnext(i);
                    int pre = findpre(i); 
                    if (next >= 0) {
                        if (i - next < 4) {
                            printf("a");
                            aldy = i;
                            if (aldy > 4) 
                                aldy -= i / 4 * 4;
                        }
                        else if (pre / 4 - i / 4 > 0) {//if the prenon0 is not in the same block as this num.
                            aldy = i;
                            if (aldy > 4) 
                                aldy -= i / 4 * 4;
                            printf("a");
                        }   
                    }   
                }
                if (aldy > 0)
                    aldy--;
                if(!ch[i])
                if (i % 4 == 0 && findpre(i) - i < 4)
                    flag = true;
                if (flag)
                    printf("%c", val[i - 1]);
            }
            printf("\n");
        }
      return 0;
    }
    

    相关文章

      网友评论

        本文标题:PTA 7-23 币值转换

        本文链接:https://www.haomeiwen.com/subject/ehvymqtx.html