C language PTA writing notes

7-12 Date Formatting

Different countries in the world have different habits of writing dates. For example, Americans are used to writing "month-day-year", while Chinese are used to writing "year-month-day". Next, please write a program to automatically rewrite the date that is read in the American format into a date that is customary in China.
Input format:
Input the month, day, and year in the format of "mm-dd-yyyy" in one line. The title guarantees that the dates given are legal dates from New Year's Day 1900 to the present.
Output format:
Give the year, month, and day in the format "yyyy-mm-dd" in one line.

think:

  1. Change the order of "mm-dd-yyyy" and output "yyyy-mm-dd"; three memories are required to store mm, dd, and yyyy respectively.
  2. mm needs to output 2-digit integers / dd outputs 2-digit integers / yy outputs 4-digit integers;

Idea 1: mm, dd, yyyy are treated as strings

#include<stdio.h>
#include<string.h>
int main(void)
{
    
    
	char mm[3]={
    
    '\0'}, dd[3]={
    
    '\0'}, yyyy[5]={
    
    '\0'};	//mm占两个字符最后还有一个转义字符'\0',占据三个数组单元。以此类推,dd[2+1]; yyyy[4+1]。如果想给一个大一点的内存空间,一定要初始化数组,房间打扫干净!
	scanf("%2s-%2s-%4s", &mm, &dd, &yyyy);	//限定读入字符数量,否则输出会露娜吗

//以下代码可以作调试
//	{	int i;
//		for (i=0; i<strlen(yyyy); i++) {
    
    
//			printf("mm[%d] = %c \t", i, mm[i]);
//			printf("dd[%d] = %c \t", i, dd[i]);
//			printf("yyyy[%d] = %c\n", i, yyyy[i]);
	
	printf("%s-%s-%s", yyyy, mm, dd);
}
  • A string in C language is an array, and the last array unit memory of a character number is an escape character'\0'
  • %cIs to output a character, %sis to output a string
  • scanf("%xs", &a)Can represent a string of x characters read. (x is several strings, a is a character array)

Idea 2: mm, dd, yyyy are regarded as integers

#include<stdio.h>
int main(void)
{
    
    
    int dd, mm, yyyy;
    scanf("%d-%d-%d", &mm, &dd, &yyyy);
    printf("%04d-%02d-%02d", yyyy, mm, dd);	//这个输出整数,%04d表示的是,输出4位的整数,若不足4位则在前面补0
}
  • The y in %xyd means to output an integer of y digits, and it means that if it is less than y digits, it will be supplemented with x in front.

7-13 the day after tomorrow

If today is Wednesday, the day after tomorrow is Friday; if today is Saturday, the day after tomorrow is Monday. We use numbers 1 to 7 for Monday to Sunday. Given a certain day, please output the day of the week that is the "day after tomorrow".
Input format:
Input the first line to give a positive integer D (1 ≤ D ≤ 7), representing a certain day of the week.
Output format:
output the day of the week after day D in one line.

think

  1. When D is 1-5, the day after tomorrow is D+2; when D is 6, 7, the day after tomorrow is not D+2, it is 1 and 2 respectively;
  2. It is necessary to separate the two cases of D: 1-5 and D: 5-6

Idea 1: Use switch case

#include<stdio.h>
int main(void)
{
    
       
    int D, day;
    scanf("%d", &D);
    
    switch (D)
    {
    
    
        default : 
            day = D+2;
            break;
        case 6 : 
            day = 1;
            break;
        case 7 : 
            day = 2;
            break;
     }
    printf("%d", day);
}
  • The switch case will go through the rest of the cases when it enters the case judgment. If you only want the switch to do the operation of a certain case, you must use break together;

Idea 2: Use if statement

#include<stdio.h>
int main(void)
{
    
    
	int D, d1, d2;
	scanf("%d", &D);
	if ( D>=1 && D<=5 ) {
    
    
	d1 = D+2;
	printf("%d", d1);
	} else {
    
    
	d1 = D+2;
	d2 = d1%7;
	printf("%d", d2);
	}
}

7-14 and what time

Sometimes people use four digits to represent a time, such as 1106 for 11:06. Now, your program needs to calculate the end time based on the start time and the elapsed time.
Read in two numbers, the first number represents the current time with such four digits, the second number represents the number of minutes, calculate what time the current time is after so many minutes, and the result is also expressed as four digits. When the hour is a single digit, there is no leading zero, for example, 5:30 is represented as 530; 0:30 is represented as 030. Note that the number of minutes represented by the second number may exceed 60 or be negative.
Input format:
Enter 2 integers in one line, which are the starting time represented by four digits and the number of minutes elapsed, separated by spaces. Note: In the start time, when the hour is a single digit, there is no leading zero, that is, 5:30 is represented as 530; 0:30 is represented as 030. The number of elapsed minutes may exceed 60, or it may be negative.
Output format:
Output the end time represented by no more than four digits, when the hour is a single digit, there is no leading zero. The topic guarantees that the start time and end time are within the same day.

think

  • The given start time is a four-digit integer, how to divide the four-digit integer into hours and minutes (use integer division and remainder )
  • The given elapsed time is the number of minutes, how to add and subtract with the start time (the start time needs to be converted into a number in minutes )
  • Output format requirements, if the hour is 0, then output 3 digits, if not
#include<stdio.h>
int main()
{
    
    
    int start, pass;
    scanf("%d %d", &start, &pass);
    
    int hour1, minute1, hour2, minute2, hour3, minute3;
    
    //算出pass的小时/分钟
    hour2 = pass/60;
    minute2 = pass%60;
    
    //算出start的小时/分钟
    hour1 = start/100;
    minute1 = start%100;
    
    //如果是正数
    if (pass>0){
    
    
    //改变小时分钟
    hour3 = hour1+hour2;
    minute3 = minute1+minute2;
    } else {
    
    
    hour3 = hour1-hour2;
    minute3 = minute1-minute2;
    }
    
    if (minute3 > 59){
    
    
        minute3 %=60;
        hour3 += 1;
        if (minute3 < 0){
    
    
        minute3 += 60;
        hour3 =-1;
        }
    }
    
    printf("%d%02d", hour3, minute3);
    
    return 0;
}

I found that the whole idea was messy and not well thought out. .

Correct solution:

#include <stdio.h>
int main()
{
    
    
    int hour1, minute1;
    scanf("%d %d", &hour1, &minute1);
    int a = hour1 / 100;
    int b = hour1 % 100;
    int c = a * 60 + b + minute1;
    int e = c / 60;
    int f = c % 60;
    int t = e * 100 + f;
    printf("%d", t);
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_71290816/article/details/127203339