UVA458 - The Decoder
# 问题描述
输入多个字符串,经过某种变化后,输出相应字符串。
# 思路
比较输入和输出字符之间的ASCII码表,发现两个字符之间输入字符-7=输出字符。
# 代码
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
while(getline(cin,s))
{
for(int i=0;s[i]!='\0';i++)
s[i]-=7;
cout<<s<<endl;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
还有一种c语言的做法感觉更好,一个字符一个字符地进行比较。
#include <stdio.h>
int main()
{
char c;
while ((c = getchar()) != EOF)
if (c != '\n')
putchar(c - 7);
else
printf("\n");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
编辑 (opens new window)
上次更新: 2022/05/18, 19:03:08