日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

PAT Advanced Level 1010

發(fā)布時(shí)間:2023/11/27 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT Advanced Level 1010 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1010 Radix (25)(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

?

?

/**********************
author: yomi
date: 18.6.8
現(xiàn)在來(lái)梳理思路:
1. 把已知進(jìn)制的數(shù)轉(zhuǎn)換為相應(yīng)的已給的進(jìn)制 (為什么要轉(zhuǎn)呢?因?yàn)樗o你的這個(gè)數(shù)并不一定是正確的表示,比如說(shuō)
ab 二進(jìn)制 , 那么就需要把a(bǔ)b先轉(zhuǎn)到二進(jìn)制)
2. 尋找未知進(jìn)制數(shù)的進(jìn)制,使他與前面的數(shù)相等--->進(jìn)制數(shù)一定是從小到大遍歷的,那么可以用到二分
然而還有一些小問(wèn)題要去攻克:
1.二分上下界的確定:二分下界:進(jìn)制必須能夠表示這個(gè)數(shù),所以該進(jìn)制必須大于這個(gè)數(shù)中最大的那位數(shù),故下界為:最大的那位數(shù)+1二分上界:分兩種情況:一是已知數(shù)大于下界,那么上界為:已知數(shù)二是已知數(shù)小于下界,那么上界為:下界(容易想到的是上界為已知數(shù),而由于上界不能小于下界,故此時(shí)上界與下界相等)這樣就歸結(jié)成了一個(gè)問(wèn)題,為什么上界為已知數(shù)?需要指出的是 A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. 只是說(shuō)某一位數(shù)的進(jìn)制為36,而我卻以為我們要找的進(jìn)制也在36進(jìn)制以內(nèi),結(jié)果就WA了一半。實(shí)際上,題目并沒(méi)有提上界,而當(dāng)要找的進(jìn)制數(shù)比已知數(shù)大的時(shí)候,這兩個(gè)數(shù)就決不可能再相等了。所以將上界設(shè)置為已知數(shù)即可(一定是轉(zhuǎn)換到已知進(jìn)制數(shù)的已知數(shù))。
看了大神的代碼收獲很多:
1. char it = *max_element(n.begin(), n.end());//查找最大元素的好辦法整型數(shù)據(jù)只要在vector中就可以使用啦
2. 適當(dāng)使用三目運(yùn)算符確實(shí)能使代碼簡(jiǎn)潔不少,這樣有助于考場(chǎng)上分析。不管你們懂沒(méi)懂,反正我懂了。
*********************分析為原創(chuàng),代碼部分原創(chuàng) 改編自柳婼******************/#include <iostream>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
long long convert(string n, long long radix) {
??? long long sum = 0;
??? int index = 0, temp = 0;
??? for(int i=0; i<n.length(); i++){
??????? temp = isdigit(n[i]) ? n[i]-'0' : n[i]-'a'+10;
??????? sum = sum*radix+temp;
??? }
??? return sum;
}

long long find_radix(string n, long long num) {char it = *max_element(n.begin(), n.end());//查找最大元素的好辦法long long low = (isdigit(it) ? it - '0': it - 'a' + 10) + 1;long long high = max(num, low);while (low <= high) {long long mid = (low + high) / 2;long long t = convert(n, mid); /// n->mid->t n->mid->tif (t < 0 || t > num) high = mid - 1;///1010->10->1010 1010->2->10 故t>num時(shí)說(shuō)明當(dāng)前進(jìn)制大 應(yīng)該往左找 t<0是溢出的情況 else if (t == num) return mid; else low = mid + 1;}return -1; } int main() {string n1, n2;long long tag = 0, radix = 0, result_radix;cin >> n1 >> n2 >> tag >> radix;result_radix = tag == 1 ? find_radix(n2, convert(n1, radix)) : find_radix(n1, convert(n2, radix));if (result_radix != -1) {printf("%lld", result_radix);} else {printf("Impossible");}return 0; }/** 沒(méi)用二分17分 一堆答案錯(cuò)誤 因?yàn)槲覜](méi)考慮好radix的范圍 #include <iostream> #include <string> #include <fstream> #include <cstdio> using namespace std;int pow(int a, int n) {int ans = 1;for(int i=0; i<n; i++){ans *= a;}return ans; } int main() {int tag, radix;string a,b;int flag = 1;int aa[20], bb[20];long long int n1 = 0, n2 = 0;int ans = 0;cin >> a >> b >> tag >> radix;int cnt1 = 0, cnt2 = 0;for(int i=0; i<a.length(); i++){if(a[i]>='0' && a[i]<='9'){aa[cnt1++] = a[i]-'0';}else if(a[i]>='a' && a[i]<='z'){aa[cnt1++] = a[i] - '0' - 39;}}for(int i=0; i<b.length(); i++){if(b[i]>='0' && b[i]<='9'){bb[cnt2++] = b[i]-'0';}else if(b[i]>='a' && b[i]<='z'){bb[cnt2++] = b[i] - '0'-39;}} // for(int i=0; i<cnt2; i++){ // cout << bb[i] << endl; // }if(tag == 1){//to find 2nd's radix// convert 1st and 2nd to decimalfor(int i=0; i<cnt1; i++){n1 += aa[i]*pow(radix, cnt1-i-1);}for(ans = 2; ans <=36; ans++){n2 = 0;for(int i=0; i<cnt2; i++){n2 += bb[i]*pow(ans, cnt2-i-1);}if(n2 == n1){flag = 0;break;}}}else if(tag == 2){//to find 1st's radix//convert 1st and 2nd to decimalfor(int i=0; i<cnt2; i++){n1 += bb[i]*pow(radix, cnt2-i-1);}for(ans = 2; ans <=36; ans++){n2 = 0;for(int i=0; i<cnt1; i++){n2 += aa[i]*pow(ans, cnt1-i-1);}if(n2 == n1){flag = 0;break;}}}if(flag == 0)cout << ans;elsecout << "Impossible";return 0; } **/ /** 6 110 2 2 21 ab 1 2 **/

?

轉(zhuǎn)載于:https://www.cnblogs.com/AbsolutelyPerfect/p/9154834.html

總結(jié)

以上是生活随笔為你收集整理的PAT Advanced Level 1010的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。