CSUST 2020 National Day Freshman Prize Contest Solution

Foreword: First of all, I feel unfortunate that the 20-level cute newbies were brutally attacked by the senior elder sister. Secondly, I hope that you don't get discouraged if you don't perform well this time. Then you will learn the algorithm carefully, and you will definitely have a chance ( said the tumor president ).

Insert picture description here

Question A: zw eat takeaway ( beat the questioner first )
Solution: check in question, output YES for odd number, and output NO code for even number
:

#include<cstdio>
int main()
{
    
    
    int n;
    scanf("%d",&n);
    if(n%2==1)
    {
    
    
        printf("YES");
    }
    else
    {
    
    
        printf("NO");
    }
}

Question B: Solution to lcj's troubles
: Sign-in question, original price x yuan, after WeChat discount is x*0.98 yuan

#include<cstdio>
int main()
{
    
    
  	int x;
  	scanf("%d",&x);
    double ans=x*0.98;
    printf("%.2lf\n",ans);
}

C problem: a+b
problem solution: sign-in problem. Although the problem is a bit long, it is nonsense (the president of the tumor kindly bolded the key sentence of the problem), in fact, only need to judge the R value and B value of the two colors. If the G values ​​are added up to be equal to 255 respectively.

#include<stdio.h>
int main()
{
    
    
    int n1,k1,x1;
    int n2,k2,x2;
    scanf("%d%d%d",&n1,&k1,&x1);
    scanf("%d%d%d",&n2,&k2,&x2);
    if(n1+n2==255&&k1+k2==255&&x2+x1==255)
    {
    
    
        printf("yes\n");
    }
    else
    {
    
    
        printf("no\n");
    }
    return 0;
}

Question D: zw grab an apple ( beat the questioner again here ) The
malignant questioner has not passed the Chinese level four, and provides various pots for the question.
Solution: The first time I took the first group, the second time I took the second group from the remaining groups, that is, the third group at the beginning, and so on, the easy to know is 1 3 5 7… group, then the rest are all even groups, 2, 4, 6, 8 …, then the number of the remaining k th group is 2 k, so the answer is the number of apples in the 2 k group, if 2 *k>n, there is no answer.
Code:

#include<cstdio>
int main()
{
    
    
    int n,k,x;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    {
    
    
        scanf("%d",&x);
        if(i==2*k)
        {
    
    
            printf("%d\n",x);
        }
    }
    if(n<2*k)
    {
    
    
        printf("-1\n");
    }
}

Question E: Being single has always been cool.
Solution: First calculate how many 1s there are, and then judge whether there are k consecutive 1s. How to judge?
Use cnt to record how many consecutive 1s there are currently, traverse each element of the array in turn, if the value is 1, then cnt++, if it is 0, then cnt=0, if cnt>=k at a certain position, it means If there are k consecutive 1s, the output is infinite, otherwise, the number of 1s * x
code:

#include<cstdio>
int main()
{
    
    
    int n,x,k,y;
    scanf("%d%d%d",&n,&x,&k);
    int cnt=0,flag=0,res=0;
    for(int i=1;i<=n;i++)
    {
    
    
        scanf("%d",&y);
        cnt+=y;
        if(y==1) res++;
        else res=0;
        if(res==k) flag=1;
    }
    if(flag) printf("inf\n");
    else printf("%d\n",cnt*x);
}

Question F: Magical Green Grassland
Problem Solution: If there is only one wolf, then Big Gray Wolf will definitely eat lazy sheep, because he will definitely not be eaten by other wolves. If there are two wolves, no matter which wolf eats sheep, He will be eaten by another wolf, so wolves will not eat sheep. If there are three wolves, if the gray wolf eats lazy sheep, the situation will return to the situation of two wolves and one sheep, leaving two wolves. They don't dare to eat, so Big Gray wolves can eat sheep, and so on, it can be found that when the wolf is an even number, it cannot eat sheep, and when the wolf is an odd number, it can eat sheep.
Code:

#include<cstdio>
int main()
{
    
    
    int n;
    scanf("%d",&n);
    if(n%2==1)
    {
    
    
        printf("nice");
    }
    else
    {
    
    
        printf("555");
    }
}

Question G: The president of dl (the president is indeed a cancer )
Problem solution: This question is actually to find any number between the second and third largest of the four numbers. Reason: When there are only two numbers, if you choose a number other than two, the cost must be greater than ba. If you choose any number between the two numbers, then the cost is ba, and the same is true for four numbers. Either use the sort function directly to sort the order, or enumerate 4 numbers, one of which is the second or third largest, and maintain the minimum value of the answer. Because it is not the number between the second largest and the third largest, the calculated answer must be larger than the optimal solution, so it is the minimum value. Remember to use long long in the second method, otherwise it will burst int.
Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[10],ans = 2e9,sum;
int main(){
    
    
    for(int i = 1; i <= 4; i ++){
    
    
       scanf("%d",&a[i]);
    }
    for(int i = 1; i <= 4; i ++){
    
    
       sum = 0;
       for(int j = 1; j <= 4; j ++){
    
    
           sum = (sum + abs(a[j] - a[i]));
       }
       ans = min(ans , sum);
   }
   printf("%lld",ans);
}

Guess you like

Origin blog.csdn.net/weixin_45755679/article/details/108987123