算法Advent of Code程序员

Advent of Code Day 2 损坏校验和

2017-12-02  本文已影响32人  宅纸箱

解题语言不限Java

题目内容

As you walk through the door, a glowing humanoid shape yells in your direction. "You there! Your state appears to be idle. Come help us repair the corruption in this spreadsheet - if we take another millisecond, we'll have to display an hourglass cursor!"

当你通过门的时候,一个人形的火焰对你大喊“你来帮我们修复这个表格吗。如果我们用一毫秒来恢复这个表格,我们必须要一个滴漏光标”

The spreadsheet consists of rows of apparently-random numbers. To make sure the recovery process is on the right track, they need you to calculate the spreadsheet's checksum. For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.

这个表格是由随机数字组成的。为了保证恢复过程顺利进行,要计算这个表格的校验和。校验和是每一行最大和最小值得差值的和。

5 1 9 5
7 5 3
2 4 6 8
The first row's largest and smallest values are 9 and 1, and their difference is 8.
第一行最大的数字是9,最小的是1,所以差值是8
The second row's largest and smallest values are 7 and 3, and their difference is 4.
第二行最大的数字是7,最小的是3,所以差值是4
The third row's difference is 6.
第三行的差值是6
In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18.
在这个例子里,电子表格的校验和是8+4+6=18

解题思路

将输入复制到文件
在读取循环中,同时计算单行数据的最大值和最小值
计算差值并加到buff里

上一篇 下一篇

猜你喜欢

热点阅读