zoukankan      html  css  js  c++  java
  • CF1B Spreadsheets

    题意翻译

    人们常用的电子表格软件(比如: Excel)采用如下所述的坐标系统:

    第一列被标为A,第二列为B,以此类推,第26列为Z。接下来为由两个字母构成的列号: 第27列为AA,第28列为AB...在标为ZZ的列之后则由三个字母构成列号,如此类推。

    行号为从1开始的整数。

    单元格的坐标由列号和行号连接而成。比如,BC23表示位于第55列23行的单元格。

    有时也会采用被称为RXCY的坐标系统,其中X与Y为整数,坐标(X,Y)直接描述了对应单元格的位置。比如,R23C55即为前面所述的单元格。

    您的任务是编写一个程序,将所给的单元格坐标转换为另一种坐标系统下面的形式。

    输入

    第一行一个整数n(1<=n<=10^5),表示将会输入的坐标的数量。

    接下来n行,每行一个坐标。

    注意: 每个坐标都是正确的。此外不会出现行号或列号大于10^6的单元格。

    输出

    n行,每行一个被转换的坐标。

    输入格式:

    2

    R23C55

    BC23

    输出格式:

    BC23

    R23C55

    分析:这道题其实挺简单的,想到就是将十进制的数转换为二十六进制就可以了,然后注意一下当n2%26==0的时候是'A'就可以了

     1 #include<iostream>
     2 #include<cmath>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cstdlib>
     6 using namespace std;
     7 char a[20];
     8 
     9 void solve1(){
    10     int len=strlen(a);
    11     int i;
    12     int n1=0,n2=0;
    13     int flag=0;
    14     for( i=1; i<len; i++ ){
    15         if(a[i]>='0'&&a[i]<='9'&&flag==0){
    16             n1=n1*10+(a[i]-'0');
    17         }
    18         else if(a[i]>='0'&&a[i]<='9'&&flag==1){
    19             n2=n2*10+(a[i]-'0');
    20         }
    21         else{
    22             flag=1;
    23         }
    24     }
    25 //    cout<<"n1="<<n1<<" n2="<<n2<<endl;
    26     char temp[100];
    27     int pos=0;
    28     while(n2!=0){
    29         int t=n2%26;
    30         if(t==0) temp[pos++]='A';
    31         else{
    32             temp[pos++]=(char)(65+t-1);
    33         }
    34         n2/=26;
    35     }
    36     for( int i=pos-1; i>=0; i-- ){
    37         cout<<temp[i];
    38     }
    39     cout<<n1<<endl;
    40 }
    41 
    42 void solve2(){
    43 //    cout<<a<<endl;
    44     int len=strlen(a);
    45     int t=0;
    46     for( int i=0; i<len; i++ ){
    47         if(!(a[i]>='A'&&a[i]<='Z')){
    48             t=i-1;
    49             break;
    50         }
    51     }
    52     double m2=0;
    53     for( int i=0; i<=t; i++ ){
    54         m2=m2+(a[i]-'A'+1)*(pow(26,(t-i)));
    55 //        cout<<"a[i]-'A'+1="<<a[i]-'A'+1<<" "<<pow(26,(t-i))<<endl;
    56 //        cout<<"m2="<<m2<<endl;
    57     }
    58     cout<<'R';
    59     for( int i=t+1; i<len; i++ ){
    60         cout<<a[i];
    61     }
    62     cout<<'C'<<m2<<endl;
    63 }
    64 
    65 int main(){
    66     int n;
    67     cin>>n;
    68     while(n--){
    69         cin>>a;
    70         if(a[0]=='R'&&a[1]>='0'&&a[1]<='9'){
    71 //            cout<<a<<endl;
    72             solve1();
    73         }
    74         else{
    75             solve2();
    76         }
    77     }
    78     return 0;
    79 }

    但是在做这道题的时候有个很气愤的事,如果你把52行的double m2=0;改成int m2=0;(笔者实在codeblocks,,,MINGW编译器下跑的程序),会发现结果是不对的,样例二得出结果是m2=51

    这个去看了下pow函数的源码,。。。。问了下大佬们,pow是很玄学的东西,所以大家在写东西的时候尽量避开pow函数,自己写个for循环鸭,也不长。。。

    有些目标看似很遥远,但只要付出足够多的努力,这一切总有可能实现!
  • 相关阅读:
    高盛、沃尔玛 题做出来还挂了的吐槽
    amazon师兄debrief
    到所有人家距离之和最短的中点 296. Best Meeting Point
    问问题没人回答的情况怎么办终于有解了
    找名人 277. Find the Celebrity
    数组生存游戏 289. Game of Life
    547. Number of Provinces 省份数量
    428. Serialize and Deserialize Nary Tree 序列化、反序列化n叉树
    alias别名简介和使用
    面试官:线程池执行过程中遇到异常会发生什么,怎样处理? Vincent
  • 原文地址:https://www.cnblogs.com/Bravewtz/p/10348924.html
Copyright © 2011-2022 走看看