PTA 1027 Colors in Mars (20 分)燚

本文介绍了一种将RGB颜色从十进制转换为十三进制的算法,适用于火星上颜色表示的方法。通过连续除13取余并使用栈存储转换后的数值,确保了输出格式符合火星RGB值的要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

题目大意:颜色由 R G B 三个字符表示,每个字符对应一个十三进制数。现给出 RGB 三个十进制数,将他们转换位十三进制数

输出以#字符开始

思路:1.由于最后输出的有“#”字符,并且13进制涉数大于9的都用字母表示,因此选择用char 或string类型来存储。

           2.因为int类型转char类型比较麻烦,可将单个的char类型当作string类,所以选择string类型

           3.十进制转十三进制为连续除13取余并反向输出,为了方便可以用栈临时存储

注意事项: If a single color is only 1-digit long, you must print a 0 to its left.  如果转换的13进制数只有一位,高位用0补齐

#include<iostream>
#include<stack>
#include<vector>
#include<string>
using namespace std;
//将十进制数转换为13进制数
void changeNum(vector<string>&result, int n) {
	stack<int> s;//存储转换的13进制数
    //如果十进制数为0则将0压入栈中
	if (n == 0) {
		s.push(0);
	}
    //连续取余进制转换
	while (n != 0) {
		int temp = n % 13;
		s.push(temp);
		n /= 13;
	}
    //如果13进制数只有一位,则用零补齐
	if (s.size() == 1) {
		s.push(0);
	}
    //将13进制数转换为字符串
	while (!s.empty()) {
		if (s.top() > 9) {
			switch (s.top()) {
			case 10:result.push_back("A");break;
			case 11:result.push_back("B");break;
			case 12:result.push_back("C");break;
			}
			s.pop();
		}
		else {
			result.push_back(to_string(s.top()));
			s.pop();
		}
	}
}
int main() {
	vector<string>result;
    //3个十进制数,边输入边转换
	for (int i = 0;i < 3;i++) {
		int n;
		cin >> n;
		changeNum(result, n);
	}
    //输出
	cout << "#";
	for (int i = 0;i < result.size();i++) {
		cout << result[i];
	}
	cout << endl;
	return 0;
}

 

### 如何在指定位置输出字符串 为了实现在特定位置输出字符串的功能,可以考虑使用多种编程语言中的不同方法。这里将以 C++ 和 Python 为例展示两种实现方式。 #### 使用 C++ 在 C++ 中可以通过控制台输入/输出流来精确定位光标的打印位置。然而标准库并不直接支持这种操作,通常借助第三方库如 `ncurses` 或者 Windows 平台上特有的 API 函数 `_setcursortype()` 及 `_getch()`. 对于简单的命令行应用来说,也可以利用转义序列来进行基本的位置移动[^1]. 对于更复杂的图形界面程序,则可能涉及到 GUI 库的选择,比如 Qt、wxWidgets 等. ```cpp #include <iostream> using namespace std; void gotoxy(int x, int y){ printf("%c[%d;%df",0x1B,y,x); } int main(){ string str="Hello"; // 将光标定位到第3行第5列并打印字符串 gotoxy(5,3); cout<<str; } ``` 此段代码展示了如何通过 ANSI 转义码将终端上的光标移至 (5,3),然后在此处显示 "Hello". 需要注意的是这种方法依赖于终端解释这些特殊字符的能力,在某些环境下可能会失效。 #### 使用 Python Python 提供了一个叫做 `curses` 的模块用于创建基于文本的应用程序,它允许开发者轻松地管理屏幕布局以及响应键盘事件。如果只是简单的需求,还可以尝试使用 `os.system('cls')`(Windows) 或 `os.system('clear')`(Unix-like systems) 清屏后再重新绘制整个画面达到类似效果[^2]. 另外一种更为简便的方法是在字符串前加上 `\r\n` 来回车换行从而改变当前行号,但这仅限于在同一行内调整起始点: ```python import os def clear_screen(): _ = os.system('cls' if os.name=='nt' else 'clear') print(" "*4+"Start from here") # 向右偏移四个空格再开始写入文字 ``` 以上例子中,先清除了之前的输出内容,接着用多个空白符模拟横向位移的效果,使得新加入的内容看起来是从页面中间某一处开始书写的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值