第3章 进位制

A1010


#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;    //这里就得设置成 long long


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


LL calc(string n, LL r)             //计算给出的数的进制的10进制
{
    LL res = 0;
    for (auto c : n)
    {     //注意这里用double类型,不用就是扣4分
        if ((double)res * r + get(c) > 1e16) return 1e18;
        res = res * r + get(c);
    }
    return res;

}

//接下来看下数字的细节

int main()
{
    string n1, n2;
    cin >> n1 >> n2;
    int tag, radix;
    cin >> tag >> radix;

    if (tag == 2) swap(n1, n2);

    LL target = calc(n1, radix);            //计算目标值


    LL l = 0, r =target;               //l  r现在是左右 边界

    //本题中l是2 r为6
    for (auto c : n2) l = max(l, (LL)get(c) + 1);      //去找到n2的 最小的进制数,比如110的最小进制数就是2,其实找到的是1,然而是2进制;比如ab 是1011,则找到的值是11,所以进制数为12


    while (l < r)         //2分查找法
    {                              //>>1表示除以2   
        LL mid = l + r >> 1;          //计算他的一个中位点
        if (calc(n2, mid) >= target) r = mid;
        else l = mid + 1;
    }

    if (calc(n2, l) != target) puts("Impossible");
    else cout << l << endl;

    return 0;
}

A1015

在这里插入图片描述


#include <iostream>

using namespace std;

typedef long long LL;       //注意这是long long类型

bool is_prime(int n)        //判断素数的函数
{
    if (n == 1) return false;

    for (int i = 2; i * i <= n; i ++ )
        if (n % i == 0)
            return false;
    return true;
}

bool check(int n, int d)
{
    if (!is_prime(n)) return false;          //首先如果n不是素数,直接返回false

    LL r = 0;
    while (n)
    {
        r = r * d + n % d;              //首先这是用了秦九昭算法,算d进制的数,n%d算的是d进制的最后一位,再算翻转的时候,算的就是第1位
        n /= d;
    }

    return is_prime(r);
}

int main()
{
    int n, d;
    while (cin >> n >> d, n >= 1)               //n是负数就不行
    {
        if (check(n, d)) puts("Yes");
        else puts("No");
    }

    return 0;
}

A1027


/*
    注意这道题中,只有两位数字,进制是13,所以第1为就是直接除,第2位是求余

*/

#include<iostream>
using namespace std;

char get(int x)          //将x改为字符
{
    if(x<=9)
        return '0'+x;
    else
        return 'A'+x-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);     //总共就2位,第一位就是除数,第2为就是余数
    }
    return 0;
}

A1100



#include<iostream>
#include<sstream>
using namespace std;

//注意n的值,最多是169,也就是最多两个字符串

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 s)
{
    for(int i=0; i<25; i++)
        if(names[i]==s)
        {
            if(i<13)
                return i;
            else
                return (i-12)*13;
        }
}

int main()
{
    int n;
    cin >> n;
    getchar();
    while(n--)
    {
        string line;
        getline(cin,line);
        stringstream ssin(line);
        if(line[0]<='9')       //表示为数字,需要转化成字符,如果是一个数字,那就分为小于13的和大于13的,小于13的直接输出,大于13的先输出除数对应的字符,再输出余数对应的字符,如果此时整除,则直接换行
        {
            int v;          //v就是line中的数字
            ssin >> v;
            if(v<13)
                cout << names[v] << endl;
            else
            {
                cout << names[12+v/13];
                if(v%13==0)
                    cout << endl;
                else
                    cout << " " << names[v%13] << endl;
            }
        }
        else            //就是 字符串
        {
            int res=0;
            string w;
            while(ssin>>w)
                res+=get(w);
            cout << res << endl;

        }
    }
    
    return 0;
}

A1019


#include<iostream>
#include<vector>
using namespace std;

vector<int> nums;

bool check()
{
    for(int i=0,j=nums.size()-1; i<j; i++,j--)        //通过双指针,去进行判断
        if(nums[i]!=nums[j])
            return false;
    return true;
}

int main()
{
    int n,radix;
    cin >> n >> radix;
    while(n)
    {
        nums.push_back(n%radix);
        n/=radix;
    }

    if(check())
        cout << "Yes" << endl;
    else
        cout << "No" << endl;

    cout << nums.back();        //这个表示的是nums中的最后一位

    for(int i=nums.size()-2; i>=0; i--)
        cout << " " << nums[i];

    return 0;


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值