zoukankan      html  css  js  c++  java
  • 1023. Have Fun with Numbers (20)

    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 file 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


    题目大意:求大整数加法的一个变种,一个大整数(N<20),乘以2后的的另一个大整数,判断其中一个大整数是否可以通过位数的变化得到另一个大整数。

    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    using namespace std;
    #define max 22
    int number[max];
    int double_number[max];
    int main(){
    	char num[max];
    	scanf("%s",num);
    	memset(number,0,sizeof(number));
    	memset(double_number,0,sizeof(double_number));
    	int length = strlen(num);
    	int i,j=0,k=0;
    	for(i=length-1;i>=0;i--){
    		int value=num[i]-'0';
    		number[value]++;
    		double_number[i]=value*2;
    	}
    	int sum[max];
    	for(i=length-1,j=0;i>=0;i--,j++){
    		sum[j]=(double_number[i]+k)%10;
    		k=(double_number[i]+k)/10;
    	}
    	if(k!=0){
    		sum[j]=k;
    		j++;
    	}
    	int compare[max];
    	memset(compare,0,sizeof(compare));
    	for(i=0;i<j;i++){
    		compare[sum[i]]++;
    	}
    	int flag=0;
    	for(i=0;i<j;i++){
    		if(compare[i]!=number[i]){
    			flag = 1;
    			break;
    		}
    	}
    	if(flag == 1){
    		printf("No
    ");
    	}
    	else {
    		printf("Yes
    ");
    	}
    	for(i=j-1;i>=0;i--){
    		printf("%d",sum[i]);
    	}
    	printf("
    ");
    } 
    

      

  • 相关阅读:
    喜大普奔,微软Microsoft JDBC Driver For SQL Server已发布到maven中央仓库
    maven jdbc 驱动安装
    SpringIoC和SpringMVC的快速入门
    再见 Spring Boot 1.X ,Spring Boot 2.X 走向舞台中心
    LRU设计
    二叉搜索树转换成双向链表
    快速排序
    二叉搜索树的先序中序后序非递归遍历代码
    EM算法
    模型调优
  • 原文地址:https://www.cnblogs.com/grglym/p/7659481.html
Copyright © 2011-2022 走看看