1. 程式人生 > 其它 >hduoj Text Reverse

hduoj Text Reverse

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1062

這個老六啊我真的服了,一個cin和一個scanf都能給我卡,我真的服了,

其實這個題不是很難,這個題就是處理字串逆序的板子甚至(自認為i)

處理逆序用字串就ok了啊;

但是比較一下這兩段程式碼:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    char ch;
    cin
>>n; getchar(); while(n--) { stack<char>s; while(true) { ch=getchar(); if(ch==' '||ch=='\n') { while(!s.empty()) { printf("%c",s.top()); s.pop(); }
if(ch=='\n') break; printf(" "); } else s.push(ch); } printf("\n"); } return 0; }

Talk is cheap. Show me the code.

AC程式碼:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     std::ios::sync_with_stdio(false
); 6 cin.tie(0); 7 cout.tie(0); 8 int n; 9 char ch; 10 scanf("%d",&n); 11 getchar(); 12 while(n--) 13 { 14 stack<char>s; 15 while(true) 16 { 17 ch=getchar(); 18 if(ch==' '||ch=='\n') 19 { 20 while(!s.empty()) 21 { 22 printf("%c",s.top()); 23 s.pop(); 24 } 25 if(ch=='\n') 26 break; 27 printf(" "); 28 } 29 else 30 s.push(ch); 31 } 32 printf("\n"); 33 } 34 return 0; 35 }

看到了吧,第十行的輸入用cin和scanf結果居然不一樣我就離譜,真的,我得研究研究cin和scanf的一些細節問題;

就這樣~~~~