PTA-进制转换(1019/1027/1058)

PTA1019

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number N>0 in base b≥2, where it is written in standard notation with k+1 digits a​i as ∑​i=0 ​k(a​ib​i). Here, as usual, 0≤a​i<b for all i and a​k is non-zero. Then N is palindromic if and only if a​i=a​k−i for all i. Zero is written 0 in any base and is also palindromic by definition.

Given any positive decimal integer N and a base b, you are supposed to tell if N is a palindromic number in base b.

Input Specification:
Each input file contains one test case. Each case consists of two positive numbers N and b, where 0<N≤10^9 is the decimal number and 2≤b≤ 10^9 is the base. The numbers are separated by a space.

Output Specification:
For each test case, first print in one line Yes if N is a palindromic number in base b, or No if not. Then in the next line, print N as the number in base b in the form "a^k a^k−1… a 0​ ". Notice that there must be no extra space at the end of output.

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

思路

将十进制数m转化为n进制数,判断转化后的n进制数是不是一个回文序列,并按位输出转化后的每一位。

#include<bits/stdc++.h>
using namespace std;
bool pan(vector<int> &a)
{
	int size=a.size();
	for(int i=0;i<size/2;i++)
	{
		if(a[i]!=a[size-i-1])
		{
			return false;
		}
	}
	return true;
}
int main()
{
	int m,n;
	cin>>m>>n;
	vector<int >a;
	do
	{
		a.push_back(m%n);
		m=m/n;
	}while(m>=1);
	reverse(a.begin(),a.end());
	if(pan(a)) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
	for(int i=0;i<a.size();i++)
	{
		cout<<a[i];
		if(i!=a.size()-1) cout<<' ';
	}
	cout<<endl;
	return 0;
}

PTA1027

People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only difference is that they use radix 13 (0-9 and A-C) instead of 16. Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.

Input Specification:

Each input file contains one test case which occupies a line containing the three decimal color values.

Output Specification:

For each test case you should output the Mars RGB value in the following format: first output #, then followed by a 6-digit number where all the English characters must be upper-cased. If a single color is only 1-digit long, you must print a 0 to its left.

Sample Input:

15 43 71

Sample Output:

#123456

思路

将十进制数转化成十三进制数输出即可,不足两位的要补0

#include<bits/stdc++.h>
using namespace std;
void change(int x)
{
	vector<char> a;
	do
	{
		if(x%13==10) a.push_back('A');
		else if(x%13==11) a.push_back('B');
		else if(x%13==12) a.push_back('C');
		else a.push_back(x%13+48);
		x=x/13;
	}while(x>=1);
	reverse(a.begin(),a.end());
	if(a.size()<2) cout<<'0';
	for(int i=0;i<a.size();i++)
	{
		printf("%c",a[i]);
	}
}
int main()
{
	int a,b,c;
	cin>>a>>b>>c;
	cout<<'#';
	change(a);
	change(b);
	change(c);
	cout<<endl;
	return 0;
}

PTA1058

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 B are 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)).

Input Specification:

Each input file contains one test case which occupies a line with A and B in the standard form, separated by one space.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input.

Sample Input:

3.2.1 10.16.27

Sample Output:

14.1.28

思路

各位满足进制相加,进位运算即可

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c;
	scanf("%d.%d.%d",&a,&b,&c);
	int x,y,z;
	scanf("%d.%d.%d",&x,&y,&z);
	long long int q,w,e;
	e=(c+z)%29;
	w=(b+y+(c+z)/29)%17;
	q=(a+x)+(b+y+(c+z)/29)/17;
	printf("%lld.%lld.%lld",q,w,e); 
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新西兰做的饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值