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

    problem

    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
    

    tip

    给出两个数以及其中一个数的进制,求两个数相等时另一个数可能的进制。(数只可能由0-1,a-z组成,但进制可能会超过36进制,坑点。)

    answer

    #include<bits/stdc++.h>
    using namespace std;
    
    #define INF 0x3f3f3f3f
    
    long long  tag, radix;
    string a, b;
    
    void Impossible(){
    	cout<<"Impossible";
    }
    
    long long  GetIndex(char a){
    	return isdigit(a) ? a - '0' : a - 'a' + 10;
    }
    
    long long  CalStringToInt(string s, long long  index){
    	long long  a = 0;
    	int i = 0;
    	string::reverse_iterator it;
    	for(it = s.rbegin(); it != s.rend(); it++){
    		int temp = isdigit(*it) ? *it - '0' : *it - 'a' + 10;
    		a += temp * pow((float)index, i++);
    	}
    	return a;
    }
    
    int main(){
    //	freopen("test.txt", "r", stdin);
    	cin>>a>>b>>tag>>radix;
    	long long  maxIndex= 0;
    	string A;
    	long long  goal;
    	if(tag == 1) {
    		A = b;
    		goal = CalStringToInt(a, radix);
    	}else {
    		A = a;
    		goal = CalStringToInt(b, radix);
    	}
    	char me = *max_element(A.begin(), A.end());
    	maxIndex = (isdigit(me)?me-'0':me-'a'+10) +1;
    	
    	long long left = maxIndex, right = max(maxIndex, goal), mid = (left+right)/2;
    	bool flag = false;
    	while(left <= right){
    		mid = (left+right)/2;
    		long long temp = CalStringToInt(A, mid);
    		if(temp < 0 || temp > goal) right = mid -1;
    		else if(temp == goal) {flag = true; break;}
    		else left = mid +1;
    	}
    	if(flag) cout<<mid; else Impossible();
    	return 0;
    }
    

    experience

    • c++:
      • max_element 函数
    // min_element/max_element example
    
    #include <iostream>     // std::cout
    #include <algorithm>    // std::min_element, std::max_element
    
    bool myfn(int i, int j) { return i<j; }
    
    struct myclass {
      bool operator() (int i,int j) { return i<j; }
    } myobj;
    
    int main () {
      int myints[] = {3,7,2,5,6,4,9};
    
      // using default comparison:
      std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '
    ';
      std::cout << "The largest element is "  << *std::max_element(myints,myints+7) << '
    ';
    
      // using function myfn as comp:
      std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myfn) << '
    ';
      std::cout << "The largest element is "  << *std::max_element(myints,myints+7,myfn) << '
    ';
    
      // using object myobj as comp:
      std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myobj) << '
    ';
      std::cout << "The largest element is "  << *std::max_element(myints,myints+7,myobj) << '
    ';
    
      return 0;
    }
    
    - string interator && reverse_interator
    
    // string::begin/end
    #include <iostream>
    #include <string>
    
    int main ()
    {
      std::string str ("Test string");
      for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
        std::cout << *it;
      std::cout << '
    ';
      for (std::string::reverse_iterator rit=str.rbegin(); rit!=str.rend(); ++rit)
        std::cout << *rit;
      return 0;
    }
    
  • 相关阅读:
    asp.net中插件开发模式说明
    Url路径重写的原理
    Linux上搭建各种环境(一)
    常见Post提交数据方式接口测试
    Jmeter4.0----CSV Data Set Config_使用表格进行参数化(22)
    弱网测试----App
    性能测试基础
    使用fiddler实现手机抓包
    Jmeter4.0----发送邮箱之SMTP Sampler(21)
    Jmeter4.0----发送测试结果到邮箱之邮件观察仪(20)
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/9285934.html
Copyright © 2011-2022 走看看