#UVa:483-Word Scramble

灆洢 2012-03-18 01:02:28

可利用sscanf或是stringstream會用空白來隔開輸入的特性來做。

C++(0.008)

/*******************************************************/
/* UVa 483 Word Scramble                               */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2012/03/18                                 */
/*******************************************************/
#include<iostream>
#include<sstream>
#include<cstdio>
using namespace std;

int main(){
  string s, word;
  stringstream ss;
  bool space;
  while( getline( cin, s ) ){
    ss.clear();
    ss.str(s);
    space = false;
    while( ss >> word ){
      if( space ) printf( " " );
      space = true;
      for( int i = word.length()-1 ; i >= 0 ; i-- )
        printf( "%c", word[i] );
    }
    printf( "\n" );
  }
  return 0;
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料