课时5作业2

Description

读取一个字符串,字符串可能含有空格,将字符串逆转,原来的字符串与逆转后字符串相同,输出0,原字符串小于逆转后字符串输出-1,大于逆转后字符串输出1。例如输入 hello,逆转后的字符串为 olleh,因为hello 小于 olleh,所以输出-1
注意最后的判断一定要这么写,因为strcmp标准C中并不是返回-1和1,而是负值和正值

int result = strcmp(c, d);
if (result < 0)
{
	printf("%d\n",-1);
}
else if (result > 0)
{
	printf("%d\n", 1);
}
else {
	printf("%d\n", 0);
}

Input

输入一个字符串,例如 hello,当然输入的字符串也可能是 how are you,含有空格的字符串

Output

输出是一个整型数,如果输入的字符串是hello,那么输出的整型数为-1

Sample Input 1

hello

Sample Output 1

-1

Sample Input 2

cba

Sample Output 2

1

Sample Input 3

aba

Sample Output 3

0

code:
用string进行字符串反转和比较
需要注意的是读入带空格的字符串要用getline(cin, s)

#include 
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int a[N];

void solve() {
    string s, p;
    getline(cin, s);
    p = s;
    reverse(p.begin(), p.end());
    if (s > p) {
        cout << 1 << "\n";
    } else if (s == p) {
        cout << 0 << "\n";
    } else {
        cout << -1 << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_ -- ) {
        solve();
    }
    return 0;
}

你可能感兴趣的:(考研,#,c语言督学训练营,算法,数据结构,c++)