栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 
#include 
#include 

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 
#include 
#include 

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 
#include 
#include 

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 
#include 
#include 
#include 

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 
#include 
#include 
#include 

using namespace std;

bool check(vector 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 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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/990092.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号