标签:
rose red red a like is luve my O June in sprung newly That‘s melodie the like is luve my O tune in played sweetly That‘s
O my luve is like a red red rose That‘s newly sprung in June O my luve is like the melodie That‘s sweetly played in tune
注释:这个题目其实就是翻转句子当中所有单词的顺序
(注意不是修改每一个单词中字母的顺序,而是修改单词在句子中的顺序)
本题需要注意的是:单词之间若是有多个空格要如何处理呢?若是直接用scanf或cin输入每一个单词,
然后像栈一样从栈顶到栈底输出每一个元素。这样就会忽略单词之间有多个空格的情况,无法输出单词间的多个空格。
所以要一次输入一整行,保存该行所有字符,然后再处理。其实这个做法在以前有过题目的。
http://www.cnblogs.com/huashanqingzhu/p/3457758.html
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int main() 6 { 7 char a[5000] = ""; 8 int n, i, j; 9 //freopen("5.in","r",stdin); 10 while(gets(a)) 11 { 12 n = strlen(a); 13 for(i=n-1;i>=0;i--) 14 { 15 if(a[i]==‘ ‘) 16 { 17 for(j=i+1;a[j]!=‘ ‘&&a[j]!=‘\0‘;j++) 18 cout<< a[j]; 19 cout<<" "; 20 } 21 } 22 for(i=0;a[i]!=‘ ‘&&a[i]!=‘\0‘;i++) 23 cout<<a[i]; 24 cout << endl; 25 } 26 return 0; 27 }
标签:
原文地址:http://www.cnblogs.com/huashanqingzhu/p/5116817.html