2019 Nanhai District Youth Informatics Olympiad (Primary School Group A)

2019 Nanhai District Youth Informatics Olympiad (Primary School Group A)

Score: 460 points, first class 500 (I'm still too good)

Summary: It's mainly a pity for question 5 (only 20 points)

Next time you have to clarify your thoughts and think about specific methods before doing it

(It seems that there have been 5 or 6 questions over the years to be greedy)

A. Printing square matrix

Title description
The following square matrix is ​​very regular, called a serpentine square. For example, 3×3:

1 2 3

6 5 4

7 8 9

Now given the side length, output the corresponding serpentine square matrix.

Input format
An integer n, which means to output an n×n serpentine square matrix, 1≤n≤100.

The output format is
n lines, each line is n integers, separated by spaces.

Sample
Sample input 1
4
Sample output 1
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13

Direct violence, no details.

#include<iostream>
#include<fstream>
#include<algorithm>
#include<cmath>

using namespace std;

int n;

int main()
{
    
    
	 cin>>n;
	 for(int i=1;i<=n;i++)
	 {
    
    
	 	if(i%2==1)
	 	{
    
    
	 		for(int j=(i-1)*n+1;j<=i*n;j++)
	 		cout<<j<<" ";
	 		cout<<endl;
	 	}
	 	if(i%2==0)
	 	{
    
    
	 		for(int j=i*n;j>=(i-1)*n+1;j--)
	 		cout<<j<<" ";
	 		cout<<endl;
	 	}
	 }
    return 0;
}

B. Fraction subtraction

Title description
primary Mathematics
We all know such fractional calculations. Please program to calculate the result of subtracting two fractions.

Input format The
first line, two integers a and b, represent a fraction abab, 1≤a<b≤10000.

In the second line, two integers c and d represent a fraction cdcd, 1≤c<d≤10000.

Enter the data to ensure that the calculation result is positive.

Output format
Two integers, representing the result.

Reminder: The calculation result fractions offer points.

Sample
Sample input 1
4 7
1 3
Sample output 1
5 21

Pay attention to the result offer points

#include<iostream>
#include<fstream>
#include<algorithm>
#include<cmath>

using namespace std;

int z1,m1,z2,m2,a1,a2,k;

int main()
{
    
    
     cin>>z1>>m1>>z2>>m2;
	 for(int i=1;i<=m1;i++)
	 {
    
    
	 	if(i*m2%m1==0)
	 	k=i*m2;
	 } //防止int炸(不加也没有关系)
	 z1=z1*(k/m1);
	 z2=z2*(k/m2);
	 a1=z1-z2;
	 a2=k;
	 int i=2;
	 while(i<=a1)
	 {
    
    
	 	if(a1%i==0&&a2%i==0)
	    a1=a1/i,a2=a2/i;
	    else
	    i++;
	 }//约分
     cout<<a1<<" "<<a2;

    return 0;
}

C. Backward

Topic description The
teacher wrote a line of N positive integers on the blackboard. The teacher wanted to test the students’ ability to recite and read the numbers from right to left and find their sum. For example, 3 integers are written: 123 45 60. The result is: 06+54+321=381

Input format The
first line, an integer n, 1≤n≤100.

In the second line, there are n positive integers, each of which does not exceed 1,000,000.

Output format
An integer.

Sample
Sample input 1
3
123 45 60
Sample output 1
381

Strings are more convenient

#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cmath>//pow函数

using namespace std;

int n;
long long ans;
string s;

int main()
{
    
    
     cin>>n;
     for(int e=1;e<=n;e++)
     {
    
    
     	cin>>s;
     	for(int i=s.size()-1;i>=0;i--)
     	{
    
    
     		int d=s[i]-'0';
     		ans=ans+pow(10,i)*d;
     	}
     }
     cout<<ans;
    return 0;
}

D. Difference of set

Title description
There are two series of numbers, A and B. Those numbers that appear in A but not in B are called the difference between set A and set B: AB. For example: A={1,4,5,2,6}, B={5,3,2,7}, then AB={1,4,6}.

Now give you two sets A and B, and find out how many numbers there are in AB.

Input format The
first line, two integers na and nb, indicate how many integers there are in set A and set B, 1≤na, nb≤10000;

In the second line, na different positive integers represent the numbers in the set A, and each positive integer does not exceed 10000;

In the third line, nb different positive integers represent the numbers in set B, and each positive integer does not exceed 10,000.

Output format
An integer, how many numbers are in AB.

Sample
Sample input 1
5 4
5 2 4 8 7
3 7 6 2
Sample output 1
3

Data range and hint
Sample explanation:
AB={5,4,8}.

Array statistics will not time out, no details

#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cmath>

using namespace std;

int n,a[10005],b[10005],f,ans,m;

int main()
{
    
    
     cin>>n;
	 cin>>m;
     for(int i=1;i<=n;i++)
     {
    
    
     	cin>>f;
     	a[f]++;
     	ans++;
     }
     for(int i=1;i<=m;i++)
     {
    
    
     	cin>>f;
     	if(a[f]==1)
     	ans--;
     }
     cout<<ans;
     
    return 0;
}

E. Representable numbers

Title description
There are N integers arranged in a row from left to right. If a certain number is equal to the sum of the two numbers before it, it is said that this number is a representable number. Ask how many numbers in the given sequence can be represented.

Input format The
first line, an integer N, indicates how many integers there are in the sequence, 1≤N≤10000;

In the second line, there are N positive integers, each of which does not exceed 10,000.

Output format
An integer, how many representable numbers are there.

Sample
Sample input 1
8
5 2 2 3 4 8 7 16
Sample output 1
3

Data range and hint
Sample explanation:
4=2+2; 8=5+3; 7=3+4.

Oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo big that it's so maddening, there are so many details, i think it's too complicated!

Original code:

#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cmath>

using namespace std;

int n,a[10005],f,ans;

int main()
{
    
    
     cin>>n;
     for(int i=1;i<=n;i++)
     {
    
    
     	cin>>f;
     	a[f]=i;
     }//这样只要一有重复的,数组就会被覆盖掉
     for(int i=1;i<=10000;i++)
     {
    
    
     	for(int j=1;j<=10000;j++)
     	{
    
    
     		if(a[i]!=0&&a[j]!=0&&a[i+j]!=0&&a[i+j]!=-1)
     		{
    
    
     			if(a[i]<=a[i+j]&&a[j]<=a[i+j])
     			{
    
    
     			   ans++;	
     			   a[i+j]=-1;
     			}
     		}
     	}
     }//当时的逻辑好清奇,我到现在也想不通当时为什么要这样写……
     cout<<ans;
    return 0;
}

AC code:

#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cmath>

using namespace std;

int n,a[10005],c[20005],ans;

int main()
{
    
    
     cin>>n;
     for(int i=1;i<=n;i++)
     	cin>>a[i];
     for(int i=1;i<=n;i++)
     {
    
    
     	for(int j=1;j<=i-1;j++)//往前找,保证是前面两个
     	{
    
    
            c[a[i]+a[j]]=1;//防止出现多个加起来相同的数
     	}
		 ans=ans+c[a[i]];//如果有相同的数字,那它也会++,妙哉
     }//我觉得有点类似素数筛法
     cout<<ans;
    return 0;
}

So angry~

F. Stacked Arhat

Title description
The N cows on the farm like to play the game of stacking arhats, that is, a few cows, one cow followed by one cow, stand in the shape of a pillar. However, the strength of the cows is different. Use the value Ci to indicate how many cows can stand on the i-th cow at most, and ask how many cows can stand in the shape of a pillar at least.

Input format: the
first line, 1 integer N, indicating how many cows there are, 1≤N≤1000;

The second line, N positive integers Ci, represent the power of these cows, 0≤Ci≤1000.

Output format:
an integer, which means at least several "Arhats".

Sample
Input sample:
5
0 2 1 2 2
Output sample:
2

Data Range and Reminder
Sample explanation: The
first, third, and second cows can be stacked from top to bottom;
the fourth and fifth cows can be stacked.

#In fact, it is greedy. I thought of it during the exam, but the method is too complicated (it’s good to do it right, but the problem is wrong!).

The basic idea, from big to small. (I can basically think of it)
But if it is the same, how can I put it aside?
If you put it, you will lose a bit later.
If you don’t put it, you will be wrong if you don’t put the answer at the end (I have been entangled in the exam)

So, don’t think about the question of keeping it going, just start small to big, stack from top to bottom, put it where you can, and just do whatever you can: D (This trick was thought of by a great god, it’s so powerful, worship)

AC code:

#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cmath>

using namespace std;

int n,f,ans,c[10005],k,d;

int main()
{
    
    
    //freopen("638.in","r",stdin);
	//freopen("638.out","w",stdout);
	 cin>>n;
	 for(int i=1;i<=n;i++)
	 {
    
    
	 	cin>>f;
	 	c[f]++; 
	 }
	while(k<n)
	{
    
    
		d=0;//要承受多少只
		ans++;
		for(int i=0;i<=1000;)//注意0
	    {
    
    
		    if(c[i]!=0&&i>=d)
		    {
    
    
		    	c[i]--;//考虑多只奶牛f相同
		    	k++;
		    	d++;
			}
			else
			i++;
	    }
	}
	cout<<ans;
	
    return 0;
}

ε=(´ο`*))) The test is so bad this time

Guess you like

Origin blog.csdn.net/yyh0910/article/details/115056373