很恶心的一道字符串模拟题
对于输入中的下划线,如果相邻右侧或下侧是数字,记录从数字起到下一个下划线出的所有数字的和
在这里用sprintf()着实很方便啊
sprintf(st, "%s", t)把数字t转化为字符串st
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 110
#define ll long long
using namespace std;
char ch[MAXN][MAXN];
string str;
int row, col;
void down(int a, int b) {
int t = 0;
int count = 0;
for(int i=a+1; i<=row; ++i) {
if(isdigit(ch[i][b])) {
t += (ch[i][b]-'0');
count++;
}
else break;
}
char st[20];
sprintf(st, "%d", t);
if(t<100 && t>9) {
str[0] = '0';
str.replace(1,2,st);
}
else if(t < 10) {
str[0] = '0';
str[1] = '0';
str.replace(2, 1, st);
}
else str.replace(0, 3, st);
str[3] = '\\';
// cout << "st = " << st << endl;
return ;
}
void right(int a, int b) {
int t = 0;
for(int i=b+1; i<=col; ++i) {
if(isdigit(ch[a][i]))
t += (ch[a][i]-'0');
else break;
}
char st[20];
sprintf(st, "%d", t);
if(t<100 && t>9) {
str[4] = '0';
str.replace(5,2,st);
}
else if(t < 10) {
str[4] = '0';
str[5] = '0';
str.replace(6, 1, st);
}
else str.replace(4,3,st);
str[3] = '\\';
return ;
}
int main(void) {
int T;
cin >> T;
while(T--) {
cin >> row >> col;
memset(ch, 0, sizeof(ch));
str = "";
for(int i=1; i<=row; ++i) {
for(int j=1; j<=col; ++j) {
cin >> ch[i][j];
getchar();
}
}
for(int i=1; i<=row; ++i) {
for(int j=1; j<=col; ++j) {
str = "XXXXXXX";
if(isdigit(ch[i][j]))
str = ".......";
else {
if(i<row && isdigit(ch[i+1][j])) {
down(i, j);
}
if(j<col && isdigit(ch[i][j+1])) {
right(i, j);
}
}
cout << str;
if(j != col)
cout << " ";
}
cout << endl;
}
cout << endl;
}
}