欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

蓝桥题目:B-20、数的读法

程序员文章站 2022-06-12 21:07:02
...

蓝桥题目:B-20、数的读法
基本思想:从最小位,即字符串的末端开始计算,每四个进行统计,插入位数和读法。

#include <iostream>
#include <cstring>

using namespace std;

int main() {
	string n;
	cin >> n;
	
	char *read[] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
	char *sign[] = {"","shi","bai","qian","wan","yi"};//开头多加一个“”使坐标更方便 
	string rst;
	int l = n.length();
	int flag = 0;//用来标记这是四位数中的第几个 
	for(int i = l-1; i >= 0; --i){
		
		if(flag % 4 == 0){//若是第一位 
			
			if(i == l-1){//判断是否是整体第一个数 
				flag++;
			}else{
				if( ( (l-i) / 4 ) % 2){
					if(n[i] == '0' && n[i-1] == '0' && n[i-2] == '0' && n[i-3] == '0'){//四位数全为0直接跳过 
						i -= 3;
						continue;
					}
					rst = sign[4] + rst;//前加万
					rst = " " + rst;
				}else{
					rst = sign[5] + rst;//前加亿 
					rst = " " + rst;
				}
				flag++;
			}
			if(n[i] == '0'){
				continue;
			}
			
		}else{
			if(n[i] == '0'){
				if(n[i+1] != '0'){//四位数中间非连续的0需读出 
					rst = read[0] + rst;
					rst = " " + rst;
				}
				flag++;
				continue;
			}
			rst = sign[flag % 4] + rst;//前加十百千 
			rst = " " + rst;
			flag++;
		}
		
		rst = read[ n[i] - '0' ] + rst;
		rst = " " + rst;
		
	}
	
	rst.erase(0,1);
	int index = 0;
	if(rst.find("yi shi",index) == 0)
		rst.erase(0,3); 
	cout << rst; 
	
	return 0;
}