字符矩阵翻译
输入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学长提供以上题目练习)