题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062
小结:水题,注意最后一行别输出空格就行!用到了栈这种数据结构!
CODE:
#include<stdlib.h>
#include<stdio.h>
#include< string.h>
#include<stack>
using namespace std;
stack< char> s;
const int SIZE = 1001;
char sz1[SIZE];
char sz2[SIZE];
void output( char sz1[])
{
int len = strlen(sz1);
int i, j = 0;
for(i = 0; i < len; i++)
{
s.push(sz1[i]);
}
while(!s.empty())
{
sz2[j++] = s.top();
s.pop();
}
printf( " %s ", sz2);
}
int main()
{
int t;
scanf( " %d ", &t);
getchar();
while(t--)
{
int i, j= 0;
memset(sz1, 0, sizeof(sz1));
memset(sz2, 0, sizeof(sz2));
gets(sz1);
int len = strlen(sz1);
sz1[len] = ' ';
sz1[len+ 1] = ' \0 ';
for(i = 0; i <= len; i++)
{
if(sz1[i] == ' ')
{
output(sz2);
printf(i == len? "": " ");
memset(sz2, 0, sizeof(sz2));
j = 0;
continue;
}
else sz2[j++] = sz1[i];
}
printf( " \n ");
}
// system("pause");
return 0;
#include<stdio.h>
#include< string.h>
#include<stack>
using namespace std;
stack< char> s;
const int SIZE = 1001;
char sz1[SIZE];
char sz2[SIZE];
void output( char sz1[])
{
int len = strlen(sz1);
int i, j = 0;
for(i = 0; i < len; i++)
{
s.push(sz1[i]);
}
while(!s.empty())
{
sz2[j++] = s.top();
s.pop();
}
printf( " %s ", sz2);
}
int main()
{
int t;
scanf( " %d ", &t);
getchar();
while(t--)
{
int i, j= 0;
memset(sz1, 0, sizeof(sz1));
memset(sz2, 0, sizeof(sz2));
gets(sz1);
int len = strlen(sz1);
sz1[len] = ' ';
sz1[len+ 1] = ' \0 ';
for(i = 0; i <= len; i++)
{
if(sz1[i] == ' ')
{
output(sz2);
printf(i == len? "": " ");
memset(sz2, 0, sizeof(sz2));
j = 0;
continue;
}
else sz2[j++] = sz1[i];
}
printf( " \n ");
}
// system("pause");
return 0;
}