Ruby payroll program

Introduction

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

Source

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

Commentary

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.

Finally

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

Ruby payroll program
Ruby score calculation program
Ruby Numeric Reference Program
[Ruby] Leap year judgment program
Use C program from Ruby
Ruby: I made a FizzBuzz program!
Ruby learning 4
Ruby basics
Ruby learning 5
Ruby basics
Ruby Review 2
Ruby addition
Refactoring Ruby
[Ruby] A program that determines prime numbers
Ruby learning 3
Calculator program
Zundoko Program
Ruby setting 2
Ruby problem ⑦
Ruby learning 2
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby learning 6
Ruby settings 1
Refactoring Ruby
Ruby basics
Ruby memo
Ruby learning 1
Ruby Review 1
[Ruby] Module
[Ruby] A program that uses search and each_with_index
Janken program using Ruby and its test (test-unit)