加上给定的n个持续时间

鉴于 N 持续时间以 男:SS ,其中MM表示分钟,SS表示秒。任务是以 HH:MM:SS . 例如:

null
Input : n = 5        duration1 = 0 : 45        duration1 = 2 : 31        duration1 = 3 : 11        duration1 = 2 : 27        duration1 = 1 : 28Output : 0 : 10 : 22Initially, sum = 0On adding duration 1, sum = 0 hour 0 minutes 45 seconds.On adding duration 2, sum = 0 hour 3 minutes 16 seconds.On adding duration 3, sum = 0 hour 6 minutes 27 seconds.On adding duration 4, sum = 0 hour 8 minutes 54 seconds.On adding duration 5, sum = 0 hour 10 minutes 22 seconds

其想法是将所有给定的持续时间转换为秒。一旦我们知道以秒为单位的持续时间,我们就可以计算以秒为单位的持续时间之和。 为了得到小时数,我们必须将总持续时间(以秒为单位)除以3600。余数用于计算分钟数和秒数。通过将余数除以60,我们得到分钟数,余数就是秒数。 以下是该方法的实施情况:

C++

// CPP Program to find the sum of all duration
// in the form of MM : SS.
#include <bits/stdc++.h>
using namespace std;
// Print sum of all duration.
void printSum( int m[], int s[], int n)
{
int total = 0;
// finding total seconds
for ( int i = 0; i < n; i++) {
total += s[i];
total += (m[i] * 60);
}
// print the hours.
cout << total / 3600 << " : " ;
total %= 3600;
// print the minutes.
cout << total / 60 << ": " ;
total %= 60;
// print the hours.
cout << total << endl;
}
// Driven Program
int main()
{
int m[] = { 0, 2, 3, 2, 1 };
int s[] = { 45, 31, 11, 27, 28 };
int n = sizeof (m)/ sizeof (m[0]);
printSum(m, s, n);
return 0;
}


JAVA

// Java Program to find the
// sum of all duration in
// the form of MM : SS.
class GFG
{
// Print sum of all duration.
static void printSum( int m[],
int s[], int n)
{
int total = 0 ;
// finding total seconds
for ( int i = 0 ; i < n; i++)
{
total += s[i];
total += (m[i] * 60 );
}
// print the hours.
System.out.print(total / 3600 + " : " );
total %= 3600 ;
// print the minutes.
System.out.print(total / 60 + ": " );
total %= 60 ;
// print the hours.
System.out.println(total);
}
// Driver code
public static void main (String[] args)
{
int m[] = { 0 , 2 , 3 , 2 , 1 };
int s[] = { 45 , 31 , 11 , 27 , 28 };
int n = m.length;
printSum(m, s, n);
}
}
// This code is contributed by Anant Agarwal.


Python3

# Python3 code to find the sum of
# all duration in the form of MM : SS.
# Print sum of all duration.
def printSum (m, s, n ):
total = 0
# finding total seconds
for i in range (n):
total + = s[i]
total + = (m[i] * 60 )
# print the hours.
print ( int (total / 3600 ) , end = " : " )
total % = 3600
# print the minutes.
print ( int (total / 60 ) , end = ": " )
total % = 60
# print the hours.
print ( int (total))
# Driven Code
m = [ 0 , 2 , 3 , 2 , 1 ]
s = [ 45 , 31 , 11 , 27 , 28 ]
n = len (m)
printSum(m, s, n)
# This code is contributed by "Sharad_Bhardwaj".


C#

// C# Program to find the
// sum of all duration in
// the form of MM : SS.
using System;
class GFG
{
// Print sum of all duration.
static void printSum( int []m,
int []s, int n)
{
int total = 0;
// finding total seconds
for ( int i = 0; i < n; i++)
{
total += s[i];
total += (m[i] * 60);
}
// print the hours.
Console.Write(total / 3600 + " : " );
total %= 3600;
// print the minutes.
Console.Write(total / 60 + ": " );
total %= 60;
// print the hours.
Console.Write(total);
}
// Driver code
public static void Main ()
{
int []m = {0, 2, 3, 2, 1 };
int []s = { 45, 31, 11, 27, 28 };
int n = m.Length;
printSum(m, s, n);
}
}
// This code is contributed by vt_m.


PHP

<?php
// PHP Program to find the
// sum of all duration
// in the form of MM : SS.
// Print sum of all duration.
function printSum( $m , $s , $n )
{
$total = 0;
// finding total seconds
for ( $i = 0; $i < $n ; $i ++)
{
$total += $s [ $i ];
$total += ( $m [ $i ] * 60);
}
// print the hours.
echo floor ( $total / 3600) , " : " ;
$total %= 3600;
// print the minutes.
echo floor ( $total / 60) , ": " ;
$total %= 60;
// print the hours.
echo floor ( $total ) ;
}
// Driver Code
$m = array (0, 2, 3, 2, 1);
$s = array (45, 31, 11, 27, 28);
$n = count ( $m );
printSum( $m , $s , $n );
// This code is contributed by anuj_67.
?>


Javascript

<script>
// javascript Program to find the
// sum of all duration in
// the form of MM : SS.
// Print sum of all duration.
function printSum(m, s, n)
{
var total = 0;
// finding total seconds
for ( var i = 0; i < n; i++)
{
total += s[i];
total += (m[i] * 60);
}
// print the hours.
document.write((total / 3600).toFixed(0) + " : " );
total %= 3600;
// print the minutes.
document.write((total / 60).toFixed(0) + ": " );
total %= 60;
// print the hours.
document.write(total);
}
// Driver code
var m = [0, 2, 3, 2, 1 ];
var s = [45, 31, 11, 27, 28 ];
var n = m.length;
printSum(m, s, n);
// This code is contributed by bunnyram19
</script>


输出:

0 : 10: 22

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享