catalogue
Question 1: printing graphics
1. Meaning:
The following program will draw a fractal diagram (that is, the overall and local self similar graphics) on the console.
When n=1,2,3, the output is as follows:
Please analyze the program carefully and fill in the missing code in the underlined part.
When n = 1:
o ooo o
When n = 2:
o ooo o o o o ooooooooo o o o o ooo o
When n = 3:
o ooo o o o o ooooooooo o o o o ooo o o o o ooo ooo ooo o o o o o o o o o o o o ooooooooooooooooooooooooooo o o o o o o o o o o o o ooo ooo ooo o o o o ooo o o o o ooooooooo o o o o ooo o
Source code:
#include <stdio.h> #include <stdlib.h> void show(char* buf, int w) { int i, j; for (i = 0; i < w; i++) { for (j = 0; j < w; j++) { printf("%c", buf[i * w + j] == 0 ? ' ' : 'o'); } printf("\n"); } } void draw(char* buf, int w, int x, int y, int size) { if (size == 1) { buf[y * w + x] = 1; return; } int n = _________________________; //Fill in the blanks draw(buf, w, x, y, n); draw(buf, w, x - n, y, n); draw(buf, w, x + n, y, n); draw(buf, w, x, y - n, n); draw(buf, w, x, y + n, n); } int main() { int N = 3; int t = 1; int i; for (i = 0; i < N; i++) t *= 3; char* buf = (char*)malloc(t * t); for (i = 0; i < t * t; i++) buf[i] = 0; draw(buf, t, t / 2, t / 2, t); show(buf, t); free(buf); return 0; }
Note: only submit the missing code in the underlined part, and do not copy any existing code or symbols.
2. Solution:
Careful observation shows that whenever n increases by 1, the graph at n - 1 is copied and added to the top, bottom, left and right directions.
In the code, a one-dimensional array buf is used to store the graph and recursive drawing is used. The five draw functions draw the middle, left, right, upper and lower edges respectively. Each time we draw a graph of n * n (which we need to find), the variable size is passed in. This size is the side length of the graph, so it is not difficult to see that n = size / 3 (that is, the nth graph is three times the length of the nth - 1 graph in both horizontal and vertical directions)
3: Code:
int n = size / 3; //Fill in the blanks
Question 2: flight time
1. Meaning:
Xiao h went to the United States to participate in the blue bridge cup international competition. Little h's girlfriend found that little h left at 10 a.m. and arrived in the United States at 12 a.m., so she sighed, "now the plane flies so fast that it can reach the United States in two hours.".
Little h was terrified of supersonic flight. After careful observation, it is found that the take-off and landing time of the aircraft is local time. Due to the 12 hour time difference between Beijing and the eastern United States, the plane needs a total of 14 hours of flight time.
Soon after, little h's girlfriend went to the Middle East to exchange. Xiao h doesn't know the time difference between the Middle East and Beijing. But little h got the take-off and landing time of his girlfriend's round-trip flight. Little h wants to know the flight time of his girlfriend's flight.
For a flight that may cross time zones, the take-off and landing time of the return trip is given. Assuming that the round-trip flight time of the aircraft is the same, calculate the flight time of the aircraft.
Input format:
One input contains multiple sets of data.
The first input line is a positive integer T, indicating the number of input data groups.
Each group of data contains two lines, the first line is the take-off and landing time of the departure journey, and the second line is the take-off and landing time of the return journey.
The format of takeoff and landing time is as follows
- h1:m1:s1 h2:m2:s2
- h1:m1:s1 h3:m3:s3 (+1)
- h1:m1:s1 h4:m4:s4 (+2)
Indicates that the flight takes off at h1:m1:s1 local time,
The first format indicates landing at h2:m2:s2 on the day of local time
The second format indicates landing at h3:00, m3:00, s3:00 the next day local time.
The third format indicates landing at h4:m4:s4 on the third day of local time.
For all the time given in the form of h:m:s in this topic, guarantee (0 < = h < = 23, 0 < = m, s < = 59)
Ensure that the input time is legal and the flight time does not exceed 24 hours.
Output format:
For each group of data, one line of time hh:mm:ss is output, indicating that the flight time is HHH mm min ss.
Note that when the time is a single digit, the leading zeros should be supplemented. For example, three hours, four minutes and five seconds should be written as 03:04:05.
Input sample:
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)
Output example:
04:09:05 12:10:39 14:22:05
2. Solution:
Calculate the time difference.
Column formula:
Set takeoff time as S, arrival time as E, flight time as T and time difference as D
A->B: S1+T+D=E1
B->A: S2+T-D=E2
Simplified: 2T=(E1+E2)-(S1+S2)=(E1-S1)+(E2-S2)
It can be seen that it has nothing to do with the time difference, because it is offset once
Convert the last minute to two seconds.
The key is data processing
Note: if you arrive the next day, hh += 24; If it arrives on the third day, hh += 48
3. Code:
#include<iostream> #include<cstring> using namespace std; int n; char time1[1010], time2[1010]; int cal(char s[]) {//Calculate the time difference (converted into seconds) int len = strlen(s); int time1_h = (s[0] - '0') * 10 + (s[1] - '0'); int time1_m = (s[3] - '0') * 10 + (s[4] - '0'); int time1_s = (s[6] - '0') * 10 + (s[7] - '0'); int all_time1_s = time1_h * 3600 + time1_m * 60 + time1_s; int time2_h = (s[9] - '0') * 10 + (s[10] - '0'); int time2_m = (s[12] - '0') * 10 + (s[13] - '0'); int time2_s = (s[15] - '0') * 10 + (s[16] - '0'); if (len != 17) {//The next day if (s[len - 2] == '1') time2_h += 24; else time2_h += 48; } int all_time2_s= time2_h * 3600 + time2_m * 60 + time2_s; return all_time2_s - all_time1_s; } int main(){ cin >> n; getchar(); while (n--) { cin.getline(time1, 1010); cin.getline(time2, 1010); int dif_time1 = cal(time1); int dif_time2 = cal(time2); int all_dif = (dif_time1 + dif_time2) / 2; int hh = all_dif / 3600; int mm = (all_dif - 3600 * hh) / 60; int ss = all_dif % 60; printf("%02d:%02d:%02d\n", hh, mm, ss); } return 0; }