2018蓝桥杯 航班时间和乘积尾零

法一答案均转自https://blog.csdn.net/zhanw15/article/details/79845250


标题:乘积尾零

如下的10行数据,每行有10个整数,请你求出它们的乘积的末尾有多少个零?

5650 4542 3554 473 946 4114 3871 9073 90 4329
2758 7949 6113 5659 5245 7432 3051 4434 6704 3594
9937 1173 6866 3397 4759 7557 3070 2287 1453 9899
1486 5722 3135 1170 4014 5510 5120 729 2880 9019
2049 698 4582 4346 4427 646 9742 7340 1230 7683
5693 7015 6887 7381 4172 4341 2909 2027 7355 5649
6701 6645 1671 5978 2704 9926 295 3125 3878 6785
2066 4247 4800 1578 6652 4616 1113 6205 3264 2915
3966 5291 2904 1285 2193 1428 2265 8730 9436 7074
689 5510 8243 6114 337 4096 8199 7313 3685 211

注意:需要提交的是一个整数,表示末尾零的个数。不要填写任何多余内容。
答案:31

法一:解题思路:在质数中,当且仅当2*5的结果为10,末尾为0,则求所有数字能分解出多少5(2足够多)

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6         int temp = 0, count = 0;
 7     while (scanf_s("%d", &temp) != EOF)
 8     {
 9         while (temp % 5 == 0)
10         {
11             count++;
12             temp /= 5;
13         }
14 
15     }
16 
17     printf("%d", count);
18 }        

法二:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     /*
 7     2018蓝桥 乘积尾零
 8     */
 9 
10     int num, cnt = 0;
11     long sum = 1;
12     for (int i = 0; i < 100; i++)
13     {
14         cin >> num;
15         sum *= num;
16         
17         while(sum % 10 == 0)
18         {
19             cnt++;
20             sum /= 10;
21         }
22         if (sum / 10000)
23             sum %= 10000;
24     }
25     cout << cnt << endl;
26 
27     return 0;
28 }


标题:航班时间

【问题背景】
小h前往美国参加了蓝桥杯国际赛。小h的女朋友发现小h上午十点出发,上午十二点到达美国,于是感叹到“现在飞机飞得真快,两小时就能到美国了”。

小h对超音速飞行感到十分恐惧。仔细观察后发现飞机的起降时间都是当地时间。由于北京和美国东部有12小时时差,故飞机总共需要14小时的飞行时间。

不久后小h的女朋友去中东交换。小h并不知道中东与北京的时差。但是小h得到了女朋友来回航班的起降时间。小h想知道女朋友的航班飞行时间是多少。

【问题描述】
对于一个可能跨时区的航班,给定来回程的起降时间。假设飞机来回飞行时间相同,求飞机的飞行时间。

【输入格式】
从标准输入读入数据。
一个输入包含多组数据。

输入第一行为一个正整数T,表示输入数据组数。
每组数据包含两行,第一行为去程的 起降 时间,第二行为回程的 起降 时间。
起降时间的格式如下

h1:m1:s1 h2:m2:s2

h1:m1:s1 h3:m3:s3 (+1)

h1:m1:s1 h4:m4:s4 (+2)
表示该航班在当地时间h1时m1分s1秒起飞,

第一种格式表示在当地时间 当日 h2时m2分s2秒降落
第二种格式表示在当地时间 次日 h3时m3分s3秒降落。
第三种格式表示在当地时间 第三天 h4时m4分s4秒降落。

对于此题目中的所有以 h:m:s 形式给出的时间, 保证 ( 0<=h<=23, 0<=m,s<=59 ).

【输出格式】
输出到标准输出。

对于每一组数据输出一行一个时间hh:mm:ss,表示飞行时间为hh小时mm分ss秒。
注意,当时间为一位数时,要补齐前导零。如三小时四分五秒应写为03:04:05。

【样例输入】
3
17:48:19 21:57:24
11:05:18 15:14:23
17:21:07 00:31:46 (+1)
23:02:41 16:13:20 (+1)
10:19:19 20:41:24
22:19:04 16:41:09 (+1)

【样例输出】
04:09:05
12:10:39
14:22:05

【限制与约定】
保证输入时间合法,飞行时间不超过24小时。


资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

注意:
main函数需要返回0;
只使用ANSI C/ANSI C++ 标准;
不要调用依赖于编译环境或操作系统的特殊函数。
所有依赖的函数必须明确地在源文件中 #include <xxx>
不能通过工程设置而省略常用头文件。

提交程序时,注意选择所期望的语言类型和编译器类型。

法一:

 1 #include <iostream>
 2 #include <string>
 3 #include <sstream>
 4 #include <fstream>
 5 
 6 using namespace std;
 7 int main()
 8 {
 9     ifstream in("input.txt");
10     if (!in.is_open())
11     {
12         cout << "Fail to open the file" << endl;
13         return -1;
14     }
15 
16     int N;
17     in >> N;
18     
19     in.get();
20 
21     while (N--)
22     {
23         string line[2];
24         int a[2][7] = { 0 };
25         for (int k = 0; k < 2; k++)
26         {
27             getline(in, line[k]);
28 
29             for (int i = 0; i < line[k].length(); i++)
30             if (line[k][i] == ':' || line[k][i] == '(' || line[k][i] == ')' || line[k][i] == '+')
31                 line[k][i] = ' ';
32 
33             istringstream is(line[k]);
34 
35             int i = 0;
36             while (is >> a[k][i])
37             {
38                 i++;
39             }
40 
41         }
42         int time;                //记录秒数
43         int h, m, s;
44         time = (a[0][0] + a[1][0]) * 3600 + (a[0][1] + a[1][1]) * 60 + a[0][2] + a[1][2];
45         if (a[0][6] == 1)
46             a[0][3] += 24;
47         if (a[1][6] == 1)
48             a[1][3] += 24;
49         if (a[0][6] == 2)
50             a[0][3] += 48;
51         if (a[1][6] == 2)
52             a[1][3] += 48;
53         time = ((a[0][3] + a[1][3]) * 3600 + (a[0][4] + a[1][4]) * 60 + a[0][5] + a[1][5]) - time;
54         time /= 2;
55         h = time / 3600;
56         m = (time - h * 3600) / 60;
57         s = time % 60;
58 
59         if (h < 10)
60             cout << "0";
61         cout << h << ":";
62 
63         if (m < 10)
64             cout << "0";
65         cout << m << ":";
66 
67         if (s < 10)
68             cout << "0";
69         cout << s ;
70         cout << endl;
71     }
72     return 0;
73 
74 }

法二:

 1 #include <iostream>
 2 #include <fstream>
 3 using namespace std;
 4 
 5 //打印
 6 void printFormat(int t, int c)
 7 {
 8     if (t < 10) 
 9         printf("0%d%c", t, c);
10     else
11         printf("%d%c", t, c);
12 }
13 
14 
15 int main()
16 {
17     
18     int N;
19     scanf_s("%d", &N);
20     int i = 2 * N;
21     int *ans = (int*)malloc(sizeof(int) * i);
22     
23     for (int j = 0; j < N; j++)
24         ans[j] = 0;
25 
26 
27     while (i--)
28     {
29         int h1, m1, s1, h2, m2, s2;
30         scanf_s("%d:%d:%d %d:%d:%d", &h1, &m1, &s1, &h2, &m2, &s2);
31         ans[i / 2] += (3600 * (h2 - h1) + 60 * (m2 - m1) + s2 - s1);
32         char c ;
33         while ((c = getchar()) != '
')        //次日或第三天到达的情况
34         if (c == '+')
35             ans[i / 2] += 24 * 3600 * (getchar() - '0');
36     }
37     int h, m, s;
38     for (int j = N - 1; j >= 0; j--)
39     {
40         ans[j] /= 2;
41         h = ans[j] / 3600;
42         m = ans[j] / 60 % 60;
43         s = ans[j] % 60;
44         printFormat(h, ':');
45         printFormat(m, ':');
46         printFormat(s, '
');
47 
48     }
49 
50     return 0;
51 }

 法三:

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int n;
 6     cin >> n;
 7     int a[2][6];
 8     for(int i = 0;  i < n; i++)
 9     {
10         scanf("%d:%d:%d %d:%d:%d", &a[0][0], &a[0][1], &a[0][2], &a[0][3], &a[0][4], &a[0][5]);
11         char ch = getchar();
12         if(ch != '
')
13         {
14             getchar();        //读取左括号 
15             getchar();        //读取加号
16             
17             ch = getchar();    //读取数字
18             
19             a[0][3] += (ch - '0') * 24; 
20             
21             getchar();        //读取右括号 
22             getchar();        //读取回车 
23             
24         }
25         
26         int secondAmount = (a[0][3] - a[0][0]) * 3600 + (a[0][4] - a[0][1]) * 60 + a[0][5] - a[0][2];
27         
28         scanf("%d:%d:%d %d:%d:%d", &a[0][0], &a[0][1], &a[0][2], &a[0][3], &a[0][4], &a[0][5]);
29         ch = getchar();
30         if(ch != '
')
31         {
32             getchar();        //读取左括号 
33             getchar();        //读取加号
34             
35             ch = getchar();    //读取数字
36             
37             a[0][3] += (ch - '0') * 24; 
38             
39                 getchar();        //读取右括号 
40                     getchar();        //读取回车 
41         }
42         
43         secondAmount += (a[0][3] - a[0][0]) * 3600 + (a[0][4] - a[0][1]) * 60 + a[0][5] - a[0][2];
44         
45         secondAmount /= 2;
46         
47         int hour = secondAmount / 3600;
48         int minute = secondAmount % 3600 / 60;
49         int second = secondAmount % 60;
50         
51         if(hour < 10)
52             cout << "0";
53         cout << hour << ":"; 
54         
55         if(minute < 10)
56             cout << "0";
57         cout << minute << ":"; 
58         
59         if(second < 10)
60             cout << "0";
61         cout << second << endl; 
62              
63     }
64     
65     return 0;
66 }
原文地址:https://www.cnblogs.com/hi3254014978/p/10478434.html