时间相加
题目:
给定n组(0<n<=200)时间对,将其相加。(秒与秒相加,分与分相加,小时与小时相加)
题解:
将时间对数据存到数据数组里,将计算结果存到结果数组里(nums [200][6],result[200][ 3 ])
设定一个表示进位的表示falg ,当计算出现进位时 ,更改标志符。
针对分秒和小时 设置两个类似的加法函数(原因进制不同)
代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76#include<iostream> using namespace std; int jiafa1(int a, int b, bool& f)//分秒的加法 { int result; if (f) { result = a + b + 1; result >= 60 ? f = true, result = result % 60 : f = false; } else { result = a + b; result >= 60 ? f = true, result = result % 60 : f = false; } return result; } int jiafa2(int a, int b, bool& f)//小时的加法 { int result; if (f) { result = a + b + 1; result >= 24 ? f = false, result = result % 24 : f = false; } else { result = a + b; result >= 24 ? f = false, result = result % 24 : f = false; } return result; } int main() { bool flag = false;// 进位标志 //测试样例 个数 int n; //复制 cin >> n;//等于c的scanf //数据数组 int nums[200][6]; //存数据 for (int i = 0; i < n; ++i) { for (int j = 0; j < 6; ++j) { cin >> nums[i][j];//等于c的scanf } } //结果数组 int resule[200][3]; //加法 for (int i = 0; i < n; ++i) { //计算分秒 for (int j = 2; j > 0; --j) { //将结果存在结果数组里 resule[i][j] = jiafa1(nums[i][j], nums[i][j + 3], flag); } //计算小时 resule[i][0] = jiafa2(nums[i][0], nums[i][3], flag); } //输出 for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) { cout << resule[i][j] << " "; } cout << resule[i][2] << endl; } return 0; }
有些题目:小时的加法并不进位 可以将其%24取余的操作删了就OK。
…………
最后
以上就是单薄绿茶最近收集整理的关于多组时间相加的全部内容,更多相关多组时间相加内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复