How to use scanf

Recently, I learned the deeper function of scnaf in the process of writing questions.
About the usage of scanf
scanf can be used in two ways:

  1. scanf("input controller", input parameter);
#include<stdio.h>
int main()
{
	int a;
	scanf("%d",&a);
	printf("%d\n",a);
}


This is often encountered in the course of our problem-solving. We need to input data according to the requirements, but we need to be clear that the input 123 is a character at the beginning, which is converted into an integer 123 according to our requirements.
2. scanf("input controller is not input controller", input parameter);

 # include<stdio.h>
 int main()
 {
 	int a;
 	scanf("a=%d",&a);
 	printf("a=%d",a);
 	return 0;
 }


In the process of your input, you must input with a = and other non input control characters. If you only input 15, you will enter the following:

Why is that? Because scanf does not correctly read in a=15, there is no output a=15. In this note, the integer a defined by the dev compiler is not initialized, so it outputs 0, and other values may be output on other compilers (initialization is also very important during the competition)
Here is just a brief introduction to the two formats of scanf. You can baidu yourself in more detail. Next, I will focus on the second method: please see the question
Title Description
There are some dates in the format MM/DD/YYYY. Program it to date size.
input
nothing
output
nothing
sample input
15/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005
sample output
15/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005
Solution idea: how do you input it? It includes / and sorting the dates. First of all, we need to separate the dates from the month and day of the year. In fact, scanf can be used here (sscanf can also be used here, which is not introduced too much). Then we need to sort the dates. Here we need to use sort in c + +. And then it's done.
code:

# include<iostream>
# include<cstdio>
# include<algorithm>
using namespace std;
struct data{
	int year;
	int month;
	int day;
}data1[1000];
bool cmp(struct data a,struct data b)
{
	if(a.year==b.year)
	{
		if(a.month==b.month)
		{
			return a.day<b.day;
		}
		else
		return a.month<b.month;
	}
	else
	return a.year<b.year;
}
int main()
{
	int i=0;
	while(scanf("%d/%d/%d",&data1[i].month,&data1[i].day,&data1[i].year)!=EOF)
	i++;
	//printf("%d",i);
	sort(data1,data1+i,cmp);
	for(int j=0;j<i;j++)
	printf("%02d/%02d/%d\n",data1[j].month,data1[j].day,data1[j].year);

	
	
	return 0;
}

Here we use the second format, scanf ("% D /% D /% d", & A, & B, & C); which is directly separated from, date. (you can do this if you are interested) Address of topic
Question 1883: [Blue Bridge Cup] [the eighth real question in 2017] date question
Title Description
Xiao Ming is sorting out a batch of historical documents. There are many dates in these historical documents. Xiao Ming knows that these dates are from January 1, 1960 to December 31, 2059. What bothers Xiaoming is that the format used for these dates is very different. Some of them use year / month / day, some use month / day / year, and some use day / month / year. What's more, the first two digits of the year are also omitted, so that there are many possible dates corresponding to a date in the literature.

For example, 02 / 03 / 04 may be March 4, 2002, February 3, 2004 or March 2, 2004.

Given a date in the literature, can you help Xiao Ming to determine which possible dates correspond to them?
input
A date in the form of "AA/BB/CC". (0 <= A, B, C <= 9)
output
Output several different dates, one line for each date, in the format of "yyyy MM DD". Multiple dates are arranged from morning to evening.
sample input
02/03/04
sample output
2002-03-04
2004-02-03
2004-03-02
**Solution ideas: * * this question is a question of group c/c++B of the 8th blue bridge provincial cup in 2017. There is no special test algorithm. The test is about details. Here, the second format of scanf is also used.

  1. The three numbers respectively represent the year of MM DD YY or MM DD YY or MM DD YY
  2. What is the effective date? The number of years is between 00 and 99 (just judge whether it is in the 20th century or in the 21st century). The month is in the range of 1 to 12, and the number of days is in the range of corresponding month (one more day in February in leap year)
  3. For example, 02 / 02 / 04 has two times of 2004-02-02, which can be sorted from small to large

code:

#include<iostream>
#include<cstdio>
#include<algorithm>
using  namespace std;
int ans=0;
struct Data
{
	int year;
	int month;
	int day;
}data[3];
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int is_leap_year(int year)
{
	if(year%400==0||year%4==0&&year%100!=0)
	return 1;
	else
	return 0;
}
bool cmp(struct Data a,struct Data b)
{
	if(a.year==b.year)
	{
		if(a.month==b.month)
		{
			return a.day<b.day;
		}
		else
		return a.month<b.month;
	}
	else
	return a.year<b.year;
}
int Judge(int year,int month,int day,int ans)
{
	for(int i=0;i<ans;i++)
	if(year=data[i].year&&month==data[i].month&&day==data[i].day)
	return 0;
	return 1;
}
int fun(int y,int m,int d)
{
	int ty=y,tm=m,td=d;
	if(y>=60)
	ty+=1900;
	else
	ty+=2000;
	if(tm>=1&&tm<=12)
	{
		int temp=month[tm-1];
		if(is_leap_year(y)&&tm==2)
		temp++;
		if(td>=1&&td<=temp&&Judge(ty,tm,td,ans))
		{
			data[ans].year=ty;
			data[ans].month=tm;
			data[ans].day=td;
			ans++;
		}
	}
}
int main()
{
	int a,b,c;
	scanf("%d/%d/%d",&a,&b,&c);
	fun(a,b,c);
	fun(c,a,b);
	fun(c,b,a);
	sort(data,data+ans,cmp);
	for(int i=0;i<ans;i++)
	printf("%02d-%02d-%02d\n",data[i].year,data[i].month,data[i].day);
	//printf("%02d-%02d-%02d",a,b,c);
	return 0;
}

I'll continue to learn about the use of scanf and update it later.

Published 6 original articles, won praise 0, visited 40
Private letter follow

Added by thedarkwinter on Wed, 15 Jan 2020 10:56:10 +0200