首页 > 试题广场 >

考试成绩

[编程题]考试成绩
  • 热度指数:1325 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
现在你的班级刚刚参加了一个只有单选题的考试。班级一共n个学生,考试有m个问题。每个题目都有5个可选答案(A,B,C,D,E)。并且每个题目只有一个正确答案。每个题目的分数并不一样,第i个题目的分数用a[i]表示。如果题目没答对该题会获得0分。
考试结束后,每个学生都记得自己的答案,但是他们还不知道正确答案是什么。如果非常乐观的考虑,他们班级最多可能得到多少分呢?

输入描述:
第一行包含2个正整数,n和m。(1 <= n, m <= 1000,n是班级中学生数量,m是题目的数量)

下面n行数据每行包含一个string si,表示第i个学生的答案。si的第j个字符表示该学生第j个题目的答案。


输出描述:
一个正整数,全班学生最大的可能获得的分数总和。
示例1

输入

2 4
ABCD
ABCE
1 2 3 4

输出

16

说明

最优的答案是ABCD,这样2个学生的总分是16。
示例2

输入

3 3
ABC
BCD
CDE
5 4 12

输出

21

说明

最优的答案是CCC,3个学生的总分是5+4+12=21
max求解就行了
import java.util.*;

public class Main {
    public static void main(String[] arg){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        String[] sl = new String[n];
        for (int i=0;i<n;i++){
            sl[i] = in.next();
        }
        int total = 0;
        for (int i=0;i<m;i++){
            int sc = in.nextInt();
            int max=0;
            int[] d = new int[5];
            for(String s : sl){
                int index = (int)s.charAt(i)-(int)'A';
                d[index]++;
                max = Math.max(max,d[index]);
            }
            total += (max*sc);
        }
        System.out.println(total);
    }
}


发表于 2020-08-25 11:23:45 回复(0)
使用HashMap求每一个题目答案相同的最大人数
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        sc.nextLine();
        char[][] ans=new char[n][m];
        for(int i=0;i<n;i++){
            String s=sc.nextLine();
            ans[i]=Arrays.copyOf(s.toCharArray(),m);
        }
        int[] score=new int[m];
        for(int i=0;i<m;i++){
            score[i]=sc.nextInt();
        }
        int res=0;
        for(int j=0;j<m;j++){
            int tmp=0;
            HashMap<Integer,Integer> map=new HashMap<>();
            for(int i=0;i<n;i++){
                int key=ans[i][j]-'A';
                if(map.containsKey(key)){
                    int t=map.get(key);
                    map.put(key,t+1);
                    tmp=Math.max(tmp,t+1);
                }else{
                    map.put(key,1);
                    tmp=Math.max(1,tmp);
                }
            }
            res+=tmp*score[j];
        }
        System.out.println(res);
    }
}


发表于 2020-04-11 17:13:37 回复(0)
重点在于求班级同学每道题公共答案最多的选项然后依次乘以score里每道题的分值就是最大和了.
Java实现如下:
import java.util.*;
public class Main {
    public static int MaxScore(int n, int m, String[] answer, int[] score) {
        int res = 0;
        int index = 0;
        for (int i = 0; i < m; i++) {
            /*每道题有5个答案*/
            int[] count = {0, 0, 0, 0, 0};
            int max = 0;
            for (int j = 0; j < n; j++) {
                if (answer[j].charAt(i) == 'A') {
                    max = max > ++count[0] ? max : count[0];
                } else if (answer[j].charAt(i) == 'B') {
                    max = max > ++count[1] ? max : count[1];
                } else if (answer[j].charAt(i) == 'C') {
                    max = max > ++count[2] ? max : count[2];
                } else if (answer[j].charAt(i) == 'D') {
                    max = max > ++count[3] ? max : count[3];
                } else if (answer[j].charAt(i) == 'E') {
                    max = max > ++count[4] ? max : count[4];
                }
            }
            res += max * score[index++];
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();
        String[] str = new String[n];
        int[] score = new int[m];

        for (int i = 0; i < n; i++) {
            str[i] = sc.next();
        }
        for (int i = 0; i < m; i++) {
            score[i] = sc.nextInt();

        }
        System.out.println(MaxScore(n, m, str, score));
    }
}



发表于 2020-04-08 16:14:24 回复(0)
我不理解这道题目的意思
发表于 2020-03-23 08:20:03 回复(0)
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
	 public static void main(String[] args) {
		 	Scanner sc = new Scanner(System.in);
		 	String l1 = sc.nextLine();
		 	//学生和题目数量存储在num中  学生[0]  题目[1]
		 	String [] num = l1.split(" ");
		 	int num_student = Integer.parseInt(num[0]);
		 	int num_question = Integer.parseInt(num[1]);
		 	String [] final_answer = {"A","B","C","D","E"};
		 	String [] answer = new String[num_student];
		 	int score=0;
		 	for(int i=0;i<num_student;i++) {
		 		String temp = sc.nextLine();	
		 		answer[i]=temp;
		 	}
		 	String temp = sc.nextLine();
		 	String [] str_score = temp.split(" ");
		 	//里面存储了各个题的分值
		 	int [] count = {0,0,0,0,0,0};
		 	for(int i=0;i<num_question;i++) {
                //每次重新赋值
		 		for(int l=0;l<count.length;l++) {
		 			count[l]=0;
		 		}
		 		for(int k=0;k<num_student;k++) {
		 			count[answer[k].charAt(i)-'A']+=1;
		 			//count为该题的统计,找出其中数量最多的即可
		 		}
		 		//找出其中数量最大的
		 		int local=0;
	 			int max = count[local];
	 			for(int p=1;p<count.length;p++) {
	 				if(count[p]>max) {
	 					max=count[p];
	 					local = p;
	 				}
	 			}
	 			score+=max*(Integer.parseInt(str_score[i]));
		 	} 
		 	System.out.println(score);
	 }

}
拿java写实在是太复杂了
发表于 2020-03-18 12:05:43 回复(0)
//复杂度为O(n^2)的无脑暴力法:把所有答案记录到char[][] paper中,按列检查最多的答案数放到ans[]中,
//乘以对应积分,遍历m个考试题得到最高分
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int stuNum = sc.nextInt();
            int quesNum = sc.nextInt();
            char[][] paper = new char[stuNum][quesNum];
            for (int i = 0; i < stuNum; i++) {
                String str0 = sc.next();
                for (int j = 0; j < quesNum; j++) {
                    paper[i][j] = str0.charAt(j);
                }
            }
            int[] score = new int[quesNum];
            for (int i = 0; i < quesNum; i++) {
                score[i] = sc.nextInt();
            }
            int res = 0;
            for (int j = 0; j < quesNum; j++) {
                int[] ans = new int[5];
                for (int i = 0; i < stuNum; i++) {
                    ans[paper[i][j] - 'A']++;
                }
                int max = 0;
                for (int t = 0; t < 5; t++) {
                    max = Math.max(max, ans[t]);
                }
                res += max * score[j];
            }
            System.out.println(res);
        }
    }
}

发表于 2020-05-03 00:29:03 回复(0)
二维数组(ABCDE维度和题目序号维度)存放答案的出现次数,然后将每题出现ABCDE的最大的次数拿出来乘以改题的分数,然后累加所有题目
import java.util.Scanner;
import java.util.Arrays;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int studentNum = in.nextInt();
        int questionNum = in.nextInt();
        int[][] count = new int[questionNum][5];
        int[] scoreArr = new int[questionNum];
         
        int sum = 0;
        String[] studentAns = new String[studentNum];
        //答案初始化
        for(int i =0;i<studentNum;i++) {
            studentAns[i] = in.next();
        }
        //分数数组初始化
        for(int i=0;i<questionNum;i++) {
            scoreArr[i] = in.nextInt();
        }
        //遍历字符串数组
        for(int i=0;i<studentNum;i++) {
            for(int j =0;j<questionNum;j++) {
                if(studentAns[i].charAt(j)=='A') {
                    count[j][0] ++;
                }else if(studentAns[i].charAt(j)=='B') {
                    count[j][1] ++;
                }else if(studentAns[i].charAt(j)=='C') {
                    count[j][2] ++;
                }else if(studentAns[i].charAt(j)=='D') {
                    count[j][3] ++;
                }else {
                    count[j][4] ++;
                }
            }
        }
         
         
        for(int line =0;line<questionNum;line++) {
            Arrays.sort(count[line]);
            int maxK = count[line][4];
            sum += maxK*scoreArr[line];
        }
        System.out.println(sum);
         
    }
}

编辑于 2020-04-18 13:35:30 回复(0)

将选择人数最多的选项作为答案,查看题解请移步至 ABC

发表于 2024-06-23 14:10:16 回复(0)
#include <iostream>
#include <vector>
#include <map>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    
    vector<string> s(n,"");
    for(int i=0;i<n;++i) cin>>s[i];
    
    vector<int> value(m,0);
    for(int j=0;j<m;++j) cin>>value[j];
    
    vector<int> max_ch_num;
    for(int j=0;j<m;++j){
        map<char,int> mp;
        for(int i=0;i<n;++i) mp[s[i][j]]++;
        
        int max_value=0;
        for(auto it=mp.begin();it!=mp.end();++it)
            max_value=max_value>it->second?max_value:it->second;
        max_ch_num.push_back(max_value);
    }
    
    int res = 0;
    for(int i=0;i<m;++i)
        res+=value[i]*max_ch_num[i];
    cout<<res;
    return 0;
}
我理解的题目大意是,给一个矩阵(n*m),然后每列都会有一个值,然后你要计算出,这个列的值最多能出现多少次,最后算出总体值。也就是 “选定某个单选题,计算出选A,B,C,D的人数,哪个选项人数最多,挑出来后乘以选项值” ,把全部加起来就得到最后答案了。

解决思路就是找出一列中最多出现哪个选项以及对应次数,然后找出所有的,乘以对应的值。
发表于 2023-03-10 14:39:59 回复(0)
// 哈希表,计算每一列不同字母的最大个数maxNum, 则该题的最大分数为 maxNum乘以该题的分数
// 遍历每一题,将每题分数累加即可
// 注意: 对于每一题,需清空哈希表,避免受上一题数据的影响
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n, m;
    unordered_map<char, int> mp;
    int maxScore = 0;
    while (cin >> n >> m) {
        vector<vector<char>> charVec(n, vector<char>(m, '0'));
        char c;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> charVec[i][j];
            }
        }
        vector<int> score(m, 0);
        int x;
        for (int i = 0; i < m; i++) {
            cin >> score[i];
        }
        for (int j = 0; j < m; j++) {
            int maxNum = 0;
            mp.clear();
            for (int i = 0; i < n; i++) {
                mp[charVec[i][j]]++;
                maxNum = max(maxNum, mp[charVec[i][j]]);
            }
            maxScore += maxNum * score[j];
        }
      }
    cout << maxScore;
    return 0;
}
发表于 2020-09-22 21:43:23 回复(0)
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> ans(m, vector<int>(5, 0));
    for (int i = 0; i<n; i++)
    {
        string  t;
        cin >> t;
        for (int j = 0; j<m; j++)
        {
                 ans[j][t[j] - 'A']++;

        }
    }
    vector<int> fenzhi(m);
    for (int i = 0; i<m; i++)
        cin >> fenzhi[i];
    int result = 0;
    for (int i = 0; i<m; i++)
    {
        int mt=0;
        for (int j = 0; j<5; j++)
        {
            mt = max(mt, ans[i][j]);
        }
        result += mt*fenzhi[i];
    }
    cout << result << endl;
    return  0;
}
发表于 2020-08-26 16:35:18 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int [][] ans = new int[m][5];
        for(int i = 0; i < n; i++){
            String line = in.next();
            int j = 0;
            for(char c:line.toCharArray()){
                ans[j++][c-'A'] ++;
            }
        }
        int [] score = new int[m];
        for(int i = 0;i < m; i ++)
            score[i] = in.nextInt();
        int result = 0;
        for(int i = 0; i < m; i++){
            int max = ans[i][0];
            for(int j =1; j < 5; j++ ){
                max = Math.max(max,ans[i][j]);
            }
            result+=max*score[i];
        }
        
        System.out.println(result);
    }
}

发表于 2020-08-03 23:37:10 回复(0)
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map> 
#include <algorithm>
using namespace std;
int func(int pep,int g_nums,vector<int>goods, vector<string>str)
{
    //遍历列;
    int ret = 0;
    for (int i = 0; i < g_nums; ++i)
    {
        unordered_map<char, int> mem;
        int max = 0;
        for (int j = 0; j < pep; ++j)
        {
            mem[str[j][i]]++;
            if (max < mem[str[j][i]])
            {
                max = mem[str[j][i]];
            }
        }
        ret += (max*goods[i]);
    }
    return ret;
}
int main()
{
    int num = 0;
    int num1 = 0;
    while (cin >> num>>num1)
    {
        vector<string> str;
        vector<int> goods;
        for (int i = 0; i < num; ++i)
        {
            string tmp;
            cin >> tmp;
            str.push_back(tmp);
        }
        for (int i = 0; i < num1; ++i)
        {
            int good = 0;
            cin >> good;
            goods.push_back(good);
        }
        cout<<func(num, num1, goods, str);
    }
    system("pause"); 
    return 0;
}

 
发表于 2020-07-08 20:59:31 回复(0)
//求每道题最大相同数量 * 分数即可 不知道有没有更优的解法,反正这个通过
#include<bits/stdc++.h>
using namespace std;
 
struct Node{
    int i;
    int score;
    Node(){
         
    }
    Node(int _i, int _score):i(_i),score(_score){}
    friend bool operator<(Node n1,Node n2){
        return n1.score <= n2.score;
    }
};
 
class Solution{
  public:
     int maxScore(vector<string> arr,set<Node> st){
        set<Node>::iterator iter;
        unordered_map<char,int> mp;
        int res = 0;
        int sum = 0;
        for(iter = st.begin();iter !=st.end();iter++){
            for(int i=0;i<arr.size();i++){
               mp[arr[i][(*iter).i]]++;
               res = max(res, mp[arr[i][(*iter).i]]);
            }
            //计算出有多少个;
            sum += (*iter).score * res;
            res = 0;
            mp.clear();
        }
        return sum;
     }
     
};
 
int main(){
    int n,m;
    cin>>n>>m;
    vector<string> arr(n);
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    set<Node> st;
    int score;
    for(int i=0;i<m;i++){
        cin>>score;
        st.insert(Node(i,score));
    }
    Solution c1;
    cout<<c1. maxScore(arr,st)<<endl;
    return 0;
     
}

发表于 2020-06-07 19:10:19 回复(0)
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String[] strs = sc.nextLine().split(" ");
        int n = Integer.parseInt(strs[0]);//学生数量
        int m = Integer.parseInt(strs[1]);//题目数量
        int[] times = new int[5];
        String[] answers = new String[n];
        for (int i = 0; i < n; i++) {
            answers[i] = sc.nextLine();
        }
        int[] scores = new int[m];
        strs = sc.nextLine().split(" ");
        for (int i = 0; i < m; i++) {
            scores[i] = Integer.parseInt(strs[i]);
        }
        int sumScores = 0;
        for (int i = 0; i < m; i++) {
            int maxTimes = Integer.MIN_VALUE;
            for (int j = 0; j < n; j++) {
                if (answers[j].charAt(i) == 'A') {
                    times[0]++;
                    maxTimes = Math.max(maxTimes, times[0]);
                } else if (answers[j].charAt(i) == 'B') {
                    times[1]++;
                    maxTimes = Math.max(maxTimes, times[1]);
                } else if (answers[j].charAt(i) == 'C') {
                    times[2]++;
                    maxTimes = Math.max(maxTimes, times[2]);
                } else if (answers[j].charAt(i) == 'D') {
                    times[3]++;
                    maxTimes = Math.max(maxTimes, times[3]);
                } else if (answers[j].charAt(i) == 'E') {
                    times[4]++;
                    maxTimes = Math.max(maxTimes, times[4]);
                }
            }
            sumScores += maxTimes * scores[i];
            Arrays.fill(times,0);
        }
        System.out.println(sumScores);
    }

}

发表于 2020-04-11 15:58:18 回复(0)
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static int f(int n, int m, String[] answer, int[] scores) {

        int res = 0;
        char[][] a = new char[n][m];
        int[][] book = new int[5][m];



        for (int i = 0; i < answer.length; i++) {
            String aAnswer = answer[i];
             for (int j = 0; j < aAnswer.length(); j++) {
                a[i][j] = aAnswer.charAt(j);

            }
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                book[a[j][i] - 65][i] += 1;
            }

        }
        for (int i = 0; i < m; i++) {
            int max = Integer.MIN_VALUE;
            for (int j = 0; j < 5; j++) {
                max = max < book[j][i] ? book[j][i] : max;
            }
            res += scores[i] * max;
        }
        return res;


    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();
        String[] a = new String[n];
        int[] score = new int[m];

        for (int i = 0; i < n; i++) {
            a[i] = sc.next();
        }
        for (int i = 0; i < m; i++) {
            score[i] = sc.nextInt();

        }

        System.out.print(f(n, m, a, score));


    }
}

发表于 2020-04-01 22:20:14 回复(0)
#include <iostream>

using namespace std;

int point[10010];
int a[10010][5];

int main()
{
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
        {
            char t;
            cin >> t;
            a[j][t-'A']++;
        }
    int res = 0;
    for(int j = 0; j < m; j++)
    {
        int mm = 0;
        cin >> point[j];
        for(int k = 0; k <= 4; k++)
            mm = max(mm, a[j][k]);
        res += mm*point[j];
    }
    cout << res << endl;
    return 0;
}

发表于 2020-04-01 17:42:24 回复(0)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 思路:每道题每个选项出现的次数最多即为此题最理想答案,如果平均则随机选一个为正确答案
 */
public class Main {
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n,m;
        n=scanner.nextInt();
        m=scanner.nextInt();
        //存放学生自己的答案
        String[] Ansers=new String[n];
        for(int i=0;i<n;i++){
            Ansers[i]=scanner.next();
        }

        //存放每个题的分值
        int[] points=new int[m];
        for(int j=0;j<m;j++){
            points[j]=scanner.nextInt();
        }
        //建立二维数组方便统计每道题
        char[][] p=new char[n][m];
        for(int i=0;i<n;i++){
            char[] str=Ansers[i].toCharArray();
            p[i]=str;
        }
        int[] flag;//因为每个题最多只有五个答案,标记A--E每个选项出现的次数
        char[] anser=new char[m];//记录选定的标准答案
        int maxValue;
        int maxIndex;
        int w=0;

        //统计每一列(题)每个选项出现的最大次数,并且标记此答案
        for(int j=0;j<m;j++){
            maxValue=0;
            maxIndex=0;
            flag=new int[5];
            for(int i=0;i<n;i++){
                flag[p[i][j]-'A']++;
            }
            for(int x=0;x<5;x++){
                if(flag[x]>maxValue){
                    maxValue=flag[x];
                    maxIndex=x;
                }
            }
            anser[w]=(char) (maxIndex+'A');
            w++;
        }
//        System.out.println(anser);
        //计算最终分数
        int sumPoints=0;
        for(int j=0;j<m;j++){ //列
            for (int i=0;i<n;i++){ //行
                if(p[i][j]==anser[j])
                    sumPoints=sumPoints+points[j];
            }
        }
        System.out.println(sumPoints);
    }
}

发表于 2020-03-26 16:35:46 回复(0)
#include <iostream>
(720)#include <string>
#include <vector>
(721)#include<algorithm>
using namespace std;
class Solution{
    public:
        long long test(int n, int m, vector<string> s, vector<int> score)
        {
            long long res = 0;
            for (int i=0;i<m;i++)
            {
                vector<int> temp(5);// ABCDE五个答案
                for (int j=0;j<n;j++)
                {
                    temp[s[j][i]-'A']++;
                }
                int most_ans = *max_element(temp.begin(),temp.end());
                // sort(temp.begin(),temp.end());
                // int most_ans = temp.back();
                res+=most_ans*score[i];
            }
            return res;
        }
};
 
int main()
{
    int n;
    int m;
    cin >> n >> m;
    vector<string> s(n);
    for (int i = 0; i < n; i++)
    {
        cin >> s[i];
    }
    vector<int>score(m);
    for (int j = 0; j < m; j++)
        cin >> score[j];
    long long res = Solution().test(n,m,s,score);
    cout << res << endl;
    return 0;
}

发表于 2020-03-20 23:24:36 回复(0)
public static void testCalcScore(){
//        int n = 2,m = 4;
//        String[] results = {"ABCD","ABCE"};
//        int[] scores = {1,2,3,4};
//        day01.score(n,m,results,scores);

        int n = 3,m = 3;
        String[] results = {"ABC","BCD","CDE"};
        int[] scores = {5,4,12};
        day01.calcScore(n,m,results,scores);
    }
    public long calcScore(int n,int m,String[] results,int[] scores){
        Map<Character,Integer>[] map = new HashMap[m];
        long totalScore = 0;
        StringBuilder sb = new StringBuilder();
        for (String result:results){
            for (int i=0;i<result.length();i++){
                Character option = result.charAt(i);
                if(map[i] == null) map[i] = new HashMap<>();
                if(map[i].containsKey(option)) {
                    map[i].computeIfPresent(option, (k,v) -> v + 1);
                }else {
                    map[i].put(option, 1);
                }
            }
        }

        for (int i=0;i<scores.length;i++){
            Optional<Map.Entry<Character, Integer>> resultEntry = map[i].entrySet().stream().sorted((a, b) -> b.getValue() - a.getValue()).findFirst();
            if(resultEntry.isPresent()){
                sb.append(resultEntry.get().getKey());
                totalScore += scores[i] * resultEntry.get().getValue();
            }
        }
        System.out.println(String.format("最优的答案是%s,这样%s个学生的总分是%s。",sb.toString(),n,totalScore));
        return totalScore;
    }

发表于 2020-03-18 11:23:02 回复(0)