Monday title (2)


To apologize : Some time ago bloggers review and take the final exam, coupled with the recent situation is not good, so stop more for some time. Has now put a winter vacation, a follow-up question will keep updating every week, it is expected to be every Thursday night.
About Content : Bloggers near future in preparation for March's CSP certification exams, as well as the Blue Bridge Cup competition, it will come from these exam questions in. CSP only the former two questions, one of which is the reason I am limited, and the second is the first two questions better grasp. Because each branch of CSP are about 130 points, the first two questions to take to get over 200 points, enough routed to the level before the candidates took the test about 18%. My only explanation for the contents of the first two questions, every detail will, through all the test cases, to ensure that no drop points. If higher suitors please pass.

Count off

Problem Description

g-blog.csdnimg.cn/20200116184006382.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xoeDA1MjU=,size_16,color_FFFFFF,t_70)Here Insert Picture Description

Source title

CSP   201912-1

answer

Skip Function

Meaning of the questions can be seen, we must find a way to skip several conditions are not satisfied. There are two numbers will be skipped, i.e. two conditions:
1. 7 can be divisible.
2. contain the digits of the number 7.
According to the above conditions, the following design is determined as a function of reference.

int isSkip(int num)
{
    int flag  = 0;
    if(num % 7 == 0)	//判断整除7 
        flag = 1;
    while(num != 0){	//判断包含7 
        int k =  num % 10; 
        if(k == 7)
            flag = 1;
        num = num / 10;
    }
    if(flag) return 1;
    else return 0;
}

The second condition at the time of writing is to follow from the lowest to the highest bit, bit by bit to be judged. Because if direct access to more than 10, must be taken into single digits, followed by num divided by 10, to achieve the purpose of the minimum bit discard.

The main function ideas

       Because the problem is fixed B, CD these four individuals, so we simply use an array of length 4, representing the four men. For each value of the array element number represents the number of each skipped.
       From the meaning of the questions, we want the number reported, with the last person reported numbers are not the same, because in the middle there was skipped number. It also uses a counter variable, when the number per a newspaper, this count will be added a variable cnt, unless the required number of skipped, when a reduction in the cnt, the count can be achieved without effect.
       Because each cycle must access the next element in the array, whereas only four elements in the array, the subscript can not increase the number of so many number of packets, so the modulo our method. which isa[p%4]++.
All code example below.

#include<bits/stdc++.h>
using namespace std;

int isSkip(int num)
{
    int flag  = 0;
    if(num % 7 == 0)	//判断整除7 
        flag = 1;
    while(num != 0){	//判断包含7 
        int k =  num % 10; 
        if(k == 7)
            flag = 1;
        num = num / 10;
    }
    if(flag) return 1;
    else return 0;
}
int main()
{
    int n;
    scanf("%d",&n);
    int a[4]={0};//代表甲、乙、丙、丁 
    int p=0;//下标 
    int cnt = 0;//计数变量 
    for(int i=1;;i++){
        cnt++; 
        if(isSkip(i)){
            a[p%4]++;
            cnt--;//如果不满足条件,不计数 
        }
        p++;
        if(cnt==n)
            break;
    }
    printf("%d\n%d\n%d\n%d\n",a[0],a[1],a[2],a[3]);
	return 0;
}
Published 11 original articles · won praise 13 · views 2862

Guess you like

Origin blog.csdn.net/lhx0525/article/details/104009498