首页 > 解决方案 > C# Voting Simulator(如何计算零或未投票的项目/国家)

问题描述

我正在使用 Eurovision 投票竞赛模拟器。

26个国家要随机投票给其他10个国家(不重复也不自己)。

所以我做了一个 for (Countries.Length) 和一个 for (PossibleVotes.Length) 来分配选票。

甚至处理投票最高(12 分)的计数器以显示“最佳”获胜者。

这已经完成了。

编码:

//struct array country[26] 
//country[].sName = Spain, Italy, France...
//country.iVotes = Total votes
//country.iTwelves = Counter for total12 recieved
//country.iZeros = Counter for total 0 recieved
// iPossibleVotes[10] {12 , 10 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 }

    for (int i = 0 ; i < country.Length ; i++){
      
      Console.WriteLine("\n___Country " + (i+1) + " " + country[i].sName + "___");
      Console.WriteLine("\nVotes:");
      
      int[] iVotedCountry = Utils.RandomArrayWitoutDuplicates(i);
      //Function to make an array of 10 random integers without duplicates nor itself (i value)
      

      //Executes 10 times
      for (int j = 0 ; j < iVotedCountry.Length ; j++){

        Console.WriteLine("-" + country[iVotedCountry[j]].sName + " with " + iPossibleVotes[j] + " points. ");
        
//If j = 0 equals to iPossibleVotes[] =12, sum iTwelves counter
        if (j == 0){
          country[iVotedCountry[j]].iTwelves++;
        }


   

      } // END FOR (iVotedCountry.Length) Countries that are being voted.

      for (int k = 0 ; k < country.Length ; k++){

          if (k != iVotedCountry[j]) {

            country[k].iZeros++;  

          }

        }


 } //END COUNTRY.Length ARRAY

//Expected output
Console.WriteLine("___TheLooser___\nThe country or countries with more zeros recieved are: " );

//Then sort them in an array
//BUT I can't handle to count the times a country hasn't been voted or voted '0'

老师还要求显示“TheLooser”获胜者,即投票较少或投票为“0”的国家(或平局的国家)。

我的想法是在将真正的 PossibleVotes 分配给 10 个随机国家之后,将“0”票分配给其他 16 个国家。

我也对伪代码抽象的想法或评论感兴趣,可以考虑一下。

谢谢

标签: c#arraysstructvoting

解决方案


考虑这种方法,而不是保留每个国家(例如 iVotes、iZeroes、iTwelves)的票数总和和零个数。

为每个国家保留所有选票。例如,如果有 10 轮投票,那么您可以有一个包含 10 个元素的数组,每个元素保存该轮的分数,例如 12、10、8 等,默认值为 0。

然后你有一个由 3 个部分组成的程序。

  1. 记录所有投票

  2. 将每个国家/地区的选票相加,并在考虑平局的情况下找到最高的选票

  3. 将每个国家/地区的选票相加,并再次考虑平局,找到最低的国家

如果您愿意,第 2 部分和第 3 部分可以在 1 个循环中,但不要更干净。


推荐阅读