PAT/进制转换习题集

B1022. D进制的A+B (20)

Description:

输入两个非负10进制整数A和B(<=230-1),输出A+B的D (1 < D <= 10)进制数。

Input:

输入在一行中依次给出3个整数A、B和D。

Output:

输出A+B的D进制数。

Sample Input:

123 456 8

Sample Output:

1103

 1 #include <cstdio>
 2
 3 int main()
 4 {
 5     int a, b, d;
 6     scanf("%d%d%d", &a, &b, &d);
 7
 8     int sum = a+b;
 9     int ans[31], num = 0;
10     do {
11         ans[num++] = sum%d;
12         sum /= d;
13     } while(sum != 0);
14
15     for(int i=num-1; i>=0; --i)
16         printf("%d", ans[i]);
17
18     return 0;
19 }

A1019. General Palindromic Number (20)

Description:

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 the sum of (aibi) for i from 0 to k. 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 non-negative decimal integer N and a base b, you are supposed to tell if N is a palindromic number in base b.

Input:

Each input file contains one test case. Each case consists of two non-negative 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:

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 "ak ak-1 ... a0". Notice that there must be no extra space at the end of output.

Sample Input1:

27 2

Sample Output1:

Yes
1 1 0 1 1

Sample Input2:

121 5

Sample Output2:

No

4 4 1

 1 #include <cstdio>
 2
 3 #define MaxSize 50
 4 int ans[MaxSize];
 5
 6 int main()
 7 {
 8     //freopen("E:\\Temp\\input.txt", "r", stdin);
 9
10     int N, b, num = 0;
11     bool flag = true;
12     scanf("%d %d", &N, &b);
13
14     do {
15         ans[num++] = N%b;
16         N /= b;
17     } while(N != 0);
18
19     for(int i=0, j=num-1; i<=(num-1)/2, j>=(num-1)/2; ++i, --j) {
20         if(ans[i] != ans[j]) {
21             flag = false;
22             break;
23         }
24     }
25
26     if(flag == true)    printf("Yes\n");
27     else printf("No\n");
28     for(int i=num-1; i>=0; --i) {
29         printf("%d", ans[i]);
30         if(i != 0)  printf(" ");
31     }
32
33     return 0;
34 }
 1 #include <cstdio>
 2
 3 bool judge(int z[], int num)
 4 {
 5     for(int i=0; i<=num/2; ++i) {
 6         if(z[i] != z[num-1-i])  return false;
 7         else return true;
 8     }
 9 }
10
11 int main()
12 {
13     int n, b, z[40], num = 0;
14     scanf("%d%d", &n, &b);
15
16     do {
17         z[num++] = n%b;
18         n /= b;
19     } while(n != 0);
20     bool flag = judge(z, num);
21
22     if(flag == true)    printf("Yes\n");
23     else printf("No\n");
24     for(int i=num-1; i>=0; --i) {
25         printf("%d", z[i]);
26         if(i != 0)  printf(" ");
27     }
28
29     return 0;
30 }
时间: 2024-08-25 00:00:13

PAT/进制转换习题集的相关文章

1027. Colors in Mars (20)【进制转换】——PAT (Advanced Level) Practise

题目信息 1027. Colors in Mars (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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 Re

任意进制转换算法

任意进制转换算法 N年没有写博客,发个进制转换的玩下,支持负数:功能属于简化版的 Convert.ToInt32 ,特点是: 1.任意位,如:0,1(二进制),0...7(八进制),0...9,A...F(16进制),0...N(N进制),或者是:[email protected]#$%^&*(8进制,字符符号),也可以是中文. 2.8 byte 最大长度. 3.C#源码. 最近写markdown格式习惯啦,cnblogs啥时候全改掉算了,别用这个htmleditor算了. 先说明下进制转换的基

03 php 数据类型:整数,进制转换,浮点,字符,布尔,数组,空类型,类型转换,算术运算,比较运算

03 数据类型:整数,进制转换,浮点,字符,布尔,数组,空类型,类型转换, 算术运算,比较运算,逻辑运算,短路现象, 三目运算符,字符型运算: 数据类型 整体划分 标量类型: int, float, string, bool 复合类型: array,     object 特殊类型: null,     resouce 整数类型int, integer 3种整数表示法 十进制写法:123: $n1 = 123; 八进制写法: 0123 $n2 = 0123; 十六进制写法: 0x123 $n3

计算机进制转换

一.计算机只认识0和1,二进制. 二.2进制转换成 8进制 和 16进制,如下图: 二进制 > 八进制 :  研究上图发现,3位最高二进制可以用来表示一位八进制.所以,将二进制分解每3位,不够前面补0,然后每3位转换为10进制,顺序排列即可. 二进制 > 十六进制  :4位最高二进制可以用来表示一位十六进制.所以,将二进制分解每4位,不够前面补0,然后每4位转换为10进制,超过9用字母表示即可.顺序排列即可. 如下: 二进制 > 十进制:   11001001 = 2^7+2^6+2^3

原理之一,进制转换

原理之一,进制转换 日常生活中采用个数字都是十进制,而计算机采用的是运算更简单.易实现且可靠,为逻辑设计提供了有力途经的二进制,除此之外还有八进制和十六进制作为二进制的缩写. 进制:逢N进一,N是每种进位计数制表示一位数所需要的符号数目为基数. 二进制:逢二进一,借一当二,包含的数字(0.1) 八进制:逢八进一,借八当一,包含(0.1.2.3.4.5.6.7) 十六进制:逢十六当一,以一当十六,包含(0.1.2.3.4.5.6.7.8.9.10(A).11(B).12(C).13(D).14(E

NOIP2000 进制转换

题一   进制转换              (18分)  问题描述      我们可以用这样的方式来表示一个十进制数: 将每个阿拉伯数字乘以一个以该数字所处位置的(值减1)为指数,以10为底数的幂之和的形式.例如:123可表示为 1*102+2*101+3*100这样的形式. 与之相似的,对二进制数来说,也可表示成每个二进制数码乘以一个以该数字所处位置的(值-1)为指数,以2为底数的幂之和的形式.一般说来,任何一个正整数R或一个负整数-R都可以被选来作为一个数制系统的基数.如果是以R或-R为基

进制进制进制~转换

从刚学计算机器就对进制转换有着莫名的反感,2进制 8进制 10进制 16进制各种转换. 下面就说下逻辑地址转换成物理地址的求法吧 首先,用户输入一个16进制的数字cin>>hex>>logic_add;   hex的意思是告诉计算机输入的数是以16进制方式输入的 这个时候你要是输出cout<<logic_add;  你会发现输出的是把这个16进制的数转换为10进制以后输出的结果 cout<<hext<<logic_add;这样输出的才是16进制.

黑马程序员------进制转换

------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训..Net培训</a>.期待与您交流! ------- 在java中数字的表现形式一般有二进制,八进制,十进制,十六进制等,在平时的编程中我们可以通过java提供的API函数方便的实现各个进制间的转换,如:Integer.toHexString(int i)--十进制转十六进制:Integer

c语言之进制转换(栈实现)

从上两篇博客中我们可以知道,栈具有后进先出的特性,而进制转换的打印输出刚好与计算过程相反,满足栈这后进先出的特性, 所以可以用栈很快的实现进制转换,下面是用栈实现进制转换的c函数 void conversion (SqStack *pstack,unsigned int N, const unsigned int d){ if( pstack == NULL)//当传入参数为指针,必须判空 exit(-1); int mod ;//保存mod = N %d while( N != 0){ mod