zoukankan      html  css  js  c++  java
  • PTA(Advanced Level)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 
    
    Sample Output:
    Yes
    2469135798
    
    思路
    • 就是高精度的乘法
    • 比较的时候注意最起码前后长度要一样
    代码
    #include<bits/stdc++.h>
    using namespace std;
    struct bignumber
    {
    	int num[30];
    	int len;
    	bignumber()
    	{
    		memset(num, 0, sizeof(num));
    		len = 0;
    	}
    }a;
    void assignment(string s)
    {
    	for(int i=0;i<s.size();i++)
    		a.num[i] = s[s.size() - i - 1] - '0';
    	a.len = s.size();
    }	//字符串->数组
    
    bignumber multiply(bignumber a, int b)
    {
    	bignumber c;
    	int carry = 0;
    	for(int i=0;i<a.len;i++)
    	{
    		int t = a.num[i] * b + carry;
    		c.num[c.len++] = t % 10;
    		carry = t / 10;
    	}
    	while(carry != 0)	//到最高位的时候仍有可能进位
    	{
    		c.num[c.len++] = carry % 10;
    		carry /= 10;
    	}
    	return c;
    };
    int main()
    {
    	string s;
    	cin >> s;
    
    	assignment(s);
    	bignumber b = multiply(a, 2);
    
    
    	int origin[10], double_origin[10];
    	memset(origin, 0, sizeof(origin));
    	memset(double_origin, 0, sizeof(double_origin));
        for(int i=0;i<a.len;i++)
    		origin[a.num[i]]++;		//统计a用的数字的次数
    	for(int i=0;i<b.len;i++)
    		double_origin[b.num[i]]++;	//统计b用的数字的次数
    	if(a.len != b.len)		//最基本要达到的是长度相等
    	{
    			cout << "No" << endl;
    			for(int j=b.len-1;j>=0;j--)    cout << b.num[j];
    			return 0;
    	}
    
    
    	for(int i=0;i<10;i++)
        {
    		if(origin[i] != double_origin[i])
    		{
    			cout << "No" << endl;
    			for(int j=b.len-1;j>=0;j--)    cout << b.num[j];
    			return 0;
    		}
        }
    	cout << "Yes" << endl;
    	for(int j=b.len-1;j>=0;j--)    cout << b.num[j];
    	return 0;
    }
    
    
    引用

    https://pintia.cn/problem-sets/994805342720868352/problems/994805478658260992

  • 相关阅读:
    hibernate_0100_HelloWorld
    MYSQL子查询的五种形式
    JSF是什么?它与Struts是什么关系?
    nop指令的作用
    htmlparser实现从网页上抓取数据(收集)
    The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the
    FCKeditor 在JSP上的完全安装
    Java遍历文件夹的2种方法
    充电电池和充电时间说明
    吃知了有什么好处
  • 原文地址:https://www.cnblogs.com/MartinLwx/p/12552315.html
Copyright © 2011-2022 走看看