1009. Irony (20) time limit MS Memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Chen, Yue
Given an English sentence, you are asked to write a program that reverses the order of all the words in the sentence.
input Format: The test input contains a test case that gives a string with a total length of not more than 80 in a row. The string consists of several words and a number of spaces, where the word is a string of letters (case-sensitive), the words are separated by 1 spaces, and the input guarantees that there are no extra spaces at the end of the sentence.
output format: the output of each test case takes one line, and the output is reversed after the sentence.
Input Sample:
Hello World Here I Come
Sample output:
Come I here World Hello
Submit Code
Note: The use of the chain table head interpolation method to achieve
#include <iostream>#include<cstdio>#include<cmath>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>#defineMAXSIZE 100005using namespacestd;//node Structuretypedefstructnode{Chars[ -]; structNode *Next;} Node //linked list structure, each linked list has a head node and a tail node.typedefstructlink{Node*Head;} link;//Create a new linked listLink *createnewlink (link *Tlink) {Tlink->head= (node*)malloc(sizeof(node)); Tlink->head->next=NULL; returnTlink;} //input s, generate linked list, head interpolation methodvoidPutnewlink (Link *tlink,Char*s) {Node*newpoint= (node*)malloc(sizeof(node)); strcpy (Newpoint-s,s); Newpoint->next=tlink->head->Next; Tlink->head->next=Newpoint;} //directly after the head node, a node begins to traverse, and the order is reversed .voidReadlink (link *Tlink) {Node* pthis=tlink->head->Next; while(pthis!=NULL) {cout<<pthis->s; if(pthis->next==null) cout<<Endl; Elsecout<<" "; Pthis=pthis->Next; }} intMain () {link*newlink= (link*)malloc(sizeof(link)); NewLink=Createnewlink (NewLink); CharS[maxsize]; Charss[ -]; memset (s),0,sizeof(s)); memset (SS,0,sizeof(ss)); Gets (s); intI=0, j=0; while(s[i]!=' /'&&s[i]!=' ') {ss[j++]=S[i]; //Encounter Spaces if(s[i+1]==' '&&s[i+2]!=' ') {putnewlink (NEWLINK,SS); memset (SS,0,sizeof(ss)); J=0; I+=2; } //encounter end of string Else if(s[i+1]==' /') {putnewlink (NEWLINK,SS); Break; } Elsei++; } readlink (NewLink); //cout<<newlink->head->next->s<<endl; //puts (s); return 0;} View Code
Programming Ability Test study 1009. Irony (20)