zoukankan      html  css  js  c++  java
  • 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; 
    }
    
    
  • 相关阅读:
    bzoj4152-[AMPPZ2014]The_Captain
    bzoj3209-花神的数论题
    [模板] 数位dp
    [西安交大附中集训] 自积
    [模板] 后缀数组
    [模板] 哈希表
    [西安交大附中集训] d6 删边(cip)
    java 发布订阅
    Spring Boot使用@Async实现异步调用:自定义线程池
    上传文件到服务器或者直接输出到输出流
  • 原文地址:https://www.cnblogs.com/woxiaosade/p/12404733.html
Copyright © 2011-2022 走看看