1023. Have Fun with Numbers (20)
来源:互联网 发布:cloudify 教程 ubuntu 编辑:程序博客网 时间:2024/05/22 04:34
1. 原题: https://www.patest.cn/contests/pat-a-practise/1023
2. 思路:
题意:判断一个数的2倍所含有的数字是否和原来的相同。
思路:
基本运算题。
由于一个数可能有20位,只能用字符串string处理了。
2倍也要自己模拟乘法。最后用两个表示数位的数组,判断
每个元素是否相等。
思路:
基本运算题。
由于一个数可能有20位,只能用字符串string处理了。
2倍也要自己模拟乘法。最后用两个表示数位的数组,判断
每个元素是否相等。
3. 源码(已AC):
#include<iostream>#include<string>using namespace std;int main(){//freopen("in.txt", "r", stdin);string origin, result;//分别存储原始数、2倍后的数int ori[10] = {0};//0~9的数位值int res[10] = {0};cin >> origin;result = origin;int add = 0;//表示进位,可能为1;for(int i = result.size()-1; i >= 0; i--)//模拟乘法{int tem = result[i] - '0';int mul = 2*tem + add;if(mul > 9){result[i] = mul - 10 + '0';add = 1;}else{result[i] = mul + '0';add = 0;}}if(add != 0)//两个数位数不同,输出no{string re = "0";re[0] += add;re += result;//把result添加到re后面cout << "No\n" << re <<endl;}else{for(unsigned i = 0; i < origin.size(); i++)//判断是否相同{int index = origin[i] - '0';int index2 = result[i] - '0';ori[index]++;res[index2]++;}int flag = 0;for(int i = 0; i < 10; i++){if(ori[i] != res[i]){flag = 1;break;}}if(flag)//输出cout << "No\n" << result <<endl;elsecout << "Yes\n" << result << endl;}return 0;}
0 0
- 1023. Have Fun with Numbers (20)
- 【PAT】1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- PAT: 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- PAT 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- PAT 1023. Have Fun with Numbers (20)
- PAT 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- 1023. Have Fun with Numbers (20)
- css揭秘系列之半透明边框
- 1029. 旧键盘(20)
- 共同学习Java源代码-数据结构-HashSet(二)
- 用双向最大匹配法进行中文分词
- C#90课的主要内容
- 1023. Have Fun with Numbers (20)
- 替换WebView加载网页失败时的页面
- response中添加cookie时IllegalArgumentException错误
- 9.5.2
- 最新2016年传智播客前端开发视频教程1期
- So hard so brave --y\
- jsp错误 According to TLD, tag form:input must be empty, but is not
- 常用的meta标签
- HDU 5882 Balanced Game