牛客竞赛题库 选村长

文章目录


【题目描述】
【思路】
【代码】

【题目描述】

链接:选村长题目!!

牛村正在进行村长选举,投票人一人一票,牛可乐是候选人A(投票代号为1)、牛能是候选人B(投票代号为2)和牛妹是候选人C(投票代号为3),其他投票代号无效。
投票列表用以-1表示结束,请分别统计出他们的得票数。谁的得票超过全部投票人数的半数,就输出谁当选为村长,如“A-yes”表示A当选,全部不过半数,输出“all-NO”。

输入描述:
输入一行,若干个整数,以-1结尾
输出描述:
输出五行,前三行表示三个候选人的表票数,然后是总的有效票数,最后一行表示选举结果。

输入
1 1 3 2 1 3 2 1 3 3 1 2 4 1 4 1 2 1 2 1 1 -1

输出
A=10
B=5
C=4
Tot=19
all-NO

【思路】
这道题其实挺简单的,但我真是粗心大意,本来已经输入了一个元素x,又输入了一遍…… 把我整无语了……

说一下思路, 就是用数组计数法就可以了,

也要注意一下输入(我就是输入错了,整半天才发现),因为是以-1结束,所以用while()循环输入,然后去判断x!=-1这件事,在这同时,也要将x出现的次数统计出来(这部分的代码):

while(cin>>x&&x!=-1){
	if(x==-1) break;//如果x=-1,那就停止循环
	a[x]++;//统计x出现的次数
	
	//统计输入的x的总个数
	//因为题目说"谁的得票超过全部投票人数的半数"
 	//是全部投票人数,所以用一个变量n来统计这个
	n++;
}

然后还有一个注意点,题目说超过总投票数的人当村长,所以在判断时,应该写>(大于),而不是>=(大于等于)。

注意点说完(其实也是蛮简单的 ),
【代码】

#include <bits/stdc++.h>
using namespace std;
int x,n,a[1010],s,t,ca,cb,cc;
int main(){
    while(cin>>x&&x!=-1){
        if(x==-1) break;
        a[x]++;
        n++;
    }
    for(int i=1;i<=3;i++){
        if(a[i]!=0){
            s=s+a[i];
            if(i==1){
                cout<<"A="<<a[i]<<endl;
                ca=a[i];
            }
            if(i==2){
                cout<<"B="<<a[i]<<endl;
                cb=a[i];
            }
            if(i==3){
                cout<<"C="<<a[i]<<endl;
                cc=a[i];
            }
        }
    }
    cout<<"Tot="<<s<<endl;
    t=n/2;
    if(ca>t){
    	cout<<"A-yes";
	}else if(cb>t){
		cout<<"B-yes";
	}else if(cc>t){
		cout<<"C-yes";
	}else{
		cout<<"all-NO";
	}
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优秀班委举系统 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 优秀班委举 { public partial class Form1 : Form { private ListBox listBox; public Form1() { InitializeComponent(); } public Form1(ListBox listBox) { InitializeComponent(); this.listBox = listBox; } private void button1_Click(object sender, EventArgs e) { if (listBox1.Items.Count > 0) { if (listBox1.SelectedItem == null) { MessageBox.Show("请先择一个对象!"); } else { //获取左边listBox1中中的内容 string cont = listBox1.SelectedItem.ToString(); //左边listBox1中删除中的内容 listBox1.Items.Remove(cont); //把获得的内容加入到右边的listBox2中 listBox2.Items.Add(cont); } } } private void button2_Click(object sender, EventArgs e) { if (listBox2.Items.Count > 0) { if (listBox2.SelectedItem == null) { MessageBox.Show("请先择一个对象!"); } else { //获取右边listBox2中中的内容 string cont = listBox2.SelectedItem.ToString(); //右边listBox2中删除中的内容 listBox2.Items.Remove(cont); //把获得的内容加入到左边的listBox1中 listBox1.Items.Add(cont); } } } //退出 private void button5_Click(object sender, EventArgs e) { Application.Exit(); } //显示结果 private void button4_Click(object sender, EventArgs e) { //传递listBox2到Form2窗口中 Form2 form2 = new Form2(listBox2); form2.Show(); } //修改名单 private void button3_Click(object sender, EventArgs e) { if (listBox2.SelectedItem == null) { MessageBox.Show("请先择一个对象!"); } else { string cont = listBox2.SelectedItem.ToString(); int index = listBox2.SelectedIndex; Form3 form3 = new Form3(cont, index, listBox2); form3.Show(); this.Hide(); } } //加载窗口 private void Form1_Load(object sender, EventArgs e) { if (listBox != null) { foreach (string str in listBox.Items) { listBox2.Items.Add(str); } } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值