2012年3月9日 星期五

迴文

內容 :

  迴文是運用相同詞彙、相反順序組合而成的語句,是一種修辭法,也有人把它當做文字遊戲。
  一個迴文可以是一個句子:「上海自來水來自海上」;也可以分為上下兩句:「人人為我,我為人人。」甚至可以是一首詩:

枯眼望遙山隔水,往來曾見幾心知。
壺空怕酌一杯酒,筆下難成和韻詩。
迷路阻人離別久,訊音無雁寄回遲。
孤燈夜守長寥寂,夫憶妻兮父憶兒。
兒憶父兮妻憶夫,寂寥長守夜燈孤。
遲回寄雁無音訊,久別離人阻路迷。
詩韻和成難下筆,酒杯一酌怕空壺。
知心幾見曾來往,水隔山遙望眼枯。


輸入說明 :
  第一行有一個整數 T,代表接下來有幾組測試資料。

  每一組測試資料有一個字串,字串是由小寫的英文字母所組成,每個字串的長度不會超過 100。


輸出說明 :
對每筆測試資料判斷是不是迴文 (字串正著讀和反著讀是否一樣) :如果是的話就輸出 “yes”,否則就輸出 “no”。

範例輸入 :
3
abcba
abba
abc

範例輸出 :
yes
yes
no

4 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Dim t As Boolean
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    For i = 1 To n
    ans = ""
    ans1 = ""
    Line Input #1, x
    a = Len(x)
    For j = 1 To a \ 2
    If Mid(x, j, 1) = Mid(x, a - j + 1, 1) Then
    t = True
    Else
    t = False
    End If
    If t = False Then Exit For
    Next
    If t = True Then
    Print #2, "yes"
    Else
    Print #2, "no"
    End If
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    For i = 1 To n
    Line Input #1, s
    a = Len(s)
    f = 0
    For j = 1 To a \ 2
    If Mid(s, j, 1) = Mid(s, a - j + 1, 1) Then
    Else
    f = 1
    End If
    If f = 1 Then Exit For
    Next j

    If f = 0 Then
    Print #2, "yes"
    Else
    Print #2, "no"
    End If
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim t As Boolean
    Input #1, n
    For i = 1 To n
    t = False
    Input #1, sn
    xx = Len(sn) \ 2
    cc = Right(sn, xx)
    bb = ""
    For j = 1 To xx
    bb = Mid(cc, j, 1) & bb
    Next j
    If bb = Left(sn, xx) Then t = True
    If t = True Then
    Print #2, "yes"
    Else
    Print #2, "no"
    End If
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. Private Sub Form_Load()
    Dim arr() As String
    Dim qq As Boolean
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    For i = 1 To Val(n)
    Input #1, strA
    ReDim arr(Len(strA))
    For j = 1 To Len(strA)
    arr(j) = Mid(strA, j, 1)
    Next j
    For j = 1 To Len(strA)
    If arr(i) = arr(Len(strA) - i + 1) Then
    qq = True
    Else
    qq = False
    End If
    Next j
    If qq = True Then
    Print #2, "yes"
    Else
    Print #2, "no"
    End If
    qq = False
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除