PAT甲级真题 1082 Read Number in Chinese (25分) C++实现(中文读数,分治法)

题目

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output “Fu” first if it is negative. For example, -123456789 is read as “Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu”. Note: zero (“ling”) must be handled correctly according to the Chinese tradition. For example, 100800 is “yi Shi Wan ling ba Bai”.
Input Specification:
Each input file contains one test case, which gives an integer with no more than 9 digits.
Output Specification:
For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.
Sample Input 1:
-123456789
Sample Output 1:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
Sample Input 2:
100800
Sample Output 2:
yi Shi Wan ling ba Bai

思路

很恶心的一道题,需要手动判断各种情况。

将数字分成3部分:p3亿p2万p1,每个部分的转换规则是相同的,问题就简化为4位数字的读法转换。

用函数printNum(int p)分别打印各个部分,处理的数字可能包含“千百十个”共四位。读“ling”的判断依据是:该位为百位或十位时,该位为0但高位不全为0。

代码

#include <iostream>
using namespace std;

string s[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
void printNum(int p){
    int qian = p / 1000;
    int bai = p % 1000 / 100;
    int shi = p % 100 / 10;
    int ge = p % 10;
    if (qian > 0){
        cout << s[qian] << " Qian";
    }
    if (bai > 0){
        if (qian>0){
            cout << " ";
        }
        cout << s[bai] << " Bai";
    }
    else if (qian>0 && (ge>0||shi>0)){
        cout << " ling";
    }
    if (shi > 0){
        if (qian>0 || bai>0){
            cout << " ";
        }
        cout << s[shi] << " Shi";
    }
    else if ((qian>0||bai>0) && ge>0){
        cout << " ling";
    }
    if (ge > 0){
        if (qian>0 || bai>0 || shi>0){
            cout << " ";
        }
        cout << s[ge];
    }
}
int main(){
    long long n;
    cin >> n;
    if (n == 0){
        cout << "ling";
        return 0;
    }
    if (n < 0){
        n = -n;
        cout << "Fu ";
    }
    int p1 = n / 100000000;
    int p2 = n / 10000 % 10000;
    int p3 = n % 10000;
    if (p1 != 0){
        printNum(p1);
        cout << " Yi";
    }
    if (p2 != 0){
        if (p1!=0){
            cout << " ";
            if (p2 < 1000){
                cout << "ling ";
            }
        }
        printNum(p2);
        cout << " Wan";
    }
    if (p3 != 0){
        if(p1!=0 || p2!=0){
            cout << " ";
            if (p3 < 1000){
                cout << "ling ";
            }
        }
        printNum(p3);
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分治法是一种常用的算法设计策略,它将一个大问题解成若干个相同或类似的子问题,然后递归地解决这些子问题,最后将子问题的解合并起来得到原问题的解。二查找是一种高效的查找算法,它通过将有序数组成两部,然后判断目标值在哪一部中,从而缩小查找范围。 在C++中,可以使用分治法实现查找的步骤如下: 1. 定义一个函数,接收一个有序数组和目标值作为参数。 2. 在函数内部,判断数组是否为空,如果为空则返回-1表示未找到目标值。 3. 计算数组的中间位置mid,将数组成左右两部。 4. 判断中间位置的元素与目标值的关系: - 如果中间位置的元素等于目标值,则返回mid。 - 如果中间位置的元素大于目标值,则在左半部继续进行二查找。 - 如果中间位置的元素小于目标值,则在右半部继续进行二查找。 5. 递归调用函数,在左半部或右半部进行二查找,直到找到目标值或者数组为空。 6. 如果找到目标值,则返回对应的索引;如果未找到,则返回-1表示未找到。 下面是一个使用分治法实现查找的C++代码示例: ```cpp #include <iostream> #include <vector> int binarySearch(const std::vector<int>& nums, int target) { int left = 0; int right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) { return mid; } else if (nums[mid] > target) { right = mid - 1; } else { left = mid + 1; } } return -1; } int main() { std::vector<int> nums = {1, 3, 5, 7, 9}; int target = 5; int result = binarySearch(nums, target); if (result != -1) { std::cout << "目标值在数组中的索引为:" << result << std::endl; } else { std::cout << "未找到目标值" << std::endl; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值