日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

PAT Advanced Level 1010

發布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT Advanced Level 1010 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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
現在來梳理思路:
1. 把已知進制的數轉換為相應的已給的進制 (為什么要轉呢?因為他給你的這個數并不一定是正確的表示,比如說
ab 二進制 , 那么就需要把ab先轉到二進制)
2. 尋找未知進制數的進制,使他與前面的數相等--->進制數一定是從小到大遍歷的,那么可以用到二分
然而還有一些小問題要去攻克:
1.二分上下界的確定:二分下界:進制必須能夠表示這個數,所以該進制必須大于這個數中最大的那位數,故下界為:最大的那位數+1二分上界:分兩種情況:一是已知數大于下界,那么上界為:已知數二是已知數小于下界,那么上界為:下界(容易想到的是上界為已知數,而由于上界不能小于下界,故此時上界與下界相等)這樣就歸結成了一個問題,為什么上界為已知數?需要指出的是 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. 只是說某一位數的進制為36,而我卻以為我們要找的進制也在36進制以內,結果就WA了一半。實際上,題目并沒有提上界,而當要找的進制數比已知數大的時候,這兩個數就決不可能再相等了。所以將上界設置為已知數即可(一定是轉換到已知進制數的已知數)。
看了大神的代碼收獲很多:
1. char it = *max_element(n.begin(), n.end());//查找最大元素的好辦法整型數據只要在vector中就可以使用啦
2. 適當使用三目運算符確實能使代碼簡潔不少,這樣有助于考場上分析。不管你們懂沒懂,反正我懂了。
*********************分析為原創,代碼部分原創 改編自柳婼******************/#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時說明當前進制大 應該往左找 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; }/** 沒用二分17分 一堆答案錯誤 因為我沒考慮好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 **/

?

轉載于:https://www.cnblogs.com/AbsolutelyPerfect/p/9154834.html

總結

以上是生活随笔為你收集整理的PAT Advanced Level 1010的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。