World's Worst Bus Schedule

Description

You are a very impatient person, and hate to be kept waiting. You are on your way to visit a relative all the way in New Orleans. The problem is that the bus station you are at has the world's worst bus schedule! There are no arrival or departure times listed, only route durations for each bus running. Being the impatient person you are, you whip out your laptop and attempt to write a program that will determine how long you will have to wait before the next bus comes. Hey, you have nothing better to do, right?
Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.

A single data set has 3 components:

Start line - A single line, "START N", where N is the number of buses running and 1 <= N <= 20.
Route Duration line - There will be N of these lines, and each line will consist of M route durations (where 1 <= M <= 10), which will signify how long it will take each bus to return to the bus station after completing a particular route. A route duration will be represented as an integer between 1 and 1000 (inclusive).
Arrival Time - There will only be one of these lines per data set. This line represents the time that you arrived at the bus station and began waiting. This is simply the number of time units that you arrived at the bus station after the buses began running (all buses begin running at time 0). This number is represented as a positive integer (yes, it can be 0 as well, this would signify arriving at the bus station as the buses begin running).
End line - A single line, "END".

Following the final data set will be a single line, "ENDOFINPUT".
Output

For each data set, there will be exactly one line of output. This line will simply be the number of time units you will have to wait before the next bus comes after you arrive. You hate waiting so much that you will just get on the first bus that returns to the station. Let's hope this is the bus to New Orleans!

Notes

All buses continuously go on their routes, starting back up with their first route after their last route is done.
If the passenger's arrival time coincides with any of the bus' route departure times, he/she catches the bus at that time.
Sample Input

START 3
100 200 300
400 500 600
700 800 900
1000
END
START 3
100 200 300 4 3 2 4 2 22
800
10 1000
32767
END
ENDOFINPUT
Sample Output

200
20

http://blog.csdn.net/ehi11/article/details/7875250

描述
你是一个很没有耐心的人,讨厌等待。你就去拜访亲戚在新奥尔良。问题是公共汽车站你在世界上最糟糕的公共汽车时刻表!没有到达或离开上市时间,唯一途径为每个公交运行时间。你是没有耐心的人,你拿出你的笔记本电脑并尝试编写一个程序,将决定你要等多久才能下一辆公车来了。嘿,你没有更好的事情可做,对吧?
输入
这个问题将包括一个输入(非空)系列100数据集。每个数据集将根据以下格式的描述,和不会有空白行分隔数据集。
一个数据集有三个组件:
行,一行开始,“N”开始,其中N是运行的公交车数量和1 < = N < = 20。
路线时间线——会有N行,每一行将包含M路线持续时间(1 < = M < = 10),这将意味着每辆公交车需要多长时间回到汽车站在完成一个特定的路线。路线时间将表示为1到1000之间的一个整数(包容)。
到达时间,只会有一条线/数据集。这条线代表了时间,你来到公车站,开始等待。这仅仅是时间单位的数量,你到达公交车站公交车开始后运行(0时刻运行的所有公交车开始)。这个数字是表示为一个正整数(是的,它也可以是0,这将意味着到达汽车站的公交车开始运行)。
结束行一行,“结束”。
最后的数据集将后一行,“ENDOFINPUT”。
输出
对于每个数据集,将会有一行输出。这条线只是时间单位的数量,你将不得不等待下一辆公车后你的到来。你讨厌等待,你只会得到第一巴士返回车站。让我们希望这是新奥尔良的巴士!
笔记
所有公共汽车路线不断继续,开始了他们的第一个路线返回后,他们最后的路线。
如果乘客的到达时间恰逢任何公共汽车的路线起飞时间,他/她有公共汽车。
样例输入
开始3
100 200 300
400 500 600
700 800 900
1000年
结束
开始3
100 200 300 4 3 2 4 2 22
800年
1000
32767年
结束
ENDOFINPUT
样例输出
200年
20