思路:
用一个二维字符数组保存所有输入的字符串,倒序输出二维字符数组中的四个字符串。
解答:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int sample_num;
while(scanf("%d", &sample_num) != EOF) {
getchar(); // 吸收scanf后的换行符
char strings[100][100];
for(int i = 0; i <= sample_num - 1; i++) {
gets(strings[i]);
for(int j = i, k = 1; k <= 4 && j >= 0; j--, k++) { // j是要输出的字符串在二维数组中的序号
cout << k << "=" << strings[j] << " ";
}
cout << endl;
}
}
return 0;
}