一个萌新的成长之路

Background

  • 同学们都回家了,只有我和wjh还有邢神在机房敲代码,吃random口味的方便面……

Description

Translated by @PC_DOS from luogu

  • In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.
  • The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.
  • Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.
  • Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
  • 人们常用的电子表格软件(比如: Excel)采用如下所述的坐标系统:
  • 第一列被标为A,第二列为B,以此类推,第26列为Z。接下来为由两个字母构成的列号: 第27列为AA,第28列为AB...在标为ZZ的列之后则由三个字母构成列号,如此类推。
    行号为从1开始的整数。
  • 单元格的坐标由列号和行号连接而成。比如,BC23表示位于第55列23行的单元格。
    有时也会采用被称为RXCY的坐标系统,其中X与Y为整数,坐标(X,Y)直接描述了对应单元格的位置。比如,R23C55即为前面所述的单元格。
  • 您的任务是编写一个程序,将所给的单元格坐标转换为另一种坐标系统下面的形式。

Input&Output

Input Format:

  • The first line of the input contains integer number n (1<=n<=10^6) , the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 10^6.
  • 第一行一个整数n(1<=n<=10^5),表示将会输入的坐标的数量。
  • 接下来n行,每行一个坐标。
  • 注意: 每个坐标都是正确的。此外不会出现行号或列号大于10^6的单元格。

Output Format:

  • Write n lines, each line should contain a cell coordinates in the other numeration system.
  • n行,每行一个被转换的坐标。

Sample

Input:

  1. 2
  2. R23C55
  3. BC23

Output:

  1. BC23
  2. R23C55

Solution

  • 本题并不难,涉及字符串处理和一点数学.
    首先是判断输入数据属于哪一种类型,由于数据规模较小,本蒟蒻选择了暴力.
    这道题其实相当于一个进制互换,而我们发现行数R是不需要计算的,也就是需要考虑列数C的转化.
  • 不难发现对于一个字符串“C(1)C(2)C(3)……C(n)”,给定每个字符的编号为i(A=1,B=2……),她所代表的列数即为i(n)×26^0+i(n-1)×26^1+i(n-2)×26^2.
  • 数字转字符串呢?类比其他进制转换的过程,我们可以知道每次将c对26取模,结果就是对应字符串的第n,n-1,n-2……位.进行每次操作后,c减去 c%26,再用26除即可.
  • 需要注意的是,26%26=0,需要对Z进行特判,作差时也要注意减去26.
  • 代码如下:

    1. #include<iostream>
    2. #include<cstdio>
    3. #include<cstring>
    4. using namespace std;
    5. char s[1000];
    6. char alphabet[27] = { 'Z','A','B','C','D','E',
    7. 'F','G','H','I','J','K',
    8. 'L','M','N','O','P','Q',
    9. 'R','S','T','U','V','W',
    10. 'X','Y','\0' };
    11. inline bool check(char *s)//检查类型
    12. {
    13. int len=strlen(s);
    14. int type=0;
    15. for(int i=0;i<len;++i)
    16. {
    17. if(s[i]>='0'&&s[i]<='9')type=1;
    18. if(type&&s[i]=='C')return true;
    19. }
    20. return false;
    21. }
    22. inline void rd()
    23. {
    24. int r = 0, c = 0;
    25. int bit = 1;
    26. scanf("%s", &s);
    27. char output[1000];
    28. if (check(s))
    29. {
    30. int i = 1;
    31. while (s[i] >= '0'&&s[i] <= '9')
    32. {
    33. r = (r << 1) + (r << 3) + (s[i] ^ 48);//类似于读入优化,提速の小技巧
    34. i++;
    35. }
    36. i++;
    37. while (s[i] >= '0'&&s[i] <= '9')
    38. {
    39. c = (c << 1) + (c << 3) + (s[i] ^ 48);
    40. i++;
    41. }
    42. while (c != 0)//特判
    43. {
    44. int t=c%26;
    45. output[bit++] = alphabet[t];
    46. c -= t?t:26;
    47. c /= 26;
    48. }
    49. for (int j = bit-1;j >=1;--j)printf("%c", output[j]);
    50. printf("%d\n", r);
    51. return;
    52. }
    53. else {
    54. int i = 0;
    55. r=0;
    56. c=0;
    57. while (s[i] >= 'A'&&s[i] <= 'Z')
    58. {
    59. c = (c << 1) + (c << 3) + (c << 4) + s[i] + 1 - 'A';
    60. i++;
    61. }
    62. while (s[i] >= '0'&&s[i] <= '9')
    63. {
    64. r = (r << 1) + (r << 3) + (s[i] ^ 48);
    65. i++;
    66. }
    67. printf("R%dC%d\n", r, c);
    68. return;
    69. }
    70. }
    71. int main()
    72. {
    73. int n;
    74. scanf("%d", &n);
    75. for (int i = 1;i <= n;++i)rd();
    76. return 0;
    77. }
  • Feb,06,2018 Tue

CodeForces 1B-字符串,进制转换与数学的更多相关文章

  1. SHUoj 字符串进制转换

    字符串进制转换 发布时间: 2017年7月9日 18:17   最后更新: 2017年7月9日 21:17   时间限制: 1000ms   内存限制: 128M 描述 Claire Redfield ...

  2. SHU 414 - 字符串进制转换

    题目链接:http://acmoj.shu.edu.cn/problem/414/ 很咸鱼的网上拉了个进制转换模板过来,因为数组开的太小一直WA,后来一气之下MAXN开到1e5,真是蓝瘦…… 后来实在 ...

  3. EOJ Monthly 2019.2 (based on February Selection) D 进制转换 【数学 进制转换】

    任意门:https://acm.ecnu.edu.cn/contest/140/problem/D/ D. 进制转换 单测试点时限: 2.0 秒 内存限制: 256 MB “他觉得一个人奋斗更轻松自在 ...

  4. B. Spreadsheets(进制转换,数学)

    B. Spreadsheets time limit per test 10 seconds memory limit per test 64 megabytes input standard inp ...

  5. C语言拼接字符串以及进制转换

    #include<stdio.h> #include<stdlib.h> #include<string.h> char *join1(char *, char*) ...

  6. 总结day3 ---- 进制转换,字符串切片,字符串常用方法.,for 循环,

    前情提要: int 的相关操作 进制转换 bit_lenth() str 的索引,以及常用的相关方法 for 循环 索引 切片 相关方法 一  : int 的相关操作 int 主要用于生活中的计算问题 ...

  7. 19-3-4 Python进制转换;bool str int三者之间的转换;字符串的索引,切片;for循环的使用

    进制转换: 二进制转十进制:  0010 1111 = 1*2**0+1*2**1+1*2**2+1*2**3+1*2**5 十进制转换二进制: 用十进制数除2逆序取余 --->101010 布 ...

  8. 暴力/进制转换 Codeforces Round #308 (Div. 2) C. Vanya and Scales

    题目传送门 /* 题意:问是否能用质量为w^0,w^1,...,w^100的砝码各1个称出重量m,砝码放左边或在右边 暴力/进制转换:假设可以称出,用w进制表示,每一位是0,1,w-1.w-1表示砝码 ...

  9. C# 进制转换 在什么情况下使用16进制,字节数组,字符串

    C# 进制转换 Admin2013年9月18日 名人名言:从工作里爱了生命,就是通彻了生命最深的秘密.——纪伯伦 1.请问c#中如何将十进制数的字符串转化成十六进制数的字符串 //十进制转二进制Con ...

随机推荐

  1. unity3D加密如何做到防编译?

    先介绍对unity3D程序进行加密的几种方式. Unity3D是一个基于Mono框架的跨平台游戏开发引擎,Unity3D所使用的Mono属于Mono开源项目的分支 在Unity3D中,代码会编译到As ...

  2. videojs双击全屏幕观看,videojs动态加载视频

    前段时间闲来无事弄了弄video.js,感觉蛮好玩,能应用到各个应用端,自己在最后玩耍的时候,需要注意的只剩下两方面了,1,动态加载播放视频内容2,双击全屏观看, var urlRoad = &quo ...

  3. IPFS: BitSwap协议(数据块交换)

    原创 2018-01-11 飞向未来 IPFS指南 BitSwap协议 IPFS节点之间是如何进行数据交换的?本文来讲一下这个问题. IPFS在BitTorrent的基础上实现了p2p数据交换协议:B ...

  4. EOS 新增的 WebAssembly 解释器,是什么鬼?

    Daniel Larimer 在最近的博客中透露,EOS 新增了官方的 WebAssembly 解释器,用来解释执行 WebAssembly 智能合约,加上之前的编译执行,EOS 智能合约有了两种执行 ...

  5. struts2和spring mvc的区别

    在项目中使用struts2和spring mvc为了实现后台的业务代码和前台数据之间的传递,现在基本上不会有用struts2的了,几次面试问的最多的关于struts2的问题就是struts2和spri ...

  6. 百度API地图的标注不居中显示,而显示在左上角

    前言:今天弄个百度地图,弄了半天就是不居中,之前使用一直没有遇到这个问题.所以就一直在找原因. 百度地图对地图所在的div做了显示隐藏之类操作,标注就不再居中显示,而显示在左上角. 查了很久,有人提出 ...

  7. 【Python】 hash值计算 hashlib & hmac

    hashlib & hmac *不是很清楚能不能把这种hash值取样算法称之为加密,但是似乎好像也是这么说的哈(非科班出身的野路子就是没这种基本知识的) ■ 基本用法 hashlib支持MD5 ...

  8. react 实用的性能优化方式

    react 组件渲染分为初始化渲染和更新渲染,当我们更新某个组件的时候,只是想关键路径上组件的render,但react的默认做法是调用所以组件的reder,再生成虚拟dom进行对比,如不变则不进行更 ...

  9. 关于php日期前置是否有0

    例如:2018-01-04,这个日期和月份前置是有0 如果不想有0,date( 'y-n-j',time() ):默认的是date( 'y-m-d',time() ),这个日期和月份前置是有0. da ...

  10. Android开发简易教程

    Android开发简易教程 Android 开发因为涉及到代码编辑.UI 布局.打包等工序,有一款好用的IDE非常重要.Google 最早提供了基于 Eclipse 的 ADT 作为开发工具,后来在2 ...