zoukankan      html  css  js  c++  java
  • PTA 自测-4 Have Fun with Numbers

    PTA 自测-4 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.

    输入格式

    Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

    输出格式

    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.

    输入输出样例

    输入样例#1
    1234567899
    
    输出样例#1
    Yes
    2469135798
    

    题目思路

    理解题意,题目意思为原数x2之后,每个数字出现的次数相同为Yes,并且无论Yes/No都要输出x2的结果。
    最大长度为20位,所以采用C++的高精度算法来进行计算。

    #include<iostream>
    #include<string>
    #include<vector>
    int a[10], b[10];
    using namespace std;
    int main()
    {
    	string str;
    	cin >> str;
    	vector<int> c;
    	for (int i = str.size() - 1; i >= 0; i--)c.push_back(str[i] - '0');
    	int t = 0;
    	for (int i = 0; i < c.size(); i++) {
    		c[i] *= 2;
    		c[i] += t;
    		t = c[i] > 9 ? 1 : 0;
    		if (t)c[i] %= 10;
    	}
    	if (t)c.push_back(1);
    	if (c.size() != str.size()) {
    		cout << "No" << endl;
    		for (int i = c.size() - 1; i >= 0; i--)
    			cout << c[i];
    		return 0;
    	}
    	for (int i = 0; i < str.size(); i++) {
    		a[str[i] - '0']++;
    		b[c[i]]++;
    	}
    	for (int i = 0; i < 10; i++)
    		if (a[i] != b[i]) {
    			cout << "No" << endl;
    			for (int i = c.size() - 1; i >= 0; i--)
    				cout << c[i];
    			return 0;
    		}
    	cout << "Yes" << endl;
    	for (int i = c.size() - 1; i >= 0; i--)
    		cout << c[i];
    	return 0;
    }
    
  • 相关阅读:
    1-13Object类之toString方法
    jvm源码解读--16 锁_开头
    jvm源码解读--16 cas 用法解析
    jvm源码解读--15 oop对象详解
    jvm源码解读--14 defNewGeneration.cpp gc标记复制之后,进行空间清理
    jvm源码解读--13 gc_root中的栈中oop的mark 和copy 过程分析
    Error: Could not find or load main class ***
    使用javah 给.class类编译jni_helloworld.h文件头
    jvm源码解读--12 invokspecial指令的解读
    jvm源码解读--11 ldc指令的解读
  • 原文地址:https://www.cnblogs.com/fsh001/p/13227761.html
Copyright © 2011-2022 走看看