当前位置:   article > 正文

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

进制转换pta

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/681808
推荐阅读
相关标签
  

闽ICP备14008679号