PAT1027 Colors in Mars (20分) 10进制转13进制
题目
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
题目解读
简单说,就是给你3个10进制数字(0-168),输出一个"#"号,把他们都转成13进制(0-9,A-C)并输出,中间不要有空格,168也就是 CC,所以转换结果最多也就是 CC,宽度为2,但是要求转换结果只有1位的时候要前面补0,以2位的格式输出,并且字母只能是大写。(比如输出 #12A3BB)
思路
最核心的肯定就是把这个10进制的数(num)转成13进制,但是它最多只有两位,所以高位就是 num / 13,低位就是 num % 13,这不就是两个位置凑齐了??
还有个问题是,10-->A,11-->B,12-->C,所以用一个字符数组作为映射表就可以了。
比如 char c[14] = {"0123456789ABC"}, 然后把原本的输出 cout << num / 13 << num % 13 变成 cout << c[num / 13] << c[num % 13] 就搞定
代码
#include <iostream>
using namespace std;
int main() {
// 作为映射表
char c[14] = {"0123456789ABC"};
// cout << "#";
printf("#");
for(int i = 0; i < 3; ++i) {
int num;
// cin >> num;
scanf("%d", &num);
// 转成13进制,两位,高位是 / 13,地位是 % 13
cout << c[num / 13] << c[num % 13];
}
return 0;
}
PAT1027 Colors in Mars (20分) 10进制转13进制的更多相关文章
- PAT 甲级 1027 Colors in Mars (20 分)(简单,进制转换)
1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way ...
- PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642
PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...
- PAT Advanced 1027 Colors in Mars (20 分)
People in Mars represent the colors in their computers in a similar way as the Earth people. That is ...
- PAT (Advanced Level) Practice 1027 Colors in Mars (20 分)
People in Mars represent the colors in their computers in a similar way as the Earth people. That is ...
- 1027 Colors in Mars (20 分)
People in Mars represent the colors in their computers in a similar way as the Earth people. That is ...
- PAT1027. Colors in Mars (20)
#include <iostream> using namespace std; string tbl="0123456789ABC"; int main() { in ...
- 【PAT甲级】1027 Colors in Mars (20 分)
题意: 输入三个范围为0~168的整数,将它们从十三进制转化为十进制然后前缀#输出. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...
- A1027 Colors in Mars (20)(20 分)
A1027 Colors in Mars (20)(20 分) People in Mars represent the colors in their computers in a similar ...
- PAT1027:Colors In Mars
1027. Colors in Mars (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue People ...
随机推荐
- JDBC 工具类封装
每次使用jdbc 我们都要 加载驱动类 创建链接 创建Statement 接口对象执行sql 关闭资源 按照这样的套路可以封装一些重用代码方便在其他方法中调用 package com.xzlf.jdb ...
- 如何保证kafka消息不丢失
背景 这里的kafka值得是broker,broker消息丢失的边界需要对齐一下: 1 已经提交的消息 2 有限度的持久化 如果消息没提交成功,并不是broke丢失了消息: 有限度的持久化(broke ...
- Nginx+Fastdfs
注: 在配置时,使用非root用户配置 fdfs/fdfs 1. 集群部署 1.1. 准备 创建目录:本文档中所有内容安装到/fdfs目录 [fdfs@5861be93b5b0 /]$mk ...
- 如何打开 Visual Studio 的 Dump,适用于调试 appcrash,exception
https://keithbabinec.com/2018/06/12/how-to-capture-and-debug-net-application-crash-dumps-in-windows/ ...
- [Python] bytes 转换成 str
b = b"example" # bytes object s = "example" # str object sb = bytes(s, encoding ...
- QT 的 parent 该如何理解
对话框是GUI程序和用户进行简短交互的顶层窗口,所谓顶层窗口即始终在主窗口之上显示.QDialog是Qt所有类型的对话框窗口的基类,它继承于QWidget,是一种容器类型组件. QWidget是所有窗 ...
- 移动端rem适配&iOS兼容
移动端rem适配js // 默认375,750设计稿请将375替换为750 (function (doc, win) { // 移动端适配 var docEl = doc.documentElemen ...
- 5.Python是怎么解释的?
Python是怎么解释的? Python language is an interpreted language. Python program runs directly from the sour ...
- javascript 控制台调试方法
console在我们调试js程序的时候是一个非常有效的工具. 日志输出是我们最常用的功能: console.log(); console.info(); console.warn(); console ...
- cookie、session 和 token 区别
1.什么是 cookie cookie 是保存在本地终端的数据.cookie 由服务器生成,发送给浏览器,浏览器把 cookie 以 kv 形式保存到某个目录下的文本文件内,下一次请求同一网站时会把该 ...