zoukankan      html  css  js  c++  java
  • 学生投票-某班有20个学生,投票选班长,总共有5个候选人,实现投票,并且计算出得票最多的人以及他的票数。

    nt[] tp = new int[20]; //每个人投的票
                int [] ps = new int[5]; //每个候选人所得票数
    
                for (int i = 0; i < tp.Length; i++)
                {
                    Console.WriteLine("请投票,1代表第一个候选人,2代表第二个候选人--5带表第五个候选人");
                    tp[i] = Convert.ToInt32(Console.ReadLine());
                }
    
                for (int j = 0; j < tp.Length; j++)
                {
                    if (tp[j] == 1)
                    {
                        ps[0] = ps[0] + 1;
                    }
                    else if (tp[j] == 2)
                    {
                        ps[1] = ps[1] + 1;
                    }
                    else if (tp[j] == 3)
                    {
                        ps[2] = ps[2] + 1;
                    }
                    else if (tp[j] == 4)
                    {
                        ps[3] = ps[3] + 1;
                    }
                    else if (tp[j] == 5)
                    {
                        ps[4] = ps[4] + 1;
                    }
                }
    
                //看一下谁得的票数最多
                int max = ps[0]; //找出最大的
                for (int x = 1; x < ps.Length; x++)
                {
                    if (max < ps[x])
                    {
                        max = ps[x];
                    }
                }
                //找最大的索引
                int [] sy = new int [5];
                for (int y = 0; y < ps.Length; y++)
                {
                    if (ps[y] == max)
                    {
                        sy[y] = y+1;
                    }
                }
    
                //输出
    
                Console.WriteLine("所得票数最多为:"+max);
                Console.WriteLine("所得票数最多的人为:");
                for (int z = 0; z < sy.Length; z++)
                {
                    if (sy[z] == 0)
                    {
                    }
                    else
                    {
                        Console.WriteLine(sy[z]);
                    }
                }
    
    
    
                Console.ReadLine();
  • 相关阅读:
    POJ 3093 Margaritas on the River Walk(背包)
    BZOJ 2287 【POJ Challenge】消失之物(DP+容斥)
    WC2017 Day1
    WC2017 Day0
    WC2017 Conclusion
    WC2017 Day6
    UOJ #58 糖果公园
    WC2017 Day5
    codevs 1946 阿狸的打字机
    HDU 2457 DNA_repair
  • 原文地址:https://www.cnblogs.com/franky2015/p/4634493.html
Copyright © 2011-2022 走看看