PAT甲级真题:进位制系列题解

题目集:

1010 Radix
1015 Reversible Primes
1027 Colors in Mars
1100 Mars Numbers
1019 General Palindromic Number

知识点总结:

(1)d 进制下的 n 转 十进制模板 (秦九韶算法)

int get(char c)
{
    if (c >= '0' && c <= '9')   return c - '0';
    else    return c - 'a' + 10;
}

LL cal(string n, LL radix)
{
    LL res = 0;
    for (auto item : n)
    {
        if ((double)res * radix + get(item) > 1e16) return 1e18;
        res = res * radix + get(item);
    }
    return res;
}

题解:

题目:1010 Radix

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;


int get(char c)
{
    if (c >= '0' && c <= '9')   return c - '0';
    else    return c - 'a' + 10;
}

LL cal(string n, LL radix)
{
    LL res = 0;
    for (auto item : n)
    {
        if ((double)res * radix + get(item) > 1e16) return 1e18;
        res = res * radix + get(item);
    }
    return res;
}

int main( )
{
    string n1, n2;
    int tag, radix;
    cin >> n1 >> n2 >> tag >> radix;
    
    // 将已经给出进制的数放到第一位
    if (tag == 2) swap(n1, n2);
    
    LL target = cal(n1, radix);     // 将radix进制的数 n1 转为十进制
    
    
    LL l = 0, r = max(target, 36ll);
    for (auto item : n2)    // 找出n2可能的最大进制数
        l = max(l, (LL)get(item) + 1);
    
    // 二分
    while (l < r)
    {
        LL mid = l + r >> 1;
        if(cal(n2, mid) >= target)  r = mid;
        else l = mid + 1;
    }
    
    if (cal(n2, l) != target)   puts("Impossible");
    else    cout << r << endl;
    
    return 0;
}

题目:1015 Reversible Primes

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;



// 试除法判断质数
bool is_prime(int n)
{
    if (n == 1) return false;
    
    for (int i = 2; i <= n / i; i ++)
        if (n % i == 0)
            return false;
            
    return true;
}

bool check(int n, int d)
{
    if (!is_prime(n))  return false;
    
    LL ans = 0;
    while (n)
    {
        ans = ans * d + n % d;
        n /= d;
    }
    
    return is_prime(ans);
    
}

int main()
{
    int n, d;
    while (cin >> n >> d, n >= 1)
    {
        if (check(n, d)) puts("Yes");
        else puts("No");
        
    }

    return 0;
}

题目:1027 Colors in Mars

AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

char get(int n)
{
    if (n <= 9) return '0' + n;
    else    return 'A' + n - 10;
}

int main( )
{
    int a[3];
    for (int i = 0; i < 3; i ++)    cin >> a[i];
    
    cout << "#";
    
    for (int i = 0; i < 3; i ++)    cout << get(a[i] / 13) << get(a[i] % 13);
    
    return 0;
}

题目:1100 Mars Numbers

AC代码:

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>

using namespace std;

char names[][5] = {
    "tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec",
    "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou",
};

int get(string word)
{
    for (int i = 0; i < 25; i ++)
        if (names[i] == word)
        {
            if (i < 13) return i;
            else
                return 13 * (i - 12);
        }
    return 0;
}


int main( )
{
    int n;
    cin >> n;
    getchar();  // 要读整行需要将第一行末尾的换行读掉
    while(n --)
    {
        string line;
        getline(cin, line);
        
        stringstream ssin(line);
        if(line[0] <= '9')    // 读入的是数字
        {
            int num;
            ssin >> num;
            if (num < 13)   cout << names[num] << endl;
            else
            {
                cout << names[num / 13 + 12];
                if (num % 13 == 0)  cout << endl;
                else cout << ' ' << names[num % 13] << endl;
            }
        }
        else    // 读入的是单词
        {
            int res = 0;
            string word;
            while (ssin >> word) res += get(word);
            cout << res << endl;
        }
    }
    return 0;
}

题目:1019 General Palindromic Number

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

bool check(vector<int> a)
{
    for (int i = 0, j = a.size() - 1; i < j; i ++, j --)
    {
        if (a[i] != a[j])
            return false;
    }
    return true;
}

int main( )
{
    int n, d;
    cin >> n >> d;
    
    vector<int> a;
    if (n == 0) a.push_back(n);
    while (n)
    {
        a.push_back(n % d);
        n /= d;
    }
    if (check(a))   cout << "Yes" << endl;
    else    cout << "No" << endl;
    
    
    
    cout << a[a.size() - 1];
    for (int i = a.size() - 2; i >= 0 ; i --)
        cout << ' ' << a[i];
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值