PAT甲級1023 Have Fun with Number
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.
翻譯:請注意,數字123456789是一個9位數的數字,完全由1到9組成,沒有重複。給它加倍之後我們將得到246913578,它恰好是另一個9位數的數字,正好包含從1到9的數字,只是排列不同。如果我們再加倍看看結果!
現在,假設您要檢查是否有更多具有此屬性的數字。也就是說,給一個有k位數字的數加倍,你要知道結果數字是否只由原始數字中的數字排列組成。
Input Specification:
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
Output Specification:
For each test case, first print in a line “Yes” if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or “No” if not. Then in the next line, print the doubled number.
Sample Input:
1234567899
Sample Output:
Yes
2469135798
思路
這題很簡單,就是普通的大數乘法模擬,要注意的是:不是判斷加倍前的每位數是否在加倍後的數中出現過,而是判斷是否由加倍前的每一位陣列成的。也就是說要統計數的個數(可能只有我一個人理解錯題意了,不過也只有測試點4出錯)正確理解題意之後這題就滿分AC
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
string get_double(string);
int str1[10] = {0}; //記錄加倍前的每位數出現的頻數
int str2[10] = {0}; //記錄加倍後的每位數出現的頻數
int main()
{
string str;
cin >> str;
for (int i = 0; i < str.size(); i++)
str1[str[i] - '0']++; //統計頻數
string d_str = get_double(str);
for (int i = 0; i < d_str.size(); i++)
str2[d_str[i] - '0']++;
int flag = 0;
for (int i = 0; i < 10; i++)
{
if (str1[i] != str2[i])
{
flag = 1;
break;
}
}
if (!flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
cout << d_str << endl;
system("pause");
return 0;
}
string get_double(string s) //模擬大數乘法
{
string ans;
int carry = 0;
for (int i = s.size() - 1; i >= 0; i--)
{
int d = (s[i] - '0') * 2 + carry;
carry = d / 10;
d %= 10;
ans = to_string((long long)d) + ans;
}
if (carry != 0)
ans = "1" + ans;
return ans;
}
相關文章
- 【PAT甲級A1038】Recover the Smallest Number (30分)(c++)C++
- HAVE FUN | SOFARegistry 原始碼解析原始碼
- HAVE FUN|Layotto 原始碼解析原始碼
- PAT 甲級 1152 Google Recruitment (20分)GoUI
- PAT甲級考試題庫題目分類
- HAVE FUN | 原始碼解析活動進展原始碼
- PAT甲級-1005. Spell It Right (20)各位之和
- 【PAT甲級A1084】Broken Keyboard (20分)(c++)C++
- PAT甲級1126~1130|C++實現C++
- PAT甲級-1010. Radix (25)進位制
- PAT甲級-1012. The Best Rank (25)並列排序排序
- 2024 秋季PAT認證甲級(題解A1-A4)
- PAT甲級1154 Vertex Coloring (25分)|C++實現C++
- PAT甲級1122 Hamiltonian Cycle (25分)|C++實現C++
- PAT甲級-1014. Waiting in Line (30)(模擬)AI
- 2021.9.12週六PAT甲級考試覆盤與總結
- 【PAT甲級A1065】A+B and C (64bit) (20分)(c++)C++
- PAT甲級1110 Complete Binary Tree (25分)|C++實現C++
- (非原創)PAT甲級1123 Is It a Complete AVL Tree (30分)|C++實現C++
- 菜鳥記錄:c語言實現PAT甲級1010--RadixC語言
- 資源 | 25個深度學習開源資料集,have fun !深度學習
- 【PAT乙級】1048 數字加密加密
- 1023 GPA計算
- PAT乙級——1093(字串匹配)Java實現字串匹配Java
- PAT乙級比賽-互評成績計算
- PTA甲級 1076 Forwards on Weibo (30分)Forward
- PAT 乙級 1094 谷歌的招聘 (20分)---【素數 字串】谷歌字串
- [leetcode] 1023. Camelcase MatchingLeetCode
- PAT乙級——1092(陣列排序 自定義sort)Java實現陣列排序Java
- 【PAT乙級、C++】1024 科學計數法 (20分)C++
- very important -have a LookImport
- coca would have done 搭配
- PAT-A Java實現Java
- Just for fun——Docker做個NMP環境Docker
- 論文分享-《GPU Memory Exploitation for Fun and Profit》GPU
- CF445D. Serega and Fun分塊
- PAT乙級1004 成績排名 (20分)(C語言版)及解析C語言
- 【PTA甲級、C++簡單解答】1001 A+B Format (20分)C++ORM