【问题标题】:C# Voting Simulator (How to count zeros or non-voted items/countries)C# 投票模拟器(如何计算零或未投票的项目/国家)
【发布时间】:2021-05-06 14:28:17
【问题描述】:

我正在使用 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 个国家。

我也对伪代码抽象思想或cmets感兴趣,可以考虑一下。

谢谢

【问题讨论】:

    标签: c# arrays struct voting


    【解决方案1】:

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

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

    那么你有一个分为 3 个部分的程序。

    1. 记录所有投票

    2. 对每个国家/地区的投票求和 并找到最高的那个,考虑到平局

    3. 将每个国家/地区的选票相加,找出最低的那个, 再次,考虑到关系

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多