年龄几何:张三、李四、王五、刘六的年龄成一等差数列,他们四人的年龄相加是26,相乘是 880,求以他们的年龄为前4项的等差数列的前20项。

#include<stdio.h>

int main(){

      int a,b,c,d,x,n;

      for(a=1;a<26;a++)

             for(b=1;b<a;b++)

                    for(c=1;c<b;c++)

                           for(d=1;d<c;d++){

                                  if(a+b+c+d==26&&a*b*c*d==880){

                                         printf("%d,%d,%d,%d\n",a,b,c,d);

                                        

                                  }

                           }

      return 0;

      }

#include<stdio.h>

int main(){

       int a=2,x,n=3,i;

       printf("%d\n",a);

       x=a+n;

       printf("%d\n",x);

       for(i;i<=18;i++){

              x=x+n;

              printf("%d\n",x);

       }

       return 0;

       }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现思路: 1. 将原始文件中的每一行按照空格分割,得到学生姓名和成绩。 2. 将学生姓名作为 Map 的 key,成绩作为 value,将数据进行 Map 操作,输出 <key, value> 键值对。 3. 在 Reduce 阶段,对于每一个 key(即学生姓名),将其对应的所有 value(即成绩)进行累加求和,并记录对应的数量。最后计算平均成绩并输出。 代码实现: Mapper: ```java public class ScoreMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private Text name = new Text(); private IntWritable score = new IntWritable(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] words = value.toString().split(" "); if(words.length == 2) { name.set(words[0]); score.set(Integer.parseInt(words[1])); context.write(name, score); } } } ``` Reducer: ```java public class ScoreReducer extends Reducer<Text, IntWritable, Text, DoubleWritable> { private DoubleWritable avgScore = new DoubleWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; int count = 0; for (IntWritable val : values) { sum += val.get(); count++; } double avg = (double)sum / count; avgScore.set(avg); context.write(key, avgScore); } } ``` Driver: ```java public class ScoreDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "score average"); job.setJarByClass(ScoreDriver.class); job.setMapperClass(ScoreMapper.class); 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); } } ``` 运行结果: ``` 张三 88.66666666666667 李四 92.0 王五 87.0 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值