• PAT 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 <stdio.h>
    #include <string>
    #include <stdlib.h>
    #include <iostream>
    #include <vector>
    #include <string.h>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <queue>
    #include <stack>
    #include <functional>
    #include <limits.h> 
    using namespace std;
    
    
    int main() {
    	string a;
    	cin >> a;
    	reverse(a.begin(), a.end());
    	int num[100];	memset(num, 0, sizeof(num));
    	string ans = "";
    	int carry = 0;
    	int flag = true;
    	for(int i = 0; i < a.length(); i++){
    		num[a[i] - '0']++;
    		ans.push_back((2 * (a[i] - '0') + carry) % 10  + '0');
    		carry = (2 * (a[i] - '0') + carry) / 10;
    	}
    	if(carry){
    		ans.push_back(carry + '0');
    	}
    	for(int i = 0; i < ans.length(); i++){
    		num[ans[i] - '0']--;
    	}
    	for(int i = 0; i < 15; i++){
    		if(num[i] != 0){
    			flag = false;
    			break;
    		}
    	}
    	reverse(ans.begin(), ans.end());
    	if(flag)	cout << "Yes" << endl;
    	else cout << "No" << endl;
    	cout << ans << endl;
    	return 0; 
    }
    
    
  • 相关阅读:
    Ruby单例方法和实例方法
    Silverlight本地化和全球化
    多线程 or 多进程 (转强力推荐)
    循环pthread_create导致虚拟内存上涨
    int在linux上的保存情况
    查看数据流的流程
    查看linux系统版本,内核,CPU,MEM,位数的相关命令(转)
    0/1背包问题
    linux下计算程序运行时间
    夸平台夸字符编码问题
  • 原文地址:https://www.cnblogs.com/woxiaosade/p/12404733.html
Copyright © 2020-2023  润新知