This time, we will create a part-time salary calculation program from standard input.
The salary is determined by the following rules.
・ From 9:00 to 17:00:Hourly wage X yen(Normal hourly wage)
・ From 17:00 to 22:00:Hourly wage Y yen(Night hourly wage)
・ Other times:Hourly wage Z yen(Midnight hourly wage)
Your N-day attendance and departure times are given on an hourly basis.
Calculate the total amount you will get for N days.
In the case of the input example, I work evenly in the time zone where the hourly wage is X yen, Y yen, and Z yen in 4 days.
The total amount is Z × 9+ X×8 + Y×5 + Z×1 = 1500×9 + 1000×8 + 1300×5 + 1500×1 =It will be 29500 yen.
Input example
1000 1300 1500
4
0 9
9 17
17 22
22 23
Output example
29500
ruby.rb
m = gets.split.map &:to_i
#Acquisition of self-sufficiency
nums = gets.to_i
#Obtaining the number of working days
times = []
while time = gets
    times << time.chomp.split(' ').map(&:to_i)
end
sum=0
(0..nums-1).each do |i|
    (times[i][0]+1..times[i][1]).each do |t|
            
        if t<=9
            sum += m[2]     
        elsif t<=17
            sum+= m[0]    
        elsif t<=22
            sum+= m[1]
        else
            sum+= m[2]
        end
    end
end
puts sum
ruby.rb
times = []
while time = gets
    times << time.chomp.split(' ').map(&:to_i)
end
Acquire multiple elements of multiple lines (working hours after the third line). By setting while time = gets, it will be repeated until all the standard input values are acquired.
ruby.rb
sum=0
(0..nums-1).each do |i|
    (times[i][0]+1..times[i][1]).each do |t|
           #Hourly wage calculation processing
 
    end
end
ruby.rb
(0..nums-1).each do |i|
 #When you retrieve the i variable
 # 0 1 2 3
Get the number of work days one by one with i variable
ruby.rb
(times[i][0]+1..times[i][1]).each do |t|
 #When you take out the t variable(First day)
 # 1 2 3 4 5 6 7 8 9
1 Get the sunrise work time zone with the t variable
ruby.rb
if t<=9
   sum += m[2]     
 elsif t<=17
   sum+= m[0]    
 elsif t<=22
   sum+= m[1]
 else
   sum+= m[2]
end
The hourly self-sufficiency acquired by the t variable in the if statement is repeatedly taken out and assigned to the sum variable.
For example, after repeating for 4 days, the sum variable is output and the total amount is calculated! !! This completes the program.
I have a program, but I think there are many corrections. I would appreciate it if you could point out what is wrong.
Recommended Posts