PATA 打卡记录 进位制

本文探讨了不同进制之间的转换算法,包括从十进制到其他进制的转换,以及如何判断一个数在特定进制下是否为回文数。此外,还涉及了质数检测和可逆质数的概念,以及在火星历法中数字的表示方法。通过这些题目,展示了计算机科学中数论和算法的基础应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ACwing PATA 打卡记录

第三章 进位制

1.PATA 1010 进制

主要考察二分思想,注意数据范围可能爆long long
//二分满分做法(测试点巨坑)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL zhuanhuan(char x[]);
LL panduan(char x[],LL mid);
char a[11],b[11];
LL c,d;
LL e,f,flag;
LL l,r;
LL j=2;
LL res;
int main(){
	scanf("%s%s%lld%lld",a,b,&c,&d);
	int maxi=0;
	if(c==1){
		int i=0;
		for(i=0;i<strlen(b);i++){
			if(b[i]>='0'&&b[i]<='9'){
				if(b[i]-'0'>=maxi){
					maxi=b[i]-'0';
				}
			}else{
				if(b[i]-'a'>=maxi){
					maxi=b[i]-'a';
				}
			}
		}
		l=maxi+1;//b的进制一定大于它数字内容里最大的数,比如b中含有数字3,它最小是个4进制
		r=1e18+10;
		e=zhuanhuan(a);
		while(l<r){
			LL mid=(l+r)/2;
			if(panduan(b,mid)>=0){
				r=mid;
				res=mid;
			}else{
				l=mid+1;
			}
		}
		if(panduan(b,res)==0){
			printf("%lld",res);
		}else{
			printf("Impossible");
		}
	}else if(c==2){
		swap(a,b);
		int i=0;
		for(i=0;i<strlen(b);i++){
			if(b[i]>='0'&&b[i]<='9'){
				if(b[i]-'0'>=maxi){
					maxi=b[i]-'0';
				}
			}else{
				if(b[i]-'a'>=maxi){
					maxi=b[i]-'a';
				}
			}
		}
		l=maxi+1;
		r=1e18+10;
		e=zhuanhuan(a);
		while(l<r){
			LL mid=(l+r)/2;
			if(panduan(b,mid)>=0){
				r=mid;
				res=mid;
			}else{
				l=mid+1;
			}
		}
		if(panduan(b,res)==0){
			printf("%lld",res);
		}else{
			printf("Impossible");
		}
	}
	return 0;
}
LL zhuanhuan(char x[]){
	LL ans=0;
	LL a=1;
	LL i=strlen(x)-1;
	for(i;i>=0;i--){
		if(x[i]>='0'&&x[i]<='9'){
			ans=(x[i]-'0')*a+ans;
			a*=d;
		}else{
			ans=(x[i]-'a'+10)*a+ans;
			a*=d;
		}
	} 
	return ans;
}
LL panduan(char x[],LL mid){
	LL ans=0;
	LL a=1;
	LL i=strlen(x)-1;
	for(i;i>=0;i--){
		if(x[i]>='0'&&x[i]<='9'){
			ans=(x[i]-'0')*a+ans;
			a*=mid;
		}else{
			ans=(x[i]-'a'+10)*a+ans;
			a*=mid;
		}
	} 
	if(ans<0) return 1; //测试数据有可能爆出long long
	if(ans>e) return 1;
	else if(ans==e) return 0;
	else return -1; 
}

2.PATA 1015 可逆质数

#include<bits/stdc++.h>
using namespace std;
bool is_prime(int x){
    if(x<2) return false;
    else{
        for(int i=2;i*i<=x;i++){
            if(x%i==0) return false;
        }
        return true;
    }
}
int zhuanhuan(int x,int y){
    int res=0;
    while(x){
        res=res*y+x%y;
        x/=y;
    }
    return res;
}
int main(){
    int m,n;
    while(cin>>m>>n,m>0){
        if(is_prime(m)){
            m=zhuanhuan(m,n);
            if(is_prime(m)){
                printf("Yes\n");
            }else{
                printf("No\n");
            }
        }else{
            printf("No\n");
        }
    }
    return 0;
}

3.PATA 1027 火星颜色

#include<bits/stdc++.h>
using namespace std;
char get(int x){
    if(x>=10) return x+'A'-10;
    else return x+'0';
}
int main(){
    int num[3];
    cout<<'#';
    for(int i=0;i<3;i++){
        cin>>num[i];
        cout<<get(num[i]/13)<<get(num[i]%13);
    }
    return 0;
}

4.PATA 1100 火星数字

#include <iostream>
#include <vector>
using namespace std;
vector<string> mapr = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
vector<string> mapl = {"","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};



string encode(int  a) {
    if (a <= 12) return mapr[a];
    string res;
    res += mapl[a/13];
    if (a % 13 != 0) {
        res += " " + mapr[a%13];
    }
    return res;
}

string decode(string a) {
    if (a.size() == 4) return "0";
    if (a.size() <= 3){
        for (int i = 1; i < 13; i ++ ) {
            if (a == mapr[i]) {
                return to_string(i);
            }
            if (a == mapl[i]) {
                return to_string(13*i);
            }
        }
    } else {
        int res = 0;
        string l, r;
        bool is_first = true;
        for (int i = 0; i < a.size(); i ++ ) {
            if (is_first && a[i] != ' ') {
                l.push_back(a[i]);
            } else if (a[i] != ' ') {
                r.push_back(a[i]);
            } else if (a[i] == ' ') {
                is_first = false;
            }
        }
        for (int i = 0; i < 13; i ++ ) {
            if (l == mapl[i]) {
                res += i * 13;
            }
            if (r == mapr[i]) {
                res += i;
            }
        }
        return to_string(res);
    }
    return "a";
}

string translate(string str){
    if (isdigit(str[0])) return encode(stoi(str));
    return decode(str);
}

int main() {
    int n; cin >> n;
    getchar();
    while (n--) {
        string line;getline(cin, line);
        cout << translate(line) << endl;
    }

}

5.PATA 1019 普通回文数

#include<bits/stdc++.h>
const int N=1e5+10;
int a[N];
using namespace std;
int main(){
	int n,b;
	scanf("%d%d",&n,&b);
	int c=0;
	while(n){
		a[c++]=n%b;
		n/=b;
	}
	int ans=1;
	for(int i=0;i<=c/2;i++){
		if(a[i]!=a[c-1-i]){
			ans=0;
			break;
		}
	}
	if(ans==1){
		printf("Yes\n");
	}else{
		printf("No\n");
	}
	if(c==0){
		printf("0\n");
	}
	for(int i=c-1;i>=0;i--){
        if(i!=0){
            printf("%d ",a[i]);
        }else{
            printf("%d",a[i]);
        }
    }
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值