十进制转换d进制
关键代码
int num,d;//十进制数,进制数 int a[n];//设置数组存放转换后的数 do{ a[i++]=num%d; num/=d; }while(num!=0); for(k=i-1;k>=0;k--)//转换后的数组逆序 printf("%d",k);
例题1(PAT A1019)
1019 General Palindromic Number (20 分)
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 ai as ∑i=0k(aibi). Here, as usual, 0≤ai<b for all i and ak is non-zero. Then N is palindromic if and only if ai=ak−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≤109 is the decimal number and 2≤b≤109 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, orNo
if not. Then in the next line, print N as the number in base b in the form "ak ak−1 ... a0". 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
#include<stdio.h>
#define NUM 30
int main()
{
int num,d,i=0,k;
int a[NUM];
scanf("%d%d",&num,&d);
do{
a[i++]=num%d;
num/=d;
}while(num!=0);
if(judge(a,i))
printf("Yes\n");
else
printf("No\n");
for(k=i-1;k>=0;k--){
printf("%d",a[k]);
if(k!=0)
printf(" ");
}
}
int judge(int a[],int n)
{
int i,flag=1;
for(i=0;i<n/2;i++)
{
if(a[i]!=a[n-1-i])
flag=0;
}
return flag;
}
例题2(PAT A1027)
1027 Colors in Mars (20 分)
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 forGreen
, and the last 2 digits forBlue
. 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 a0
to its left.Sample Input:
15 43 71
Sample Output:
#123456
#include<stdio.h>
#define NUM 30
int main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("#");
reverse(a);
reverse(b);
reverse(c);
}
void reverse(int n)
{
char jinzhi[13]={'0','1','2','3','4','5','6','7','8','9','A','B','C'};
int a[NUM],i=0,k;
do{
a[i++]=n%13;
n/=13;
}while(n!=0);
if(i==1)
printf("0");
for(k=i-1;k>=0;k--){
printf("%c",jinzhi[a[k]]);
}
}
例题3(PAT A1058)
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 B are given in the standard form of
Galleon.Sickle.Knut
(Galleon
is an integer in [0,107],Sickle
is an integer in [0, 17), andKnut
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<stdio.h>
#define NUM 30
int main()
{
int a1,a2,a3,b1,b2,b3;
int a,b,c;
scanf("%d.%d.%d%d.%d.%d",&a1,&a2,&a3,&b1,&b2,&b3);
c=(b3+a3)%29;
b=(a2+b2+(b3+a3)/29)%17;
a=a1+b1+(a2+b2+(b3+a3)/29)/17;
printf("%d.%d.%d",a,b,c);
}