zoukankan      html  css  js  c++  java
  • 1019. 数字黑洞 (20)

    原题: https://www.patest.cn/contests/pat-b-practise/1019

    思路: 本题主要就是熟练掌握字符串转整数, 整数转字符串的问题.
    注意考虑一个特殊的输入情况, 就是输入 6174

    完整实现:

    #include <stdio.h>
    
    void swap (char *a, char *b);
    void intToStr (int n, char str[]);
    void sortStr (char str[], int type); // 1 big to small
    int strToInt (char str[]);
    
    int main () {
        int n;
        int big;
        int small;
        int blackHole = 6174;
        char str[10];
    
        scanf("%d", &n);
        if (n == blackHole) {
            printf("7641 - 1467 = 6174
    "); // 给我6174? OK, 直接写死!
        } else {
            while (n != blackHole) {
                intToStr(n, str);
                sortStr(str, 1);
                big = strToInt(str);   // 拿到大数
                sortStr(str, 0);
                small = strToInt(str); // 拿到小数
                if (big == small) {
                    printf("%04d - %04d = %04d
    ", big, small, 0);
                    break;
                }
                n = big - small;
                printf("%04d - %04d = %04d
    ", big, small, n);
            }
        }
    
        return 0;
    }
    
    // 正整数整数转字符串
    // 参数: n, 待转换的整数, str[], 保存字符串的数组
    void intToStr (int n, char str[]) {
        char cur;
        int i = 0;
        while (n != 0) {
            cur = (char)(n % 10) + '0';
            str[i] = cur;
            i++;
            n = n / 10;
        }
        str[i] = '';
        // 如果转换后的字符串不足4位, 人工补0
        if (i == 1) {
            str[1] = '0';
            str[2] = '0';
            str[3] = '0';
            str[4] = '';
        } else if (i == 2) {
            str[2] = '0';
            str[3] = '0';
            str[4] = '';
        } else if (i == 3) {
            str[3] = '0';
            str[4] = '';
        }
    }
    
    // 字符串转整数
    int strToInt (char str[]) {
        char *ptr = str;
        int res = 0; // 转换后的整数
        while (*ptr != '') {
            res = res * 10 + (*ptr - '0');
            ptr++;
        }
        return res;
    }
    
    // 字符串排序, str长度必然是4
    // type等于1, 从大到小排序, 等于0, 从小到大排序
    void sortStr (char str[], int type) {
        int i;
        int j;
        for (i=1; i<=3; i++) {
            for (j=0; j<=3-i; j++) {
                if (type == 1 && str[j+1] > str[j]) {
                    swap(&str[j], &str[j+1]);
                } else if (type == 0 && str[j+1] < str[j]) {
                    swap(&str[j], &str[j+1]);
                }
            }
        }
    }
    
    // 交换两个字符变量
    void swap (char *a, char *b) {
        char temp = *a;
        *a = *b;
        *b = temp;
    }
    
    
  • 相关阅读:
    IE浏览器不能启动,双击启动图标无效
    提示Internet Explorer 9 已安装在此系统上,无法完成安装
    React项目跨域处理(两种方案)
    Mock数据模拟Node服务器
    【LeetCode】15. 3Sum
    【C++】int与string互转
    【LeetCode】37. Sudoku Solver
    【LeetCode】149. Max Points on a Line
    【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
    【LeetCode】140. Word Break II
  • 原文地址:https://www.cnblogs.com/asheng2016/p/7711422.html
Copyright © 2011-2022 走看看