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
#include <cstdio> int main()
{
int a, b, d;
scanf("%d%d%d", &a, &b, &d); int sum = a+b;
int ans[], num = ;
do {
ans[num++] = sum%d;
sum /= d;
} while(sum != ); for(int i=num-; i>=; --i)
printf("%d", ans[i]); return ;
}
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
#include <cstdio> #define MaxSize 50
int ans[MaxSize]; int main()
{
//freopen("E:\\Temp\\input.txt", "r", stdin); int N, b, num = ;
bool flag = true;
scanf("%d %d", &N, &b); do {
ans[num++] = N%b;
N /= b;
} while(N != ); for(int i=, j=num-; i<=(num-)/, j>=(num-)/; ++i, --j) {
if(ans[i] != ans[j]) {
flag = false;
break;
}
} if(flag == true) printf("Yes\n");
else printf("No\n");
for(int i=num-; i>=; --i) {
printf("%d", ans[i]);
if(i != ) printf(" ");
} return ;
}
#include <cstdio> bool judge(int z[], int num)
{
for(int i=; i<=num/; ++i) {
if(z[i] != z[num--i]) return false;
else return true;
}
} int main()
{
int n, b, z[], num = ;
scanf("%d%d", &n, &b); do {
z[num++] = n%b;
n /= b;
} while(n != );
bool flag = judge(z, num); if(flag == true) printf("Yes\n");
else printf("No\n");
for(int i=num-; i>=; --i) {
printf("%d", z[i]);
if(i != ) printf(" ");
} return ;
}
A1027. Colors in Mars (20)
Description:
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:
Each input file contains one test case which occupies a line containing the three decimal color values.
Output:
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 the left.
Sample Input:
15 43 71
Sample Output:
#123456
#include <cstdio>
char radix[] = {'', '', '', '', '', '', '', '', '', '', 'A', 'B', 'C'};
int main()
{
int r, g, b;
scanf("%d%d%d", &r, &g, &b);
printf("#");
printf("%c%c", radix[r/], radix[r%]);
printf("%c%c", radix[g/], radix[g%]);
printf("%c%c", radix[b/], radix[b%]);
return ;
}
A1058. A+B in Hogwarts (20)
Description:
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), and Knut is an integer in [0, 29)).
Input:
Each input file contains one test case which occupies a line with A and B in the standard form, separated by one space.
Output:
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 <cstdio> int main()
{
int a[], b[], c[];
scanf("%d.%d.%d %d.%d.%d", &a[], &a[], &a[], &b[], &b[], &b[]); int carry = ;
c[] = (a[]+b[])%;
carry = (a[]+b[])/;
c[] = (a[]+b[]+carry)%;
carry = (a[]+b[]+carry)/;
c[] = a[]+b[]+carry; printf("%d.%d.%d", c[], c[], c[]); return ;
}
PAT/进制转换习题集的更多相关文章
- PAT甲级 进制转换题_C++题解
进制转换题 PAT (Advanced Level) Practice 进制转换题 目录 <算法笔记> 重点摘要 1015 Reversible Primes (20) 1019 Gene ...
- SQL Server 进制转换函数
一.背景 前段时间群里的朋友问了一个问题:“在查询时增加一个递增序列,如:0x00000001,即每一个都是36进位(0—9,A--Z),0x0000000Z后面将是0x00000010,生成一个像下 ...
- [No000071]C# 进制转换(二进制、十六进制、十进制互转)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- JS中的进制转换以及作用
js的进制转换, 分为2进制,8进制,10进制,16进制之间的相互转换, 我们直接利用 对象.toString()即可实现: //10进制转为16进制 ().toString() // =>&q ...
- 结合stack数据结构,实现不同进制转换的算法
#!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...
- 进制转换( C++字符数组 )
注: 较为简便的方法是用 整型(int)或浮点型(long.double 注意:该类型不一定能够准确存储数据) 来存放待转换的数值,可直接取余得到每一位数值 较为稳定的方法是用 字符数组储存待转换的数 ...
- JS 进制转换
十进制转换成其他进制 objectname.toString([radix]) objectname 必选项.要得到字符串表示的对象. radix 可选项.指定将数字值转换为字符串时的进制. 例如 ...
- php的进制转换
学习了php的进制转换,有很多的知识点,逻辑,也有最原始的笔算,但是我们还是习惯使用代码来实现进制的转换,进制的转换代码有如下:二进制(bin)八进制( oct)十进制( dec)十六进制( hex) ...
- C++ 中数串互转、进制转换的类
/******************************************************************** created: 2014/03/16 22:56 file ...
随机推荐
- HR开发 获取信息类型数据
1.PNP逻辑数据库. LOOP获取信息类型数据. TABLES: PERNR . , . START-OF-SELECTION. GET PERNR . LOOP AT P0000 WHERE .. ...
- nerual style 执行命令
python neural_style.py --content ./examples/4-content.jpg --styles ./examples/4-faguo-style.jpg --ou ...
- Android进程间的通信之AIDL
Android服务被设计用来执行很多操作,比如说,可以执行运行时间长的耗时操作,比较耗时的网络操作,甚至是在一个单独进程中的永不会结束的操作.实现这些操作之一是通过Android接口定义语言(AIDL ...
- svn ignore
工程名为simple,采用maven进行依赖管理,在check in时我不想工程下maven产生的target目录被提交到SVN(包括目录下所有文件和目录本身). 解决方法: 要被忽略的目录必须是未版 ...
- Android框架之AndroidAnnotations详细讲解
一: (1)一个activity如过使用AndroidAnnotions注入时, 那么它在 AndroidManifest.xml注册时,应该加入_ 比如: MainActivity的注册时 < ...
- mysql日志 解析
mysql有4种不同的日志,分别是二进制日志,查询日志,慢查询日志和错误日志,这些日记记录着数据库工作的方方面面,可以帮助我们了解数据库的不同方面的踪迹,下面先介绍二进制日志的作用和使用方法,并利用二 ...
- lua string的自定义分割字符串接口
-------------------------------------------------------------------- -- Create By SunC 2014/7/1 -- ...
- 设计模型MVC和JavaBean
六.设计模型1和设计模型2(MVC)1.模型1:JSP+JavaBean2.模型2:MVC M:Model模型 JavaBean V:视图 JSP C:控制器 Servlet 七.模型1开发一个简单的 ...
- C++基础知识易错点总结(1)
1. 在C++中,不能被重载的运算符有: sizeof . 成员运算符 .* 成员指针运算符 :: 作用域运算符 ?: 条件运算符 2. C++语言多态性:编译时多态和运行时多态: 编译时多态可通过函 ...
- Socket通信基本原理
Http通信: http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户端向服务器发送请求后,服务器端才能向客户端返回数据. Socket通信: Socket通信则是在双方建立起连接后就 ...