4-字符转换及所在位置

字符矩阵翻译

输入n和m,代表这个字符矩阵的行和列数,然后在接下来的n行,每行输入一个含有m个字符的字符串。要求将这个字符矩阵中的每一个字符,转换为对应的字符。

转换规则为:

对于大写字母’A’-’Z’,分别转换为0-25

对于小写字母’a’-’z’,分别转换为26-51

对于其他字符,全部转换为’X’,大写的’X’

然后

1.输出这个转换后的字符矩阵

2.输出转换后X的数目,另外再按照行优先的顺序,输出第一个X所在的行列(每个字符算一个位置,例如26X,则X是第一行第三列) 行列标均从1开始

注意:题目满足转换后一定存在至少一个’X’.

例如输入:

2 3

acZ

@2z

输出为:

262825

XX51

2 2 1


代码1:(自己写的):

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    int n,m,tem,x=0,cc=0,rr=0;
    char c;
    cin>>n>>m;
    getchar();
    for(int i=1;i<=n;i++)
    {
        tem=0;
        for(int j=1;j<=m;j++)
        {
            c=getchar();
            if(c>='A'&&c<='Z')
                if((c-'A')>=10)
                {
                    printf("%d%d",(c-'A')/10,(c-'A')%10);
                    tem+=2;
                }
                else
                {
                    printf("%d",c-'A');
                    tem++;
                }
            else if(c>='a'&&c<='z')
                {
                    printf("%d%d",(c-'a'+26)/10,(c-'a'+26)%10);
                    tem+=2;
                }
                else
                {
                    printf("X");
                    x++;
                    if(x==1)
                    {
                        cc=i;
                        rr=tem+1;
                    }
                }
        }
        getchar();
        printf("\n");
    }
        printf("%d ",x);
        printf("%d %d",cc,rr);
    return 0;
}

代码2:(西交wrong):

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;
const int maxn = 100000;
typedef pair<int,int> P;
int main()
{
    int n,m,rr,cc,tmp,cot=0;
    char c;
    bool first=0; //0还未出现X 1已经出现X
    cin>>n>>m;
    getchar();
    for(int i=1;i<=n;i++)
    {
        tmp=0;
        for(int j=1;j<=m;j++)
        {
            c=getchar();
            if(c>='A'&&c<='Z')
            {
                if(c-'A'<=9) tmp++;//当前行已经转换之后的字符个数
                else tmp+=2;
                printf("%d",c-'A');
            }
            else
                if(c>='a'&&c<='z')
                {
                    printf("%d",c-'a'+26);
                    tmp+=2;
                }
                else
                {
                    printf("X");
                    cot++;
                    if(first==0)//'X'还没有出现
                        {rr=i;cc=tmp+1;first=1;}//'X'已经出现
                }
        }
        getchar();
        printf("\n");
    }
    cout<<cot<<" "<<rr<<" "<<cc<<endl;
   return 0;
}

(感谢西交wrong学长提供以上题目练习)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值