B - Spreadsheet(进制转换+字符串大模拟)

B. Spreadsheets

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.

Input
The first line of the input contains integer number n (1 ≤ n ≤ 105), 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 106 .

Output
Write n lines, each line should contain a cell coordinates in the other numeration system.

Examples
inputCopy
2
R23C55
BC23
outputCopy
BC23
R23C55

这是一个超级大的字符串模拟,题意是有两种表示表格的方法,写出其中一种来,输出另一种方法。
过程真的不容易。。。这里贴一个我的截图

在这里插入图片描述
总是mle在6上,我也不知道为什么

开始需要先写一个函数,判断是哪种方法转哪种方法

bool is_first(string a)
{
  if(a[0]=='R'&&(a[1]>='0'&&a[1]<='9'))
  {
    int l=a.size();
    for(int i=2;i<l;i++)
    {
      if(a[i]=='C')
      {
        return 1;
      }
    }
  }
  return 0;
}

刚开始看起来像是一道简单的进制转换问题,可是发现AA=十进制的27,如果按照传统的进制转换的话,模拟得到十进制55转变成26进制是D开头的而并非是BC,所以需要进行以下处理:

  if(ans2%26==0)
        st.push('Z');
        else
        {
          st.push(char('A'+(ans2-1)%26));
        }
        ans2=(ans2-1)/26;

这样就可以保证十进制数可以顺利转换成26进制啦
接下来就是十进制转换成26进制的全过程:利用栈来存,最后倒序输出就是结果

while(ans2)
      {
        if(ans2%26==0)
        st.push('Z');
        else
        {
          st.push(char('A'+(ans2-1)%26));
        }
        ans2=(ans2-1)/26;
      }
      while(st.size())
      {
        cout<<st.top();
        st.pop();
      }

接下来是26进制转换成十进制

else
    {
      string t3="";
      string t4="";
      int id2=0;
      for(int i=0;i<a.size();i++)
      {
        if(isalpha(a[i]))
        t3+=a[i];
        else
        {
          id2=i;
          break;
        }
      }
      for(int i=id2;i<a.size();i++)
      t4+=a[i];
      stringstream s3;
      s3<<t4;
      int ans3;
      s3>>ans3;
      int sum=0;
      reverse(t3.begin(),t3.end());
      for(int i=0;i<t3.size();i++)
      {
        sum+=(t3[i]-'A'+1)*qpow(26,i);
      }
      cout<<"R"<<ans3<<"C"<<sum<<"\n";
    }

这里有一个奇怪的地方是再算十进制数的时候,如果直接用pow函数,虽然编译器跑出来是对的,但是评测机器却少了1,这个原理我也不知道,总之还得自己写一个pow函数:其中base代表的是进制

int qpow(int base,int n)
{
  int sum=1;
  for(int i=1;i<=n;i++)
  sum*=base;
  return sum;
}

剩下的就是一些细节问题了,数据量较大,注意写"\n"代替endl 还有关闭同步节省时间

AC代码:

#include<iostream>
#include<stack>
#include<sstream>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
stack<char>st;
int qpow(int base,int n)
{
  int sum=1;
  for(int i=1;i<=n;i++)
  sum*=base;
  return sum;
}
bool is_first(string a)
{
  if(a[0]=='R'&&(a[1]>='0'&&a[1]<='9'))
  {
    int l=a.size();
    for(int i=2;i<l;i++)
    {
      if(a[i]=='C')
      {
        return 1;
      }
    }
  }
  return 0;
}
int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin>>n;
  while(n--)
  {
    string a;
    cin>>a;
    if(is_first(a))
    {
      string t1="";
      string t2="";
      int id=0;
      for(int i=1;i<a.size();i++)
      {
        if(isdigit(a[i]))
        t1+=a[i];
        else
        {
          id=i;
          break;
        }
      }
      for(int i=id+1;i<a.size();i++)
      {
        t2+=a[i];
      }
      stringstream s1,s2;
      s1<<t1;
      s2<<t2;
      int ans1,ans2;
      s1>>ans1;
      s2>>ans2;
      while(ans2)
      {
        if(ans2%26==0)
        st.push('Z');
        else
        {
          st.push(char('A'+(ans2-1)%26));
        }
        ans2=(ans2-1)/26;
      }
      while(st.size())
      {
        cout<<st.top();
        st.pop();
      }
      cout<<ans1<<"\n";
    }
    else
    {
      string t3="";
      string t4="";
      int id2=0;
      for(int i=0;i<a.size();i++)
      {
        if(isalpha(a[i]))
        t3+=a[i];
        else
        {
          id2=i;
          break;
        }
      }
      for(int i=id2;i<a.size();i++)
      t4+=a[i];
      stringstream s3;
      s3<<t4;
      int ans3;
      s3>>ans3;
      int sum=0;
      reverse(t3.begin(),t3.end());
      for(int i=0;i<t3.size();i++)
      {
        sum+=(t3[i]-'A'+1)*qpow(26,i);
      }
      cout<<"R"<<ans3<<"C"<<sum<<"\n";
    }
  }
}

看来要想担任字符串手这个位置还得付出很多倍的努力才行啊!现在还是太菜了哎 加油把!

我 相 信 我 自 己

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值