L1-007. 念数字(C++)
来源:互联网 发布:360软件升级提醒 编辑:程序博客网 时间:2024/05/20 17:27
原题链接:https://www.patest.cn/contests/gplt/L1-007
我的解题思路:
首先,看到有ling yi这样的字符串类的东西,联想到要准备好字符串数组。输入格式提示整数包括负数、零和正数所以采用分治策略可能比较简单。
题目的计算难度在于拿到每一位是什么然后输出,一般来说都是用不断对10取余然后除以10的方法去拿到各个位上面的数,但是是从最低位到最高位,输出是从最高位到最低位的,为了输出——使用栈。
字符串——string
倒置信息——栈——stack
我的代码:
#include<iostream>#include<string>#include<stack>using namespace std;int main(){ //输入及准备 string shu_zi_pin_yin[10] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" }; int input_number = 0; cin >> input_number; stack<int> every_result;//存储各个位数 //分情况计算每位 if (input_number>0) { while (input_number/10!=0)//如果一个数除以10等于0,那么它一定是个位数 { every_result.push(input_number % 10); input_number /= 10; } every_result.push(input_number);//最后一位入栈 } else if (input_number==0) { cout << "ling"; } else { //这里手懒了,直接乘了-1,对应正数的算法了 cout << "fu "; input_number = (-1)*input_number; //同上面的解法 while (input_number / 10 != 0) { every_result.push(input_number % 10); input_number /= 10; } every_result.push(input_number); } //输出部分 while (!every_result.empty()) { //倒置输出 int this_pinyin_number = every_result.top(); //打印 if (every_result.size()==1)//最后一个数区别对待,后面没有空格 { cout << shu_zi_pin_yin[this_pinyin_number]; } else { cout << shu_zi_pin_yin[this_pinyin_number] << " "; } //弹出栈顶,进行下一轮循环 every_result.pop(); } system("pause"); return 0;}
0 0
- L1-007. 念数字(C++)
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字
- L1-007. 念数字 Java
- PAT L1-007. 念数字
- Opencv2.4.9源码分析——Gradient Boosted Trees
- iOS学习之—— UIGestureRecognizerState的简单了解
- 人脸识别之人脸检测(三)--Haar特征原理及实现
- 前台属性----localStorage、sessionStorage用法总结
- Linux日常维护命令
- L1-007. 念数字(C++)
- 今天开通了博客,应该写一篇我的记录
- Build path ->No action available/classpath .project 作用
- 【BZOJ1483】[HNOI2009]梦幻布丁【启发式合并】【Set】
- 深入理解Java的接口和抽象类
- eclipse-adt与androidstudio项目目录对比
- Java读写注册表两种方式
- 多选框checkbox和单选框用法
- 组合预测模型