神奇的编码
来源:互联网 发布:java生成6位邀请码 编辑:程序博客网 时间:2024/05/15 04:29
Problem E: 神奇的编码
Description
假如没有阿拉伯数字,我们要怎么表示数字呢
小明想了一个方法如下:
1 -> A
2 -> B
3 -> C
....
25 -> Y
26 -> Z
27 -> AA
28 -> AB
....
现在请你写一个程序完成这个转换
Input
输入的第一个数为一个正整数T,表明接下来有T组数据。
每组数据为一个正整数n ( n <= 1000)
Output
对于每个正整数n,输出他对应的字符串
Sample Input
3
1
10
27
Sample Output
A
J
AA
暴力解法:以要求的26进制从1数到MAXN。
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;typedef long long LL;const int MAXN = 1005;const int INF = 0xfffffff;char a[MAXN][10];int head, tail;void Updata(int i){int p = 0;do{a[i][p] = 'A';p++;//向下一位进1if (a[i][p]) a[i][p]++;else a[i][p] = 'A';} while (a[i][p]>'Z');}void solve(){memset(a, 0, sizeof(a));char cur = 'A';for (int i = 1; i<MAXN; i++, cur++){strcpy(a[i], a[i - 1]);if (cur <= 'Z'){a[i][0] = cur;}else{cur = 'A';Updata(i);}}}int main(){//freopen("E:\\input.txt","r",stdin);int T;scanf("%d", &T);solve();while (T--){int n;scanf("%d", &n);int len = strlen(a[n]);for (int i = len - 1; i >= 0; i--)printf("%c", a[n][i]);printf("\n");}return 0;}
稍规范的解法:
#include <iostream>#include <algorithm>#include <cstdio>#include <string>#include <cstring>using namespace std;typedef long long LL;const int MAXN = 1005;const int INF = 0xfffffff;int n;string ans;void solve(){ans = "";do{int r = n % 26;//取余n = n / 26;if (r == 0){//如果整除,则向前借1位r = 26;n--;}ans =(char) (r + 'A' - 1) + ans;//余数拼接到ans前面} while (n > 0);cout << ans << endl;}int main(){//freopen("E:\\input.txt","r",stdin);int T;scanf("%d", &T);while (T--){scanf("%d", &n);solve();}return 0;}
0 0
- 神奇的编码
- 神奇的编码
- 神奇的编码 【进制转换】
- 神奇的世界—读《编码-隐匿在计算机软硬件背后的语言》有感
- QR code 解码/编码 没有那么神奇
- 神奇的的调色板
- 神奇的*号
- 一组神奇的图片
- 神奇的一天
- 神奇的预言
- 神奇的jsfl!
- 神奇的VIM
- 神奇的猴子Mono
- 神奇的xx宏
- 神奇的视觉艺术
- 神奇的上班路上
- 神奇的视觉艺术
- 神奇的 DataGrid
- java 操作日志 log
- 抽象数据类型Triplet的表示和实现
- PLSQL collection 示例 之 varray
- VirtualBox 虚拟机不能访问的原因
- プロキシサーバにSSLモジュールの追加
- 神奇的编码
- EXT2 file system namei.c with my comments
- js 实现 table 行上移 (数据库)
- RubyGems简介和使用 ruby+gem常用命令
- 解锁
- Hibernate之三 调用存储过程、触发器、数据库的隔离级别
- 深度解读django class base view 之 detaiview
- RedHat 8.0/9.0 LVS安裝手冊
- 类1(1)