zoukankan      html  css  js  c++  java
  • 15-07-08 数组-- 投票选班长

    每班共有20个学生,投票选班长,总共有5个候选人,实现投票,并计算出得票最多的人以及他的票数

    int [] tp = new int [20];    //每个人投的票
                int [] ps = new int[5];    //每个人所得的票数
    
                //投票
                for (int i = 0; i < tp.Length; i++)
                {
                    Console.WriteLine("支持一号请按1,支持二号请按2,支持三号请按3,支持四号请按4,支持五号请按5");
                    tp[i] = Convert.ToInt32(Console.ReadLine());
                }
                
                //找出每个候选人所得的票数
                for (int j = 0; j < tp.Length; j++)
                {
                    if (tp[j] == 1)    //投1的话第一位同学加一票
                    {
                        ps[0] = ps[0] + 1;
                    }
                    else if (tp[j] == 2)    //投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];
                    }
                }
    
                //输出
                Console.WriteLine("所得票数最多为:"+max);
                Console.Write("所得票数最多的人为:");
    
                for (int y = 0; y < ps.Length; y++)
                {
                    if (ps[y] == max)
                    {
                        Console.Write(y+1+"");
                    }
                }
    
    
                Console.ReadLine();
  • 相关阅读:
    你所不知道的React Hooks
    DataRow的RowState属性变化
    gRPC详解
    Protobuf简明教程
    docker容器内没有权限
    Locust高并发情况下的性能优化与分布式场景的应用
    Docker容器日志打满机器的解决方式
    Django单测详解
    使用Flask+uwsgi+Nginx部署Flask正式环境
    Locust-分布式执行
  • 原文地址:https://www.cnblogs.com/jia520110270/p/4649790.html
Copyright © 2011-2022 走看看