【PAT甲级 - C++题解】1023 Have Fun with Numbers

简介: 【PAT甲级 - C++题解】1023 Have Fun with Numbers

1023 Have Fun with Numbers


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.

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
• 1

Sample Output:

Yes
2469135798


题意


这道题是让我们计算给定的数字加倍过后的结果是否满足题目要求,比如 123456789 加倍过后得到 246913578 ,发现加倍后的数字都完全由加倍前的数字构成,即加倍前和加倍后都用到了相同的数字构成。


现在让我们输出加倍后的结果,如果满足要求就输出 Yes ,否则输出 No ,并且还要输出加倍后的结果。

思路

这道题由于数字的长度最多能取到 20 ,所以要用到高精度的算法。因为题目只要求算给定数字的平方,故我们可以直接计算该数自加的结果即 a ∗ 2 = a + a a * 2 = a + aa∗2=a+a ,这样就可以将高精度乘法问题转换为高精度加法问题,然后再套用高精度加法的模板即可求出本题的答案。

代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string str;
    cin >> str;
    vector<int> a;
    for (int i = str.size() - 1; i >= 0; i--)    a.push_back(str[i] - '0');
    //高精度加法运算模板,a*2=a+a
    vector<int> b;
    int t = 0;
    for (int i = 0; i < a.size(); i++)
    {
        t = a[i] + a[i] + t;
        b.push_back(t % 10);
        t = t / 10;
    }
    if (t) b.push_back(t);
    //判断是否满足条件
    vector<int> c = b;
    sort(a.begin(), a.end());
    sort(c.begin(), c.end());
    if (a == c)    puts("Yes");
    else    puts("No");
    //输出加倍后的数字
    for (int i = b.size() - 1; i >= 0; i--)  cout << b[i];
    return 0;
}



目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
303 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
382 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
363 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
331 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
257 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
304 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
511 0
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
759 12
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)