首页 > 试题广场 >

竞选社长

[编程题]竞选社长
  • 热度指数:39459 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
假设你们社团要竞选社长,有两名候选人分别是A和B,社团每名同学必须并且只能投一票,最终得票多的人为社长.

输入描述:
一行,字符序列,包含A或B,输入以字符0结束。


输出描述:
一行,一个字符,A或B或E,输出A表示A得票数多,输出B表示B得票数多,输出E表示二人得票数相等。
示例1

输入

ABBABBAAB0

输出

B
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String str=sc.next();
        int all=str.length()-1;
        String[] arr=str.split("O")[0].split("");
        int count=0;
        for(String s:arr){
            if(s.equals("A")) count++;
        }
        if(count*2>all) System.out.print("A");
        else if(count*2==all) System.out.print("E");
        else System.out.print("B");
    }
}

发表于 2022-08-12 23:43:16 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        int countA = 0;
        int countB = 0;
        for(int i = 0; i < str.length();i++){
            char c = str.charAt(i);
            if(c == 'A'){
                countA++;
            }
            if(c == 'B'){
                countB++;
            }
        }
        if(countA > countB){
            System.out.println("A");
        }else if(countA < countB){
            System.out.println("B");
        }else if(countA == countB){
            System.out.println("E");
        }
    }
}

发表于 2022-07-05 11:11:10 回复(0)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @Title: 竞选社长
 * @Remark: 假设你们社团要竞选社长,有两名候选人分别是A和B,社团每名同学必须并且只能投一票,最终得票多的人为社长.
 *          输入描述:
 *              一行,字符序列,包含A或B,输入以字符0结束。
 *          输出描述:
 *              一行,一个字符,A或B或E,输出A表示A得票数多,输出B表示B得票数多,输出E表示二人得票数相等。
 * @Author: ijunfu
 * @Version: 1.0.0
 * @Date: 2022-03-19
 */
public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<Character, Integer> map = new HashMap<>();
        map.put('A', 0);
        map.put('B', 0);

        char[] data = in.nextLine().toCharArray();

            for(int i=0, len=data.length; i<len; i++) {

                char ele = data[i];
                
                if(!map.containsKey(ele)) break;
                
                Integer count = map.get(ele);

                map.put(ele, ++count);
            }

            Integer a =  map.get('A');
            Integer b = map.get('B');

            if(a > b) {
                System.out.println("A");
            } else if(a == b) {
                System.out.println("E");
            } else {
                System.out.println("B");
            }

        in.close();
    }
}

发表于 2022-03-19 12:04:13 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        int count = 0;
        int calculation = 0;
        for (char ch : str.toCharArray()){
            if (ch == 'A'){
                count++;
            }if (ch == 'B'){
                calculation++;
            }
        }
        if (count > calculation){
            System.out.println("A");
        }else if (count == calculation){
            System.out.println("E");
        }else {
            System.out.println("B");
        }
    }
}

发表于 2021-12-12 01:19:15 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        String vote;
        Scanner sc1 = new Scanner(System.in);
        vote = sc1.nextLine();
        char[] get_=vote.toCharArray();
        int a=0,b=0;
        for(int i=0;i<get_.length-1;i++){
            if(get_[i]=='A'){
                a++;
            }
            else {
                b++;
            }
        }
        if(a>b){
            System.out.println('A');
        }
        else if(b>a) {
            System.out.println('B');
        }
        else {
            System.out.println('E');
        }
    }
}

发表于 2021-11-07 11:13:14 回复(0)
//直接将输入拆分成单个的字符串,再统计
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] votes = in.nextLine().split("");
        int A = 0;
        int B = 0;
        for (String i : votes) {
            if (i.equals("A")) {
                A++;
            } else if (i.equals("B")) {
                B++;
            }
        }
        if (A > B) {
            System.out.println("A");
        } else if (A < B) {
            System.out.println("B");
        } else if (A == B) {
            System.out.println("E");
        }
    }


}

发表于 2021-10-19 18:56:52 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        int countA = 0;
        int countB = 0;
        for(int i=0;i<s.length()-1;i++){
            if(s.substring(i,i+1).equals("A")){
                countA++;
            }
            else{
                countB++;
            }
        }
        if(countA>countB){
            System.out.println("A");
        }
        else if(countA<countB){
            System.out.println("B");
        }
        else{
            System.out.println("E");
        }
    }
}

发表于 2021-09-03 11:17:39 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String [] B = str.split("A");
        String [] A = str.split("B");
        String str1 = "";
        String str2 = "";
        for(String s : A )
        {
            str1 = str1+s;
        }
        for(String s : B )
        {
            str2 = str2+s;
        }
        Integer countA = str1.length();
        Integer countB = str2.length();
        if(countA>countB){
            System.out.println("A");
        }
        else  if(countA<countB){
            System.out.println("B");
        }
        else {
            System.out.println("E");
        }
    }
}
好像是这样吧
发表于 2021-08-15 16:24:38 回复(0)
import java.util.Scanner;
public class Main { 
    public static void main (String [] args) { 
        Scanner sc = new Scanner(System.in);
        String a = new String(sc.next());
        String[] split = a.split("A");
        int len = split.length-1;
        String[] split1 = a.split("B");
        int len1 = split1.length-1;
        if(len > len1) {
            System.out.println("A");
        }else if(len == len1) {
            System.out.println("E");
        }else {
            System.out.println("B");
        }
        }
}
发表于 2021-07-16 20:11:07 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        int sum = 0;
        int countA = 0;
        int countB = 0;

        String vote = scanner.nextLine();
            while (vote.length()!=sum){
                switch (vote.charAt(sum)){
                    case 'A':
                        countA+=1;
                        break;
                    case 'B':
                        countB+=1;
                        break;
                    case '0':
                        break;
                }   sum+=1;

            }

        if(countA==countB){
            System.out.println("E");
        }else{
            System.out.println(countA>countB?"A":"B");
        }
    }
}

发表于 2021-03-25 16:33:07 回复(1)
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
     Scanner scanner=new Scanner(System.in);
     
     while (scanner.hasNext()) {
        String str=scanner.next();
        char[] strs=str.toCharArray();
        int a=0;
        for(int i=0;i<strs.length-1;i++) {
            if(strs[i]=='A') {
                a++;
            }
        }
        if((strs.length-1)-a>a) {
            System.out.println("B");
        }else if((strs.length-1)-a==a) {
            System.out.println("E");
        }else {
            System.out.println("A");
        }
        if(str.contains("0")) {
            break;
        }
     }
    }

}

发表于 2020-06-17 15:45:38 回复(0)
import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str = bf.readLine();
        int Bnum = str.replace("A","").length();
        int Anum = str.replace("B","").length();
        if(Anum>Bnum){
            System.out.println("A");
        }else if(Anum<Bnum){
            System.out.println("B");
        }else{
            System.out.println("E");
        }
    }
}

发表于 2020-04-15 14:37:48 回复(0)
import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        Scanner sc=new Scanner(System.in);
        int aCount=0;
        int bCount=0;
        while(sc.hasNext())
        {
            String str=sc.next();
            for(int i=0;i<str.length()-1;i++)
            {
                char s=str.charAt(i);
                String ss=""+s;//转成字符串类型
                if(ss.equals("A"))
                {
                    aCount++;
                }
                else
                {
                    bCount++;
                }
            }
            if(aCount>bCount)
            {
                System.out.println("A");
            }
            else if(aCount<bCount)
            {
                System.out.println("B");
            }
            else
            {
                System.out.println("E");
            }
        }
    }
}
发表于 2020-03-26 09:55:33 回复(0)