代码求调,悬关
查看原帖
代码求调,悬关
749811
Licis_Subway楼主2023/10/6 14:37

第4个点WA了,思路就是模拟,分两部分,一部分是RXCY转EXCEL,另一部分是EXCEL转RXCY
声明一下,样例全对
第三个点错误信息:

Wrong Answer.
wrong answer 2nd words differ - 
expected: 'R1C4', found: 'R1C4B7'

错误代码:

#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool is_number(char a)
{
    if(a>='0'&&a<='9') return true;
    else return false;
}
bool is_RXCY(string s)
{
    bool flag=false;
    if(s[0]!='R') return false;
    if(!is_number(s[1])) return false;
    for(int i=1;i<s.size();i++)
    {
        if(is_number(s[i])&&(!flag)) flag=true;
        else if(flag&&s[i]=='C') return true;
    }
    return false;
}
string change_to_excel(int RXCY)
{
    string ans="";
    while(RXCY)
    {
        if(RXCY%26==0)
        {
            ans.push_back('Z');
            RXCY/=26;
            --RXCY;
        }
        else
        {
            ans.push_back(RXCY%26+'@');
            RXCY/=26;
        }
    }
    reverse(ans.begin(),ans.end());
    return ans;
}
int change_to_RXCY(string Excel)
{
    int ans=0;
    for(int i=0;i<Excel.size();i++) ans=ans*26+(Excel[i]-'@');
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        string s;
        cin>>s;
        if(is_RXCY(s))//RXCY 转换至 Excel形式
        {
            int R=0,C=0,ccnt=0;
            for(int i=1;s[i]!='C';i++)
            {
                R=R*10+(s[i]-'0');
                ccnt=i;
            }
            for(int i=ccnt+2;i<s.size();i++) C=C*10+(s[i]-'0');//ccnt是'C'这个字符的前一位,所以从ccnt+2开始
            string ans=change_to_excel(C);
            cout<<ans;
            printf("%d\n",R);
        }
        else//Excel形式 转换至 RXCY
        {
            string first;
            int second=0,firstcnt=0;
            for(int i=0;s[i]>='A'&&s[i]<='Z';i++)
            {
                first.push_back(s[i]);
                firstcnt=i;
            }
            for(int i=firstcnt+1;i<s.size();i++) second=second*10+(s[i]-'0');
            int ans=change_to_RXCY(first);
            printf("R%dC%d",second,ans);
        }
    }
    return 0;
}
2023/10/6 14:37
加载中...