iT邦幫忙

1

[VB] 程式設計 95年歷屆試題 第6題(已解決)

題目:https://ithelp.ithome.com.tw/upload/images/20190905/20120621HQFcPi9bWX.jpg
我的程式:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBoxOutput.Text = ""
        Dim InputMsg As String = TextBoxInput.Text
        Dim EnglishWords As Integer = 26
        Dim Character As String() = New String(EnglishWords - 1) {} '(0 ~ [EnglishWords]-1)
        Dim EnglishWordsArray As Integer() = New Integer(EnglishWords - 1) {}
        For IndeedWords = 1 To Len(InputMsg)
            For j As Integer = 0 To EnglishWords - 1
                If LCase(Mid(InputMsg, IndeedWords, 1)) = Chr(j + 97) Then
                    Character(j) += 1
                End If
            Next
        Next
        Dim a = 97 '10進位ASCII
        Dim z = 122
        For o = 0 To EnglishWords - 1
            For i = a To z
                EnglishWordsArray(o) = i
            Next
        Next
        Array.Sort(Character, EnglishWordsArray)
        Array.Reverse(Character)
        Array.Reverse(EnglishWordsArray)
        For Output = 0 To UBound(Character)
            If Character(Output) <> 0 Then
                TextBoxOutput.Text &= Chr(EnglishWordsArray(Output)) & "=" & Character(Output) & ";"
            End If
        Next
    End Sub
End Class

我的結果:https://ithelp.ithome.com.tw/upload/images/20190905/201206212ORooqsZhA.png
原本以為能正常顯示對應英文和次數
結果無法
可以的話告訴我錯的位置以及原因

第一次修改

Public Class Form1
    Private Sub Sort(sender As Object, e As EventArgs) Handles ButtonSort.Click
        TextBoxOutput.Text = ""
        Dim InputMsg As String = TextBoxInput.Text
        Dim EnglishWords As Integer = 26
        Dim Character As String() = New String(EnglishWords - 1) {}
        Dim EnglishWordsArray As Integer() = New Integer(EnglishWords - 1) {}
        For IndeedWords = 1 To Len(InputMsg)
            For j As Integer = 0 To EnglishWords - 1
                If LCase(Mid(InputMsg, IndeedWords, 1)) = Chr(j + 97) Then
                    Character(j) += 1
                End If
            Next
        Next

        Dim i = 97
        For o = 0 To EnglishWords - 1 Step 1
            EnglishWordsArray(o) = i
            i += 1
        Next

        Array.Sort(Character, EnglishWordsArray)
        Array.Reverse(Character)
        Array.Reverse(EnglishWordsArray)
        For Output = 0 To UBound(Character)
            If Character(Output) <> 0 Then
                TextBoxOutput.Text &= Chr(EnglishWordsArray(Output)) & "=" & Character(Output) & ";  "
            End If
        Next
    End Sub
End Class

能正常運作,但還不是題目要求的所有字元
接下來的疑問是
大寫部分和小寫如何並存以及空格部分
空格 ASCII: 32
大寫A~Z ASCII: 65 ~ 90
小寫a~z ASCII: 67 ~ 122
我是在想直接包32~122
但這樣運算元好像有點多
雖然暴力破解好像不錯用

最後做法

Public Class Form1
    Private Sub Sort(sender As Object, e As EventArgs) Handles ButtonSort.Click
        TextBoxOutput.Text = ""
        Dim InputMsg As String = TextBoxInput.Text
        Dim CharacterCalculation As Integer = 90
        Dim Character As String() = New String(CharacterCalculation - 1) {}
        Dim EnglishWordsArray As Integer() = New Integer(CharacterCalculation - 1) {}
        Dim StartWords As Integer = 32
        For IndeedWords = 1 To Len(InputMsg)
            For j As Integer = 0 To CharacterCalculation - 1
                If Mid(InputMsg, IndeedWords, 1) = Chr(j + StartWords) Then
                    Character(j) += 1
                End If
            Next
        Next

        Dim i = StartWords
        For o = 0 To CharacterCalculation - 1
            EnglishWordsArray(o) = i
            i += 1
        Next

        Array.Sort(Character, EnglishWordsArray)
        Array.Reverse(Character)
        Array.Reverse(EnglishWordsArray)
        For Output = 0 To UBound(Character)
            If Character(Output) <> 0 Then
                TextBoxOutput.Text &= Chr(34) & Chr(EnglishWordsArray(Output)) & Chr(34) & "=" & Character(Output) & ";" & "  "
            End If
        Next
    End Sub
End Class

dragonH iT邦大師 6 級 ‧ 2019-09-05 10:58:26 檢舉
所以你這 code 結果是什麼
無尾熊 iT邦新手 5 級 ‧ 2019-09-05 11:03:56 檢舉
已補充
1
海綿寶寶
iT邦超人 1 級 ‧ 2019-09-05 15:16:45
最佳解答
For i = a To z
   EnglishWordsArray(o) = i
Next

你這段的寫法
在迴圈跑完之後
EnglishWordsArray(o) 都會是「最後一個 i」
也就是z

你要的可能是

For o = 0 To EnglishWords - 1
   EnglishWordsArray(o) = o + 97
Next
無尾熊 iT邦新手 5 級 ‧ 2019-09-05 17:03:31 檢舉

了解,已修正
雖然寫法不同

3
froce
iT邦大師 5 級 ‧ 2019-09-05 12:15:15

https://ideone.com/EFmvXF
python3

testStr = "gdsfgfdhjghfgsdf"

def sortBy(item):
	return item[1]

result = sorted([[str(c), testStr.count(c)] for c in set(testStr)], key=sortBy, reverse=True)

print(";".join('"{}"={}'.format(i[0], i[1]) for i in result))

所以說你這個比賽真的沒限制語言嗎?XD

無尾熊 iT邦新手 5 級 ‧ 2019-09-05 13:11:14 檢舉

orz我不知道所以只敢用VB

souda iT邦研究生 3 級 ‧ 2019-09-05 13:12:11 檢舉

試看看python......

3
dragonH
iT邦大師 6 級 ‧ 2019-09-05 13:40:52

js 參戰 /images/emoticon/emoticon78.gif

codepen

const encoding = (input) => {
  return [...new Set(input)].map((el) => {
    const temp = {};
    const regexp = new RegExp(`[^${el}]`, 'g');
    temp[el] = input.replace(regexp, '').length;
    return temp;
  }).sort((a, b) => {
    return Object.values(a) > Object.values(b)
      ? -1
      : Object.values(a) < Object.values(b)
        ? 1
        : Object.keys(a) > Object.keys(b)
          ? 1
          : -1;
  }).map(el2 => `"${Object.keys(el2)[0]}"=${Object.values(el2)[0]}`)
    .join(';');
}
const result = encoding('Conventional approaches for encoding technique');
console.log(result);

是說他這題目根本有問題吧

輸出我找不出來有任何規則

原本以為是

sort by key 然後 sort by value

但看起來不是

或者是 sort by value 然後 value 一樣的 照原字串順序排

但是看到 c 跟 a 的輸出位置就知道不是/images/emoticon/emoticon11.gif

難道是要自己寫 sort 規則!?

而且後半部的; 間空格不一樣是哪招 XD


回正題

你的輸出 value 看起來沒問題的

但是 key 不對

所以你可能要看一下你產生 key 那部份的 code

froce iT邦大師 5 級 ‧ 2019-09-05 15:51:56 檢舉

這種題目應該只有sort by value啦,其他不用想太多。

dragonH iT邦大師 6 級 ‧ 2019-09-05 16:20:32 檢舉

可能刷題刷習慣

多一個空白就會過不了 XD/images/emoticon/emoticon46.gif

無尾熊 iT邦新手 5 級 ‧ 2019-09-05 17:06:33 檢舉

dragonH

你的輸出 value 看起來沒問題的

但是 key 不對

所以你可能要看一下你產生 key 那部份的 code

是的,已修正

1
YoChen
iT邦新手 4 級 ‧ 2019-09-05 15:57:14

C# 參戰

using System;
using System.Collections.Generic;
using System.Linq;

namespace TestArea
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Input Your String:");
            string userInput = Console.ReadLine();
            Dictionary<char, int> dictionary = new Dictionary<char, int>();
            List<char> characterList = userInput.ToList();

            foreach (char character in characterList)
            {
                if (dictionary.Keys.Contains(character))
                {
                    dictionary[character] += 1;
                }
                else
                {
                    dictionary.Add(character, 1);
                }
            }

            List<KeyValuePair<char, int>> keyValuePairList = dictionary.ToList();
            keyValuePairList = keyValuePairList.OrderByDescending(pair => pair.Value).ToList();

            foreach (KeyValuePair<char, int> pair in keyValuePairList)
            {
                Console.Write("\"{0}\":{1}; ", pair.Key, pair.Value);
            }

            Console.ReadKey();
        }
    }
}
無尾熊 iT邦新手 5 級 ‧ 2019-09-05 17:12:10 檢舉

看到C#有一點點的熟悉感,但我沒辦法轉換
熟悉感大概是因為我寫過arduino
感覺有些相似

2
firecold
iT邦新手 5 級 ‧ 2019-09-06 11:16:54

php 參戰

        $str = 'Conventional approaches for encoding technique';
        $result = [];
        $arr = [];
        $items = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
        foreach ($items as $item){
            isset($result[$item]) ? $result[$item]++ : $result[$item] = 1;
        }
        arsort($result);
        
        foreach ($result as $key => $item){
            //方法一 echo $key . '=' . $item . ';';
            $arr[] = $key . '=' . $item;
        }
        
        echo implode(';', $arr);

我要發表回答

立即登入回答