编程实践5-7

编程实践5-7

课程元素类型 任务

统计选票。100位村民投票,从3位候选人中选出村长和村支书。规定得票最高者为村长,次高者为村支书。统计时,输入得票候选人的姓氏,输出投票结果。

总结:大于两个的数据,尽量用数组,写程序要考虑到拓展,要程序拥有良好的拓展性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sj5_7
{
    class Program
    {
        static void Main(string[] args)
        {
            string strTem;
            string[] strB;
            int[] intA;
            int intTem;

            intA = new int[3];
            strB = new string[3];

            strB[0] = "Z";
            strB[1] = "L";
            strB[2] = "W";

            Console.WriteLine("请输入投票:");
            strTem = Console.ReadLine();
            string[] strA = strTem.Split(' ');

            for (int i = 0; i < strB.Length; i++)
            {
                for (int j = 0; j < strA.Length; j++)
                {
                    if (strB[i] == strA[j])
                    {
                        intA[i]++;
                    }
                }
                
            }

            for (int i = 0; i <intA.Length; i++)
            {
                for (int j = i + 1; j < intA.Length; j++)
                {
                    if (intA[i] < intA[j])
                    {
                        intTem = intA[i];
                        intA[i] = intA[j];
                        intA[j] = intTem;

                        strTem = strB[i];
                        strB[i] = strB[j];
                        strB[j] = strTem;
                    }
                }
            }

            Console.WriteLine("村长是:{0},得票{1}",strB[0],intA[0]);
            Console.WriteLine("村支书是:{0},得票{1}", strB[1], intA[1]);
            Console.ReadKey(true);
        }
    }
}

 

转载于:https://www.cnblogs.com/Wzqa/archive/2012/11/28/2792021.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值