Blue Bridge Zhenti 31-day sprint | Daily three-question solution report on the second day

Hello everyone, I am Bubble, today I will bring you the solutions to the four questions that I punched in today.

content

One, the exclusive square number

2. The quantity that cannot be bought

3. Date of palindrome

4. Joseph Ring

Summarize


One, the exclusive square number

Topic link: Exclusive Square Number - Lanqiao Cloud Course (lanqiao.cn)

Topic requirements:

The exclusive square number is the fill in the blank of the 2013 Blue Bridge Cup National Championship, it belongs to the Blue Bridge real question

Xiao Ming was staring at the number 203879203879 in a daze.

Turns out, 203879 * 203879 = 41566646641.

What's so magical about this? If you look closely, 203879 is a 6-digit number, and the digits in each of its digits are different, and none of its squared digits appear in its own digits.

There is another 6-digit number with such characteristics, please find it!

To summarize the screening requirements:

  1. 6-bit positive integer;
  2. The number on each digit is different;
  3. Each digit of its square number does not contain any of the constituent digits of the original number.

Problem-solving ideas: This is a relatively violent simulation problem. We need to use a scattering array similar to a hash table to solve this problem. I will add detailed comments to the code. Just look at the code for details~


#include<bits/stdc++.h>
using namespace std;
int a[10];//装一个数的各位数 
int b[10];//装平方数的各位数
void p(long long n)//p用来装数的各位数 
{
	while(n)//循环 
	{
		a[n%10] = 1;//散射表 比如234567这个数字 那就是先a[7]=1,之后n/=10变23456,之后a[6]=1; 
		n /= 10;//散射表是用这个数组存储数字 如果这个数字存在就把该数字的下标位置标记为1  
	}//大家感兴趣可以去搜一下看一下知识点 
}
void d(long long n)//d用来装平方的各位数 
{
	while(n)//同上 
	{
		b[n%10] = 1;
		n /= 10;
	}
}
int main()
{
	for(long long i=213879;i<987655;i++)
	{
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));//两个数组每次都初始化 不然出问题 
		p(i);//散射i 
		d(i*i);//散射平方 
		int sum = 0;//检查这个数是否每位都不同 
		bool num = true;//标记 
		for(int j=0;j<10;j++)
		{
			sum += a[j];//让sum加上散射表所有的数,因为散射里边是1,所以加起来六位数最多是6 
		}
		if(sum!=6)
		{
			continue;
		}//如果i不是六位数不同 跳过 因为比如222222 散射表里只会有下表2的数字为1 所以sum就会等于1
		//六位数相同不满足题意 跳过 
		sum = 0;//把检查的sum重置为0 
		for(int j=0;j<10;j++)
		{
			if(a[j]==b[j])//如果i和i*i有某位相同标记num为flase 
			{
				num = false;
				break;
			}
			sum += a[j]+b[j];//因为i*i只有四个数字不同 所以两个散射表加起来如果等于10那么答案就对 
		}//因为等于十是i这数字每个位数都不同 i*i只有四个不同 如果都满足那就是这个数字 
		if(sum==10&&num==true)//如果num是false就是因为i和i*i某位相同不符合题意 所以不会输出 
		{
			cout<<i;//如果满足条件 十个位数 
		}
	}
	return 0;
}

2. The quantity that cannot be bought

Topic link: The number that cannot be bought - Lanqiao Cloud Course (lanqiao.cn)

Topic requirements:

The number that cannot be bought is the 2013 Blue Bridge Cup Zhenti, which belongs to the Blue Bridge Zhenti


Xiao Ming opened a candy store. He is ingenious: the fruit candy is packaged into two types: a pack of 4 and a pack of 7. Candy cannot be sold unpacked.

When a child comes to buy candy, he uses these two packages to combine. Of course, some candies cannot be combined, such as buying 10 candies.

You can test it with a computer, in this case, the maximum quantity you can't buy is 17. Any number greater than 17 can be combined with 4 and 7.

The requirement of this question is to find the largest number that cannot be combined when the quantity of two packages is known.

Problem-solving idea: If two numbers xy are coprime, then the largest number that cannot be formed is xy-xy. If the two numbers are not coprime, they can form any number. Mathematical Thinking

​#include<bits/stdc++.h >
using namespace std;
int main()
{
	/*两个互质的数x,y不能组成的最大整数为xy-x-y,
	如果题目给定的两个数不互质的话,那么任何数都能组成,所以给定的数一定是互质的
	*/
	int x,y;
	cin>>x>>y;
    cout<<x*y-x-y;
    return 0; 
}

3. Date of palindrome

Topic link: Palindrome Date - Lanqiao Cloud Course (lanqiao.cn)

Topic requirements:

The palindrome date is the second question of the 2020 Blue Bridge Cup programming question, it belongs to the Blue Bridge real question

During the 2020 Chinese New Year, a special date caught everyone's attention: February 2, 2020. Because if this date is written in the format of "yyyymmdd" as an 8-digit number, it is 20200202, which happens to be a palindrome. We call such dates a palindrome date.

Some people say that 20200202 is a special day "once in a thousand years". Xiao Ming disagrees with this very much, because less than 2 years later will be the next palindrome date: 20211202, which is December 2, 2021.

Some people also said that 20200202 is not just a palindrome date, but a palindrome date of ABABABA type. Xiao Ming also disagrees with this, because in about 100 years, the next palindrome date of the ABABABA type will be encountered: 21211212, which is December 12, 2121. It's not "once in a thousand years", but at most "twice in a thousand years".

Given an 8-digit date, please calculate which day is the next palindrome date and the next palindrome date of type ABABABA after the date.

Problem solving ideas:

This is a very careful question. We have to judge leap years, judge the legitimacy of the date, and judge the first ababbaba and the ones after this. You may not understand the requirements of the question. For example, 20222202 is an ABABBABA, but The two Bs in the ABAB in 2022 are not equal, one is 0 and the other is 2. The other one he requires is that the A and B of the ABAB in 2121 are the same, and then the simulation can be done directly.


#include<bits/stdc++.h>
using namespace std;
int pd(int x,int y)//此函数判断日期的合法性
{
    switch(y)
	{
        case 2:
            if((x%4==0&&x%100==1)||x%400==0)//判断闰年
                return 29;
            else
                return 28;
            break;
        case 1:
        case 3:
        case 5:
        case 7:
        case 8: 
        case 10:
        case 12:
            return 31;
        default:
            return 30;
    }
     
}
void print(int a,int b,int c)//打印函数
{//a是年b是月c是日
    cout<<a;
    if(b<10)//如果月份不到十
    {
    	cout<<"0"<<b;//补0
	}
	else
	{
        cout<<b;
	}
	if(c<10)//如果日期不到十
    {
	    cout<<"0"<<c<<endl;//补0
	}
	else
    {
	    cout<<c<<endl;
	}
}
int main()
{
    int n,y,r;
    scanf("%4d%2d%2d",&n,&y,&r);//分段输入 分为年月日
    int flag = 0;//标记函数 出现第一个abab之后标记为1以免打印重复
    for(int i=n;;i++)
	{
        int m = i%10*10+i/10%10;//m是获取月份
        int z = i/100%10*10+i/1000;//z是获取日期
        if(i>n)//如果不是同一年
		{
            if(m<=12&&m>=1&&z<=pd(i,m))//如果月份合理并且日期合理
			{
                if(flag==0)//并且未出现过abab
				{
                    print(i,m,z);//打印出来这个日期
                    flag = 1;//标记为1
                }
                if(i/1000==i/10%10&&i/100%10==i%10)//判断2121这种年份
				{
                    print(i,m,z);//如果进来了就打印
                    break;
                }    
            }
        }
        else//如果是同一年
		{
            if((m>y&&m<=12&&z<=pd(n,y))||(m==y&&z>r&&z<=pd(n,y)))//判断月份和日期合理性
			{
                if(flag == 0)//同上
				{
                    print(i,m,z);
                    flag = 1;
                }
                if(i/1000==i/10%10&&i/100%10==i%10&&i/1000!=i/100%10)//同上
				{
                    print(i,m,z);
                    break;
                }
            }
        }
    }
    return 0;
}

4. Joseph Ring

Topic link: Joseph Ring - Lanqiao Cloud Course (lanqiao.cn)

Topic requirements:

The numbers of n persons are 1 ~ nn, if they are arranged in a circle clockwise according to the number, the number is counted clockwise from the person with the number 1.

(The number of reports starts from 1) When reporting to kk, the person quits the game circle. The next person starts counting from 1 again.

Find the number of the last remaining person. This is the famous Joseph ring problem.

This problem is to find the number of the last remaining person given n, kn, and k.

Problem solving ideas:

This is a relatively classic topic. There are many articles to explain, so I won't go into details here.

#include<bits/stdc++.h>
using namespace std;
int n,k,sum;
int main()
{

    //本质上是移动了k个位置, 所以我们可以倒推,从最后胜利的那个人的下标开始 
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		sum = (sum+k)%i;
		//对i取余是上一步的情况下往后移动k个位置在目前i个人的真实编号
	}
	//因为编号是从1开始的所以要sum+1
	cout<<sum+1;
	return 0;
}

Summarize

The questions for punching in today must be careful. Everyone should read the questions carefully during the competition. Don’t follow me and hand in 19 sides with a palindrome date. Give me a follow and three links if it is helpful to you, and let's cheer for the prize together!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324131978&siteId=291194637