2013 fourth group C and B provincial Blue Bridge Cup

Posted by megosh on Mon, 28 Feb 2022 03:31:06 +0100

Note: the degree of difficulty is officially recognized by the Blue Bridge Cup (not me (⊙ o ⊙...)

1. Gauss diary (simple)

The great mathematician Gauss has a good habit: keep a diary anyway. There is a difference in his diary. He never indicates the date, but uses an integer instead, such as 4210. Later, people know that the integer is the date, which means that the day is the day after Gauss was born. This may also be a good habit. It reminds the owner all the time: how much time can be wasted when the day passes? Gauss was born on April 30, 1777. In the diary of an important theorem discovered by Gauss, it is marked: 5343, so it can be calculated that the day is December 15, 1791. On the day Gauss got his doctorate, his diary was marked: 8113. Please calculate the date when Gauss received his doctorate.
The format for submitting answers is yyyy MM DD, for example: March 21, 1980.
Operational limits
Maximum running time: 1s
Maximum operating memory: 128M

Solution:
1. Leap year: one leap in 400 years or no leap in 100 years and four leap years.

#include <iostream>

using namespace std;

int judge(int year)
{
    if((year%4==0&&year%100!=0)||year%400==0)
        return 1;
    return 0;
}

int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
    int y=1777,m=4,d=30;
    int n;
    cin>>n;
    while(--n)//Minus one every day
    {
        d++;
        if(m==2&&d==28)
        {
            if(judge(y))
                a[2]=29;
            else
                a[2]=28;
        }
        if(d>a[m])//Days greater than current month
        {
            m++;
            d=1;
            if(m>12)
            {
                m=1;
                y++;
            }
        }
    }
    printf("%04d-%02d-%02d",y,m,d);
    return 0;
}

2. Sloppy formula (simple)

Xiao Ming is an acute child. When he was in primary school, he often copied the questions written by the teacher on the blackboard wrong. Once, the teacher's question was: 36 x 495 =? 36x495=? He copied it: 396 x 45 =? 396x45=? But the result is very dramatic. His answer is actually right!! Because 36 * 495 = 396 * 45 = 1782036 * 495 = 396 * 45 = 17820. There may be many similar coincidence situations, such as 27 * 594 = 297 * 5427 * 594 = 297 * 54. Assuming that a b c d e represents 55 different numbers from 1 to 9 (note that they are different numbers and do not contain 0), how many formulas such as AB * cde=adb * ceab * cde=adb * ce can be satisfied? Please make use of the advantages of computers to find all the possibilities and answer the types of different formulas.
The formulas satisfying the commutative law of multiplication are of different kinds, so the answer must be an even number.

Solution: violent enumeration, five cycles.

Operational limits
Maximum running time: 1s
Maximum operating memory: 128M

#include <iostream>
using namespace std;

int main()
{
    int i,j,k,m,n;
    int s1,s2,cnt=0;
    for(i=1;i<10;i++)
    {
        for(j=1;j<10;j++)
        {
            if(i!=j)
            {
                for(k=1;k<10;k++)
                {
                    if(i!=k&&j!=k)
                    {
                        for(m=1;m<10;m++)
                        {
                            if(i!=m&&j!=m&&k!=m)
                            {
                                for(n=1;n<10;n++)
                                {
                                    if(i!=n&&j!=n&&k!=n&&m!=n)
                                    {
                                        s1=(i*10+j)*(k*100+m*10+n);
                                        s2=(i*100+m*10+j)*(k*10+n);
                                        if(s1==s2)
                                        {
                                            cout<<i<<j<<k<<m<<n<<endl;
                                            cnt++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}

3. 39th step (simple)

Xiao Ming just finished watching the movie "the 39th step". When he left the cinema, he counted the steps in front of the auditorium. It happened to be 39 steps! Standing in front of the steps, he suddenly thought of another question:
If I can only take one or two steps in each step, I will take the left foot first, then turn left and right, and the last step is the right foot, that is to say, I have to take an even number of steps. So, how many different methods are there after 39 steps?
Please make use of the advantages of computer to help Xiao Ming find the answer. The required submission is an integer.
Operational limits
Maximum running time: 1s
Maximum operating memory: 128M

Problem solving: recursion is adopted. Exit conditions: it reaches the 39th step and is an even number of steps.

#include <iostream>
using namespace std;

int cnt=0;
void recursion(int k,int n)
{
    if(k>39)
        return;
    if(k==39&&n%2==0)
    {
        cnt++;
        return;
    }
    recursion(k+1,n+1);
    recursion(k+2,n+1);
}

int main()
{
    recursion(0,0);
    cout<<cnt<<endl;
    return 0;
}

4. Golden continued fraction (simple)

The golden section number 0.61803... Is an irrational number. This constant is very important and will appear in many engineering problems. Sometimes you need to get this number very accurate.
For some precision engineering, the accuracy of constants is very important. Perhaps you have heard of the Hubble Space Telescope. After its first launch, it found a manual processing error. For such a behemoth, in fact, it is only a mistake that the mirror is many times thinner than the hair, but it has become "myopia"!
To get back to business, how can we get the golden section number as accurate as possible? There are many ways.
The simpler one is to use continued fraction:

                  1
    Gold number = ---------------------
                        1
             1 + -----------------
                          1
                 1 + -------------
                            1
                     1 + ---------
                          1 + ...

The more "layers" calculated by this continued fraction, the closer its value is to the golden section. Please make use of this feature to find a sufficiently accurate value of the golden section number, which is required to be rounded to 100 decimal places. The value in three digits after the decimal point is: 0.6180.618; The value of 4 digits after the decimal point is: 0.6180; The value of 5 digits after the decimal point is: 0.61803; The value of 7 digits after the decimal point is 0.61803400. (note the 00 in the tail, which cannot be ignored) your task is to write the golden section value accurate to 100 decimal places. Note: the mantissa is rounded off! If the mantissa is 0, keep it!
Operational limits
Maximum running time: 1s
Maximum operating memory: 128M

Solution:
1. Looking for rules, we can find that the first item is 1 / 2, the second item is 2 / 3, the third item is 3 / 5, and the fourth item is 5 / 8
2. It can be found that double is not enough. It involves the multiplication of large numbers. c + + does not have a supported library yet. You need to write your own large number operation method.

#The large number operation of c + + has not thought of how to do it for the time being!!!
import decimal

decimal.getcontext().prec=101 #Retain 101 significant digits
x=decimal.Decimal(1)
y=decimal.Decimal(2)
t=decimal.Decimal(1)
for i in range(1000):#Fibonacci sequence law
    t=y
    y=x+y
    x=t
count=decimal.Decimal(x/y)
print(round(count,100))#Keep 100 digits: round

5. Prefix judgment (simple)

The following code determines the need_ Whether the string pointed to by start is haystack_ The prefix of the string pointed to by start. If not, NULL will be returned. For example, "abcd1234" contains the prefix "abc".

#include <stdio.h>

char* start_with(char* haystack_start, char* needle_start)
{
    char* haystack = haystack_start;
    char* needle = needle_start;

    
    while(*haystack && *needle){
        if(__________________) return NULL;
    }
    
    if(*needle) return NULL;
    
    return haystack_start;
}

void test(char* a, char* b)
{
    char* p = start_with(a,b); 
    if(p==NULL) 
        printf("[NO]\n");
    else 
        printf("|%s|\n", p);
}

int main()
{
    test("abcd","abc");
    test("abcd","acb");
    test("abcd","abcd");
    test("abcd","");
    test("","abc");
    test("","");
    return 0;
}

Application of pointer
Answer: (haystack + +)= (needle++)

6. Three part sorting (simple)

There are many classical algorithms for general sorting, such as quick sorting, Hill sorting and so on. However, in practical application, there are often some special requirements more or less. We don't need to apply those classical algorithms. We can establish a better solution according to the actual situation. For example, sort the numbers in an integer array: make negative numbers close to the left, positive numbers close to the right, and 0 in the middle. The characteristic of the problem of attention is that order is not required in the negative number region and the positive number region. You can use this feature to end the battle by one linear scan! The following procedure achieves this goal. Where x points to the integer array to be sorted, and len is the length of the array.

Solution: select several columns in different situations and walk through the manual simulation code
Answer: p + +;

#include <stdio.h>

void show(int* x, int len)
{
    int i;
    for(i=0; i<len; i++)
    {
        printf("%d,",x[i]);
    }
    
    printf("\n");
}
void sort3p(int* x, int len)
{
    int p = 0;
    int left = 0;
    int right = len-1;
    
    while(p<=right){
        if(x[p]<0){
            int t = x[left];
            x[left] = x[p];
            x[p] = t;
            left++;
            p++;
        }
        else if(x[p]>0){
            int t = x[right];
            x[right] = x[p];
            x[p] = t;
            right--;
            //p++;                
        }
        else{
            _______________;
        }
    }
    
    show(x, len);
}
int main()
{
    int a[] = {-1,0,1,-2,0,2,-3,0,0,3,-4,-5,4,-6,0,5,6};
    int b[] = {-1,0,-1,-2,0,-2,-3,0,0,-3,-4,-5,-4,-6,0,-5,-6};
    int c[] = {1,0,1,2,0,2,3,0,0,3,4,5,4,6,0,5,6};
    
    sort3p(a,sizeof(a)/sizeof(int));
    sort3p(b,sizeof(b)/sizeof(int));
    sort3p(c,sizeof(c)/sizeof(int));
        
    return 0;
}

7. Wrong ticket (simple)

A secret related unit has issued a certain bill and wants to recover it all at the end of the year. Each bill has a unique ID number. The ID numbers of all bills throughout the year are continuous, but the starting number of ID is randomly selected. Due to the negligence of the staff, an error occurred when entering the ID number, resulting in a broken ID and a duplicate ID. Your task is to find out the ID of the broken number and the ID of the duplicate number through programming, assuming that the broken number cannot occur in the maximum and minimu m n umbers. The program is required to first input an integer n (n < 100) to represent the number of subsequent data lines, and then read in N lines of data. The data length of each line is different. It is several (no more than 100) positive integers (no more than 100000) separated by spaces, and each integer represents an ID number. The program is required to output 1 line, including two integers m and N, separated by spaces, where M represents the hyphen ID and N represents the duplicate ID.
For example, user input:
2
5 6 8 11 9
10 12 9
Program output:
7 9
Another example is user input:
6
164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196
172 189 127 107 112 192 103 131 133 169 158
128 102 110 148 139 157 140 195 197
185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190
149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188
113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119
Program output:
105 120

Solution:
1. Consideration of input problem: how to input? crtl+z can end the input.
2. Select an efficient sorting algorithm.

#include <iostream>
using namespace std;

int a[10000];
int cnt=0;
int main()
{
    int n,i,j,done,t;
    int m1,m2;
    cin>>n;
    while(cin>>a[cnt])//To read in, press ctrl+z to terminate the reading
    {
        cnt++;
    }
    i=0;done=1;
    while(i<cnt&&done)//Bubble sorting
    {
        done=0;
        for(j=0;j<cnt-i;j++)
        {
            if(a[j]>a[j+1])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
                done=1;
            }
        }
        i++;
    }
    for(i=0;i<cnt;i++)
    {
        if(a[i]+1!=a[i+1])
        {
            if(a[i]==a[i+1])
                m1=a[i];//Duplicate sign
            else
                m2=a[i]+1;//Broken sign
        }
    }
    cout<<m2<<" "<<m1<<endl;
    return 0;
}

8. Flip a coin (simple)

Xiao Ming is playing a coin flipping game. There are some coins in a row on the table. We use * for the front and o for the back (lowercase letters, not zero). For example, the possible situation is: oo*oooo; If you flip the two coins on the left at the same time, it becomes: oooo***oooo. Now Xiao Ming's question is: if you know the initial state and the target state to be achieved, you can only flip two adjacent coins at the same time, how many times should you flip at least for a specific situation? We agreed that flipping two adjacent coins is called one-step operation.
Enter description
Two lines of equal length strings represent the initial state and the target state to be achieved respectively.
Length of each line < 1000.
Output description
An integer representing the minimum number of operation steps.
Input:

**********
o****o****

Output:

5

Solution:

Law: 1 The number of change points must be even and cannot be odd.
2. The minimum number of steps can be obtained by subtracting two by two.
3. Change from two sides to the middle, that is, the minimum number of steps (you can directly use the law of 2, which is simple and fast)

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    string s1,s2;
    vector<int> v;
    int i,len,cnt;
    cin>>s1>>s2;
    len=s1.size();
    for(i=0;i<len;i++)//Record change point
    {
        if(s1[i]!=s2[i])
            v.push_back(i);
    }
    i=v.size()-1;
    cnt=0;
    while(i>0)
    {
        cnt+=v[i]-v[i-1];
        i-=2;
    }
    cout<<cnt<<endl;
    return 0;
}

10. Number of consecutive intervals (simple)

Xiao Ming has been thinking about such a strange and interesting question these days: how many consecutive intervals are there in a full arrangement of 1~N? The definition of the serial interval mentioned here is: if all elements in the interval [L,R] (i.e. the L-th to r-th elements of this arrangement) can get a "continuous" sequence with a length of R-L+1 after incremental sorting, it is called this interval serial interval.
When N is very small, Xiao Ming can quickly calculate the answer, but when N becomes large, the problem is not so simple. Now Xiao Ming needs your help.
Input format:
The first line is a positive integer N (1 < = N < = 50000), indicating the scale of the full arrangement;
The second line is N different numbers PI (1 < = Pi < = N), indicating a full arrangement of these N numbers.
Output format: output an integer indicating the number of different serial number intervals.
Example, user input:
4
3 2 4 1
Program output: 7
User input:
5
3 4 2 5 1
Program output: 9

Problem solution: it is also a regular nature. See code for details.

//This is a timeout solution.
#include <iostream>
#include <algorithm>
using namespace std;

int a[500000];
int b[500000];

void mysort(int i,int j)
{
    int k,n,p,t;
    n=0;
    for(k=i;k<=j;k++)//copy
    {
        b[n]=a[k];
        n++;
    }
    k=0;
    while(k<n)//sort
    {
        for(p=0;p<n-k-1;p++)
        {
            if(b[p]>b[p+1])
            {
                t=b[p];
                b[p]=b[p+1];
                b[p+1]=t;
            }
        }
        k++;
    }
}
int islianhao(int i,int j)
{
    int k;
    for(k=0;k<j-i;k++)
    {
        if(b[k]+1!=b[k+1])
            break;
    }
    if(k==j-i)
        return 1;
    return 0;
}
int main()
{
    int n,i,j,cnt;
    cnt=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<=n-1;j++)
        {
            if(i!=0||j!=n-1)
            {
                mysort(i,j);
                cnt+=islianhao(i,j);
            }
        }
    }
    cout<<cnt+n+1<<endl;
    return 0;
}
//This is a clever and correct solution
#include <iostream>
using namespace std;
int a[500000];
int main()
{
	int n,i,max,min,cnt;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(i=0;i<n;i++)
	{
		max=a[i];min=a[i];
		for(j=i+1;j<n;j++)
		{
			if(a[j]>max) max=a[j];
			if(a[j]<min) min=a[j]
		}
		if(j-i==max-min) cnt++;
	}
	cout<<cnt+n<<endl;
	return 0;
}

Test site summary

  1. Mathematical thinking: find the law, find the characteristics, and get the formula (Golden continuous fraction, coin flipping, continuous interval number)
  2. Recursion (step 39)
  3. Cycle of violence (sloppy formula,)
  4. Sorting algorithm (three part sorting, wrong bill,)
  5. Large number operation (Golden continued fraction)
  6. Pointer (prefix judgment)
  7. Common sense (Gauss Diary)