编程实现: 李四去海南旅游定机票,机票的价格受淡季和旺季影响,受头等舱和经济舱影响。 假设机

public class Test_17 {
public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.println("请输入您出行的月份:");
int month = input.nextInt();
System.out.println("请问您选择头等舱还是经济舱?头等舱输入1,经济舱输入2");
int type = input.nextInt();
double price = 5000.0;
if (month==1 || month==2 || month==3 || month==11 || month==12) {
if (type==1) {
price*=0.5;
}else if (type==2) {
price*=0.4;
}
} else {
if (type==1) {
price*=0.9;
} else if (type==2) {
price*=0.8;
}
}
System.out.println("您的机票价格为:"+price);


}


}
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现思路: 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 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值