Rating 800 总目录
题目链接入口
题目描述
0 是 «.», 1 是 «-.» , 2 是 «–».输入一个字符串,判断相应的数字。
输入
输入一个字符串
输出
输出相应数字。
案例
输入案例
.-.--
输出案例
012
输入案例
--.
输出案例
20
输入案例
-..-.--
输出案例
1012
题解:
暴力遍历判断i和i+1看是否符合对应的字符。判断出来之后将str[i+1]化成空字符。以防检测到相应字符,输出多一个数字。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
using namespace std;
int read() { int w = 1, s = 0; char ch = getchar(); while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); }return s * w; }
///------------------------ 以上是我常用模板与刷题几乎无关 ------------------------//
string str;
signed main()
{
IOS;
cin >> str;
for (int i = 0; i < str.size(); i++)
{
if (str[i] == '.')
printf("0");
else if (str[i] == '-' && str[i + 1] == '.')
{
printf("1");
str[i + 1] = ' ';
}
else if (str[i] == '-' && str[i + 1] == '-')
{
printf("2");
str[i + 1] = ' ';
}
}
return 0;
}