zoukankan      html  css  js  c++  java
  • 1010. Radix (25)

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a binary number.

    Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

    Input Specification:

    Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
    N1 N2 tag radix
    Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number "radix" is the radix of N1 if "tag" is 1, or of N2 if "tag" is 2.

    Output Specification:

    For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print "Impossible". If the solution is not unique, output the smallest possible radix.

    Sample Input 1:
    6 110 1 10
    
    Sample Output 1:
    2
    
    Sample Input 2:
    1 ab 1 2
    
    Sample Output 2:
    Impossible
    
    
    
    暴力破解,只得了22分,已知足了,只可惜是个半成品!!!复试之后再写吧。。
    #include <iostream>
    #include <map>
    #include <queue>
    #include <stdio.h>
    #include <vector>
    #include <algorithm>
    #include <stack>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <string>
    using namespace std;
    
    int p1(char *p,int radix)  
    {  	  
    	int len=strlen(p);  
    	int digit = 0;  
    	int m = 1;  
    	int sum = 0;  
    	for(int i=len-1;i>=0;i--)  
    	{  
    		if(p[i]>='a'&&p[i]<='z') { 
    			digit= p[i] - 'a' + 10;
    			if(digit>=radix)
    				break;
    		}
    		else if(p[i]>='0'&& p[i]<='9'){  
    			digit=p[i] - '0'; 
    			if(digit>=radix)
    				break ;
    		}
    		sum+=digit*m;  
    		m*=radix;
    	}  
    	return sum;  
    }
    int max(int x,int y){
    	return x<y?y:x;
    }
    int main(){
    	int n,m,p,q;
    	char c1[10],c2[10];
    	scanf("%s%s%d%d",c1,c2,&p,&q);
    	int flag=0;
    	int l1=strlen(c1);
    	int l2=strlen(c2);
    	int l=max(l1,l2);
    
    	if(p==1){
    		int i;
    		int n1=p1(c1,q);
    		for(i=1;i<10000;i++){
    			//int n2=p1(c2,i);
    			if(n1==p1(c2,i)){
    				cout<<i<<endl;
    				break;
    			}
    		}
    		if(i==10000)
    			cout<<"Impossible"<<endl;
    	}else{
    		int n1=p1(c2,q);
    		int i;
    		for(i=1;i<10000;i++){
    			int n2=p1(c1,i);
    			if(n1==n2){
    				cout<<i<<endl;
    				break;
    			}
    		}
    		if(i==10000)
    			cout<<"Impossible"<<endl;
    	}
    	return 0;
    }


  • 相关阅读:
    Objective-C 复合
    useContext的使用
    context的使用
    redux使用(二)
    redux使用(一)
    React class & function component 的区别
    combineReducers使用
    gnvm使用(未使用成功)
    React相关知识点
    eslint简单使用&&eslint与webpack结合使用
  • 原文地址:https://www.cnblogs.com/zh9927/p/4099040.html
Copyright © 2011-2022 走看看