1023 Have Fun with Numbers(PAT (Advanced Level) Practice)
作者:互联网
目录
题目
输入样例
1234567899
输出样例
Yes
2469135798
提交结果截图
源代码
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string number;
int num_bit;//number的每一位数
int digit[10];//存储0~9出现的次数
int carry_flag = 0,//进位标志
same_flag = 0;//相同标志
cin >> number;
int len = number.length();
for (int i = 0; i < 10; i++)
digit[i] = 0;
for (int i = len - 1; i >= 0; i--)
{
num_bit = number[i] - '0';
digit[num_bit]++;
num_bit = num_bit * 2 + carry_flag;
carry_flag = 0;
if (num_bit >= 10)
{
num_bit -= 10;
carry_flag = 1;
}
number[i] = num_bit + '0';
digit[num_bit]--;
}
for (int i = 0; i < 10; i++)
if (digit[i])
same_flag = 1;
if (carry_flag || same_flag)
printf("No\n");
else
printf("Yes\n");
if (carry_flag)
cout << "1";
cout << number;
return 0;
}
标签:PAT,1023,Level,int,number,flag,num,carry,bit 来源: https://blog.csdn.net/NOT_TODAY1/article/details/120104674