A1082 Read Number in Chinese (25)(25 分)

A1082 Read Number in Chinese (25)(25 分)

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

思路

这个和某个基础编程题目集里的相似,一时半会想不到了。

7-23 币值转换(20 分)https://pintia.cn/problem-sets/14/problems/803

代码

#include<stdio.h>

int main ()
{
    int n, initial_n;
    scanf("%d", &n);
    initial_n = n; // 保留初始值 

    char num[10] = {‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘};//数字转字母
    char unit[10] = {0, 0, ‘S‘, ‘B‘, ‘Q‘, ‘W‘, ‘S‘, ‘B‘, ‘Q‘, ‘Y‘};//舍弃前两位
    char result[17]={0}; // 9 位数最多有 17 位输出 

    int i, last_i = n % 10; //保存前一位的想法是我所欠缺的
    int j = 0;
    int count_n = 0;
    while (n > 0) {
        i = n % 10;
        n /= 10;//奇怪,不断除以10,是从右边取下一个数,和我从左取数不同
        count_n ++;//数字总位数
        if (i == 0 && (count_n % 4) != 1) { // 每4位后导0不输出
        //从十位开始统计(个位0与万位0永远不输出)
            if (last_i != 0) {   // 如果前一位不等于 0,那就输出这个 0
                result[j++]  = num[i]; // count_n % 4) > 1即2.3.6.7位的零才能输出
            }
        }
        //这里有问题,2220300,贰佰贰拾贰万零叁佰
        //2003000, 贰佰万叁仟 ,注意万位0是不输出的,这里0可输出也可不输出
        //阿拉伯金额数字万位和元位是"0",
//      或者数字中间连续有几个"0",
//      万位、元位也是"0",
//      但千位、角位不是"0"时,
//      中文大写金额中可以只写一个零字,
//      也可以不写"零"字。如¥1680.32,
//      应写成人民币壹仟陆佰捌拾元零叁角贰分,
//      或者写成人民币壹仟陆佰捌拾元叁角贰分,
//      又如¥107000.53,
//      应写成人民币壹拾万柒仟元零伍角叁分,
//      或者写成人民币壹拾万零柒仟元伍角叁分。
        if (count_n == 5 && i == 0 && initial_n < 100000000) {
            result[j++] =  unit[count_n]; // 万 w 是一定要输出的,保证了w一定输
        }

        if (count_n > 1 && i != 0) {    // 非 0 时输出单位
            result[j++] = unit[count_n];
        }
        if (i != 0) {               // 处理非 0 数的输出
            result[j++] = num[i];
        }
        last_i = i; //保留 i 的前一位的值 用于处理 0
    }

    if (initial_n == 0) {       // 处理特殊值 0
        result[j++]  = num[0];
    } 

    for (int k=j-1; k>=0; k--) {
        printf("%c", result[k]);//采用倒着的字符数组
    }
    printf("\n");

    return 0;
}

现在回想,和A1082一样,本质是0的问题。

4位一节,每节的个十百位非零位高一位的零,是要输出零的。

这就是原则。

#include <cstdio>
#include <cstring>
char num[10][5] = {
    "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"
};
char wei[5][5] = {"Shi", "Bai", "Qian", "Wan", "Yi"};
int main() {
    char str[15];
    gets(str);
    int len = strlen(str);
    int left = 0, right = len - 1;
    if(str[0] == ‘-‘) {
        printf("Fu");
        left++;
    }
    while(left + 4 <= right) {
        right -= 4;
    }
    while(left < len) {
        bool flag = false;
        bool isPrint = false;
        while(left <= right) {
            if(left > 0 && str[left] == ‘0‘) {
                flag = true;
            } else {
                if(flag == true) {
                    printf(" ling");
                    flag = false;
                }
                if(left > 0) {
                    printf(" ");
                }
                printf("%s", num[str[left] - ‘0‘]);
                isPrint = true;
                if(left != right) {
                    printf(" %s", wei[right - left - 1]);
                }
            }
            left++;
        }
        if(isPrint == true && right != len - 1) {
            printf(" %s", wei[(len - 1 - right) / 4 + 2]);
        }
        right += 4;
    }
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
char num[10][5] = {
    "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"
};
char wei[5][5] = {"Shi", "Bai", "Qian", "Wan", "Yi"};
int main() {
    char str[15];
    gets(str);//读入一行字符串,记录数字
    int len = strlen(str);//字符串长度
    int left = 0, right = len - 1;//leftright分别指向字符串首尾元素
    if(str[0] == ‘-‘) {//负数,输出“Fu”,把left右移一位
        printf("Fu");
        left++;
    }
    while(left + 4 <= right) {
        right -= 4;//将right每次左移4位,直到left与right在同一节
    }
    /*这一步,直接令right为3或4行吗,不行,因为是从个位即字符串的末尾4位又4位的计算
    如果翻转字符串还差不多
    left指向当前需要输出的位,right指向与left同节的个位*/
    while(left < len) {//循环每次处理数字的一节(4位或小于4位)
        bool flag = false;//此时表示没有累积的0
        bool isPrint = false;//表示该节没有输出过其中的位
        while(left <= right) {//从左至右处理数字中某节的每一位
            if(left > 0 && str[left] == ‘0‘) {//如果当前位为0
                flag = true;//令标记flag为true
            } else {//如果当前位不为0
                if(flag == true) {//如果存在累积的0
                    printf(" ling");
                    flag = false;
                }
                //只要不是首位(包括负号),后面的每一位前都要输出空格
                if(left > 0) {
                    printf(" ");
                }
                printf("%s", num[str[left] - ‘0‘]);//输出当前位数字
                isPrint = true;//该节至少有一位被输出
                if(left != right) {//某节中除了个位外,都需要输出十百千
                    printf(" %s", wei[right - left - 1]);//输出十百千的映射,简单推理得到
                }
            }
            left++;//left右移一位
        }
        if(isPrint == true && right != len - 1) {//只要不是个位,就输出万或者亿
            printf(" %s", wei[(len - 1 - right) / 4 + 2]);//输出万位和亿位,len-1-right值取4或8,取不到12
        }
        right += 4;//righr右移4位,输出下一节
    }
    return 0;
}

终于要进入入门篇(2)算法初步了,其实现在应该做完第六章才对,知识储备足够了。

原文地址:https://www.cnblogs.com/lingr7/p/9452259.html

时间: 2024-12-12 13:42:53

A1082 Read Number in Chinese (25)(25 分)的相关文章

1082 Read Number in Chinese (25 分)

1082 Read Number in Chinese (25 分) Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fufirst if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan li

PAT 1082. Read Number in Chinese (25)

1082. Read Number in Chinese (25) 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

1082. Read Number in Chinese (25)【字符串处理】——PAT (Advanced Level) Practise

题目信息 1082. Read Number in Chinese (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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

【PAT甲级】1082 Read Number in Chinese (25 分)

题意: 输入一个九位整数,输出它的汉字读法(用拼音表示). trick: 字符串数组""其实会输出一个空格,而不是什么都不输出,导致测试点0和4格式错误. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int num[17];string united[17]={"","yi","er","san&qu

Read Number in Chinese (25)

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

1082. Read Number in Chinese (25)

题目如下: 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 b

PAT (Advanced Level) 1082. Read Number in Chinese (25)

模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

HDU 1018.Big Number【7月25】

Big Number 这道题,没有什么难点什么的,就是为了涨姿势. Problem Description In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, y

PAT1082. Read Number in Chinese

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 Sh