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) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7) << '\n'; // using function myfn as comp:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myfn) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7,myfn) << '\n'; // using object myobj as comp:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myobj) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7,myobj) << '\n'; 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 << '\n';
for (std::string::reverse_iterator rit=str.rbegin(); rit!=str.rend(); ++rit)
std::cout << *rit;
return 0;
}

1010 Radix (25)(25 point(s))的更多相关文章

  1. PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

  2. A1016 Phone Bills (25)(25 分)

    A1016 Phone Bills (25)(25 分) A long-distance telephone company charges its customers by the followin ...

  3. MySQL5.7.25(解压版)Windows下详细的安装过程

    大家好,我是浅墨竹染,以下是MySQL5.7.25(解压版)Windows下详细的安装过程 1.首先下载MySQL 推荐去官网上下载MySQL,如果不想找,那么下面就是: Windows32位地址:点 ...

  4. PAT 甲级 1006 Sign In and Sign Out (25)(25 分)

    1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...

  5. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  6. 【PAT】1052 Linked List Sorting (25)(25 分)

    1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...

  7. 【PAT】1060 Are They Equal (25)(25 分)

    1060 Are They Equal (25)(25 分) If a machine can save only 3 significant digits, the float numbers 12 ...

  8. 【PAT】1032 Sharing (25)(25 分)

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

  9. 【PAT】1015 德才论 (25)(25 分)

    1015 德才论 (25)(25 分) 宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得 ...

  10. 1003 Emergency (25)(25 point(s))

    problem 1003 Emergency (25)(25 point(s)) As an emergency rescue team leader of a city, you are given ...

随机推荐

  1. 20145202马超 2016-2017-2 《Java程序设计》第6周学习总结

    20145202马超 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 进程:是一个正在执行中的程序,每一个进程都有一个执行程序,该顺序是一个执行路径,或者说是 ...

  2. SQL查找数据库中所有没有主键的数据表脚本

    --SQL查找数据库中所有没有主键的数据表脚本 --运行脚本后在消息中可能会显示下面现象中的一种:--(1)数据库中所有数据表都有主键(则证明所有数据表都有主键)--(2)当前数据表[数据表名]没有主 ...

  3. TC-572-D1L2 未完!待续!

    题目描述 • 有一个神秘的常数 K ,s 位• 现在有 n 个 s 位数,告诉你每个数与 K 有多少位是相同的• 判断 K 的无解.多解.唯一解,并求出唯一解(如果存在的话)• 所有出现的数都允许前导 ...

  4. aarch64_j1

    JSCookMenu-2.0.4-13.fc26.noarch.rpm 2017-02-14 07:06 37K fedora Mirroring Project Java-WebSocket-1.3 ...

  5. grpc 实现微服务生态笔记

    微服务的发展可谓是一波三折,一代一代经历和N多技术成果,grpc只是其中一个,因为其东家是google,明显比较稳定.加上其强大的文档和技术支持和跨平台的支持,在企业级应用上有很大的可信任感,所以也有 ...

  6. jupyter(ipython notebook) 安装和入门教程

    近期大家无论是自己做数据分析还是紧急答辩做PPT,可能都需要画一些数据的展示图:以前大家都是用excel画图,但excel画图存在一定的局限性,比如你要画个累积直方图,excel就很麻烦了,所以给大家 ...

  7. Codeforces 2B The least round way(dp求最小末尾0)

    题目链接:http://codeforces.com/problemset/problem/2/B 题目大意: 给你一个nxn的矩形,找到一条从左上角到右下角的路径,使得该路径上所有数字的乘积的末尾0 ...

  8. sql server中分布式查询随笔(链接服务器(sp_addlinkedserver)和远程登录映射(sp_addlinkedsrvlogin)使用小总结)

    由于业务逻辑的多样性,经常得在sql server中查询不同数据库中数据,这就产生了分布式查询的需求 现我将开发中遇到的几种查询总结如下: 1.access版本 --建立连接服务器 EXEC sp_a ...

  9. Shell学习笔记:<<EOF子命令

    在shell编程中,“EOF”通常与“<<”结合使用,“<<EOF”表示后续的输入作为子命令或子shell的输入,直到遇到“EOF”,再次返回到主调用shell,可将其理解为分 ...

  10. ASP.NET:插件化机制

    概述 nopCommerce的插件机制的核心是使用BuildManager.AddReferencedAssembly将使用Assembly.Load加载的插件程序集添加到应用程序域的引用中.具体实现 ...