考试成绩统计

案例描述:有三名同学的考试成绩,分别输出三名同学的总成绩

语文数学英语
张三100100100
李四9050100
王五607080
#include<iostream>
#include<string>
using namespace std;

int main()
{
    //1、创建二维数组
    int scores[3][3] =
    {
        {100,100,100},
        {90,50,100},
        {60,70,80}
    };

    string name[3] = { "张三","李四","王五" };
    //2、统计每个人的总分
    for (int i = 0; i < 3; i++)
    {
        int sum = 0;
        for (int j = 0; j < 3; j++)
        {
            sum += scores[i][j];
        }
        cout << name[i] << "的总分为:" << sum << endl;
    }

    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
假设有一份包含学生姓名、科目和成绩的文件,格式如下: ``` Alice,Math,90 Bob,Math,80 Alice,English,85 Bob,English,95 Charlie,Math,75 Charlie,English,80 ``` 我们可以使用MapReduce来统计每个同学总成绩。下面是一个简单的MapReduce程序: ```java import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class ExamScore { public static class ScoreMapper extends Mapper<Object, Text, Text, IntWritable>{ private Text name = new Text(); private IntWritable score = new IntWritable(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { String[] fields = value.toString().split(","); name.set(fields[0].trim()); // 学生姓名 score.set(Integer.parseInt(fields[2].trim())); // 成绩 context.write(name, score); } } public static class ScoreReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context ) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); // 累加成绩 } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "exam score"); job.setJarByClass(ExamScore.class); job.setMapperClass(ScoreMapper.class); job.setCombinerClass(ScoreReducer.class); // 使用Combiner优化 job.setReducerClass(ScoreReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 在这个程序,我们定义了两个类`ScoreMapper`和`ScoreReducer`,分别对应Map和Reduce任务。在Map任务,我们将每个学生的姓名作为key,将其成绩作为value输出。在Reduce任务,我们累加每个学生的成绩,并将其输出。我们还使用了一个Combiner来优化程序性能,将Map任务输出间结果进行合并。最后,在`main`函数,我们指定了输入文件和输出文件的路径,并启动了MapReduce任务。 运行这个程序的命令如下: ```bash hadoop jar ExamScore.jar ExamScore input output ``` 其,`ExamScore.jar`是打包好的程序包名,`input`是输入文件路径,`output`是输出文件路径。运行成功后,输出文件将包含每个同学的姓名和总成绩

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mister张!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值