0

まず第一に、私の母国語ではない英語の間違いで申し訳ありません
。問題があります。次のような配列があります

string[] arr1 = new string[] { 
            "Pakistan:4,India:3,USA:2,Iran:1,UK:0",
            "Pakistan:4,India:3,USA:2,Iran:1,UK:0", 
            "India:4,USA:3,Iran:2,UK:1,Pakistan:0" 
        };


今、私はパキスタンが 1 で何回、2 、 3 、 4 で何回来るかを知りたいだけで、インド、アメリカ、イラン、イギリスすべてについてこれを知る必要があります。

前もってありがとう、あなたたちは私の最後の希望です。

4

3 に答える 3

1

String.Split(char [])メソッドとString.SubString(int、int)メソッドを使用して、配列内のすべての「国」を検査し、各国の番号の接尾辞を取得します。

次のことを試してください。

(次のコードがコンパイルおよびテストされました。)

単純なデータ構造を使用して、操作の結果を保持するタスクを容易にします。

public struct Result {

    string Country { get; set; }
    int Number { get; set; }
    int Occurrences { get; set; }
}


// define what countries you are dealing with
string[] countries = new string[] { "Pakistan", "India", "USA", "Iran", "UK", }

全体的な結果を提供する方法:

public static Result[] IterateOverAllCountries () {

    // range of numbers forming the postfix of your country strings
    int numbersToLookFor = 4;        

    // provide an array that stores all the local results
    // numbersToLookFor + 1 to respect that numbers are starting with 0
    Result[] result = new Result[countries.Length * (numbersToLookFor + 1)];

    string currentCountry;

    int c = 0;

    // iterate over all countries
    for (int i = 0; i < countries.Length; i++) {

        currentCountry = countries[i];

        int j = 0;

        // do that for every number beginning with 0
        // (according to your question)

        int localResult;          

        while (j <= numbersToLookFor) {

            localResult = FindCountryPosition(currentCountry, j);

            // add another result to the array of all results
            result[c] = new Result() { Country = currentCountry, Number = j, Occurrences = localResult };

            j++;
            c++;
        }
    }

    return result;
}

ローカル結果を提供する方法:

// iterate over the whole array and search the
    // occurrences of one particular country with one postfix number
    public static int FindCountryPosition (string country, int number) { 

        int result = 0;
        string[] subArray;

        for (int i = 0; i < arr1.Length; i++) {

            subArray = arr1[i].Split(',');

            string current;

            for (int j = 0; j < subArray.Length; j++) {

                current = subArray[j];
                if (
                    current.Equals(country + ":" + number) &&
                    current.Substring(current.Length - 1, 1).Equals(number + "")
                 ) 
                    result++;
            }
        }

        return result;
    }

以下は、アルゴリズムを実行できるようにする必要があります

    // define what countries you are dealing with
    static string[] countries = new string[] { "Pakistan", "India", "USA", "Iran", "UK", };

    static string[] arr1 = new string[] { 
        "Pakistan:4,India:3,USA:2,Iran:1,UK:0",
        "Pakistan:4,India:3,USA:2,Iran:1,UK:0", 
        "India:4,USA:3,Iran:2,UK:1,Pakistan:0" 
    };

    static void Main (string[] args) {


        Result[] r = IterateOverAllCountries();
    }
于 2012-07-14T09:42:14.980 に答える
1

この linq は配列を Dictionary> に変換します。外側の辞書には国名が含まれ、内側の辞書には出現番号 (「:」の後の数字) と各出現のカウントが含まれます。

string[] arr1 = new string[]
                            {
                                "Pakistan:4,India:3,USA:2,Iran:1,UK:0",
                                "Pakistan:4,India:3,USA:2,Iran:1,UK:0",
                                "India:4,USA:3,Iran:2,UK:1,Pakistan:0"
                            };

var count = arr1
    .SelectMany(s => s.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
    .GroupBy(s => s.Split(':')[0], s => s.Split(':')[1])
    .ToDictionary(g => g.Key,
         g =>
         {
              var items = g.Distinct();
              var result = new Dictionary<String, int>();
              foreach (var item in items)
                  result[item] = g.Count(gitem => gitem == item);
              return result;
         });

// print the result
foreach(var country in count.Keys)
{
     foreach(var ocurrence in count[country].Keys)
     {
          Console.WriteLine("{0} : {1} = {2}", country, ocurrence, count[country][ocurrence]);
     }
}
于 2012-07-14T10:47:25.570 に答える
0

使用しているデータ構造は、その情報を提供するのに十分なほど豊富ではありません。sring[][]したがって、文字列を解析し、( )を提供できるように新しいデータ構造を作成する必要があります。

        string[] arr1 = new string[] { 
        "Pakistan,India,USA,Iran,UK",
        "Pakistan,India,USA,Iran,UK", 
        "India,USA,Iran,UK,Pakistan" 
            };

        string[][] richerArray = arr1.Select(x=> x.Split('\'')).ToArray();
        var countPakistanIsFirst = richerArray.Select(x=>x[0] == "Pakistan").Count();

アップデート

あなたはあなたの質問を変えたようです。答えは元の質問に適用されます。

于 2012-07-14T09:40:32.087 に答える