算法競賽入門經典 5.1.1 WERTYU 5.1.2 Tex括號

5.1.1  WERTYU 

 把手放在鍵盤上時,稍不注意就會往右錯一位。 這樣的話,Q會變成W,J會變成K等。 輸入一個錯位敲出的字符串,輸出打字員本來想打出的句子。

 樣例輸入:O S,GOMR YPFSU/

 樣例輸出:I AMFINE TODAY. 


#include <stdio.h>
#include <stdlib.h>
char *s = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./"; 

int main(int argc, char *argv[])
{
  int i, c;
  while((c = getchar()) != EOF)
  {
     for(i = 1; s[i]&&s[i]!=c; i++);
     if(s[i]) putchar(s[i-1]);
     else putchar(c);
  
  }
  system("PAUSE");	
  return 0;
}

總結:1 反義字符的表示  \\

            2 int c = getchar()

            3 s[i]&&s[i] != c   存在找不到的情況,考慮周全,循壞結束後下一步要判斷從哪點退出循壞的


5.1.2 

 TeX括號  在TeX中,左雙引號``,右雙引號"。輸入一篇篇包含雙引號的文章,你的任務是把它轉換成TeX的格式。

  樣例輸入:"To be or not to be,"quoth the Bard, "that is the question". 

  樣例輸出:``To be or not to be,"quoth the Bard, ``that is the question''.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int c, q = 1;
  while((c = getchar()) != EOF)
  {
  if(c == '"') {printf("%s", q ? "``" : "''"); q = !q;}
  else putchar(c);         
  }
  system("PAUSE");	
  return 0;
}

總結:1 c == '"'

            2 用q來判斷是單次遇到還是雙次遇到

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章