PAT_(进制转换) A1019 A1027 A1058

目录

1019 General Palindromic Number (20分)

1027 Colors in Mars (20分)

1058 A+B in Hogwarts (20分)


 

1019 General Palindromic Number (20分)

Sample Input 1:

27 2

Sample Output 1:

Yes
1 1 0 1 1

Sample Input 2:

121 5

Sample Output 2:

No
4 4 1

解释:27转换成二进制=11011,是回文数,输出Yes

#include<iostream>
using namespace std;
int N,c;
int a[1000];//倒序
int b[1000];//正序
void CheckNum(){
    int n,m,temp;
    int h1=0; n=N;
    while(n){
        temp=n%c;
        a[h1++]=temp;
        n/=c;
    }
    int h2=h1,u=0;
    for(int i=h1-1;i>=0;i--){
        b[u++]=a[i];
    }
    int flag=0;
    for(int i=0;i<h2;i++){
        if(a[i]!=b[i]) {
            flag=1; break;
        }
    }
    if(flag)
        cout<<"No\n";
    else
        cout<<"Yes\n";

    for(int j=0;j<h2-1;j++)
        cout<<b[j]<<" ";
    cout<<b[h2-1]<<endl;


}
int main(){
    cin>>N>>c;
    CheckNum();
    return 0;
}

1027 Colors in Mars (20分)

Sample Input:

15 43 71

Sample Output:

#123456

解释:把r,g,b三个10进制数字转化为 13进制的数

#include<cmath>
#include<iostream>
#include <cstdio>
#include <stdlib.h>
#include <algorithm>
using namespace std;
char radix[13]={'0','1','2','3','4','5','6','7','8','9','A','B','C'};

int main(){
	int r,g,b;
	scanf("%d%d%d",&r,&g,&b);
	printf("#");
	printf("%c%c",radix[r/13],radix[r%13]);
	printf("%c%c",radix[g/13],radix[g%13]);
	printf("%c%c",radix[b/13],radix[b%13]);
	return 0;
}

 

1058 A+B in Hogwarts (20分)

If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- as Hagrid explained it to Harry, "Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it's easy enough." Your job is to write a program to compute A+B where A and Bare given in the standard form of Galleon.Sickle.Knut (Galleon is an integer in [0,10​7​​]Sickle is an integer in [0, 17), and Knut is an integer in [0, 29)) 注意区间开闭

Sample Input:

3.2.1 10.16.27

Sample Output:

14.1.28

解释:从低位到高位,以此满足a+b满29进1,满17进1,满108进1;

#include<cmath>
#include<iostream>
#include <cstdio>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int main(){
	int a[3],b[3],c[3];
	scanf("%d.%d.%d %d.%d.%d",&a[0],&a[1],&a[2],&b[0],&b[1],&b[2]);
	int carry=0; //进位
	c[2]=(a[2]+b[2])%29; carry=(a[2]+b[2])/29;
	c[1]=(a[1]+b[1]+carry)%17; carry=(a[1]+b[1]+carry)/17;
	c[0]=(a[0]+b[0])+carry;
	printf("%d.%d.%d",c[0],c[1],c[2]);	
	return 0;	
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值