1009 说反话 (20 分)

一、题目

在这里插入图片描述

二、题解

1-1 c/c++题解(1)

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

int main(){
	string s;
	char str[100][100];
	getline(cin, s); 
	int str_len = s.length();
	int word = 0, cnt=0;
	for(int i=0; i<str_len; i++){
		if(s[i] == ' '){
			cnt++;
			word = 0;
		}
		else{
			str[cnt][word++] = s[i];
		}
	}
	for(int i=cnt; i>=0; i--){
		printf("%s", str[i]);
		if(i > 0) printf(" ");
		else printf("\n");
	}
	
	return 0;
}

1-2 c/c++题解(2)

#include<stdio.h>
#include<string.h>

int main(){
	char s[100];
	char str[100][100];
	gets(s);
	int str_len = strlen(s);
	int word = 0, cnt=0;
	for(int i=0; i<str_len; i++){
		if(s[i] == ' '){
			cnt++;
			word = 0;
		}
		else{
			str[cnt][word++] = s[i];
		}
	}
	for(int i=cnt; i>=0; i--){
		printf("%s", str[i]);
		if(i > 0) printf(" ");
		else printf("\n");
	}
	
	return 0;
}

2- python题解

s = input().split()
for i in range(len(s)):
    if i != len(s)-1:
        print(s[len(s)-i-1], end=' ')
    else:
        print(s[0])

三、测试点分析

1- c++中不支持gets输入;

2- 使用scanf输入字符串中遇到空格将视为结束输入,c编译可改用gets()函数输入字符串;

3- c++中中使用fgets(s, len, stdin);

其中,s为输入的字符串,len为输入长度,stdin为标准输入流,要注意fgets中’\n’符可能会影响程序的执行,因为fgets会将回车键读到数组里,所以可以使用getline(cin, str)函数来获取带空格的字符串,cin为输入,str为string类型,获取其长度可用str.length()函数。

猜你喜欢

转载自blog.csdn.net/weixin_43509263/article/details/88652957
今日推荐