2015 6th Blue Bridge Cup Problem Solution

1. Number of Lotteries

Some people are very superstitious about numbers. For example, numbers with "4" think that they are homophonic with "death", which is unlucky.

Although these claims are pure nonsense, they sometimes have to cater to the needs of the public. The lottery number of a lottery is 5 digits (10000-99999). It is required that no number with "4" appears in it. The organizer asks you to calculate how many lottery tickets can be issued if any two lottery tickets are not duplicated.

Please submit the number (an integer) and do not write any extra content or descriptive text.

 答案:52488

Idea: Violent enumeration

Method 1 : 10000~99999,

Method 2 : 5-layer for loop

Code (Method 1):

#include<stdio.h>
int main()
{
	int i,g,s,b,q,w;
	int ans=0;
	for(i=10000;i<=99999;i++)
	{
		g=i%10;
		s=i/10%10;
		b=i/100%10;
		q=i/1000%10;
		w=i/10000;
		if(g!=4&&s!=4&&b!=4&&q!=4&&w!=4)
			ans++;
	}
	printf("%d\n",ans);
	return 0;
}

Code (Method 2):

#include<stdio.h>
int main()
{
	int g,s,b,q,w,ans=0;
	for(w=1;w<=9;w++)
		for(q=0;q<=9;q++)
			for(b=0;b<=9;b++)
				for(s=0;s<=9;s++)
					for(g=0;g<=9;g++)
						if(w!=4&&q!=4&&b!=4&&s!=4&&g!=4)
							ans++;
	printf("%d\n",ans);
	return 0;
}

2. Galaxy Bomb

There are many X-star artificial "bombs" floating in the vast space of the X galaxy, which are used as road signs in the universe.

Each bomb can be set to explode in days.
For example, if the Alpha Bomb is placed on January 1, 2015 and the time is 15 days, it will explode on January 16, 2015.
There is a beta bomb, which was placed on November 9, 2014, with a timing of 1000 days. Please calculate the exact date when it exploded.

Please fill in the date, the format is yyyy-mm-dd, which is a 4-digit year, 2-digit month, and 2-digit date. For example: 2015-02-19
please write in strict accordance with the format. No other words or symbols can appear.

答案:2017-08-05

Idea and code

3. Three sheep offer

Observe the following addition formula:

    祥 瑞 生 辉
+   三 羊 献 瑞
-------------------
 三 羊 生 瑞 气

(If there is an alignment problem, please refer to [Figure 1.jpg])
Insert picture description here

Among them, the same Chinese characters represent the same numbers, and different Chinese characters represent different numbers.

Please fill in the 4 digits represented by "Sanyang Xianrui" (the answer is only), and do not fill in any redundant content.

答案:1085

Ideas:

There are a total of 8 different numbers in the title, {Auspicious, Rui, Sheng, Hui, Three, Sheep, Xian, Qi}, each word represents a different number, (0~9),

Method 1:
Arrange all (0~9), then take the first 8 and bring them into the calculation judgment.

Method 2:
8-layer for loop, each layer of loop represents a word

Code (Method 1):

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[11]={0,1,2,3,4,5,6,7,8,9};
int main()
{
	do
	{
		if(a[4]==0)
			continue; 
		int x=a[0]*1000+a[1]*100+a[2]*10+a[3];
		int y=a[4]*1000+a[5]*100+a[6]*10+a[1];
		int z=a[4]*10000+a[5]*1000+a[2]*100+a[1]*10+a[7];
		if(x+y==z)
			printf("%d%d%d%d\n",a[4],a[5],a[6],a[1]); 
	}while(next_permutation(a+0,a+10));//左开右闭 
	return 0;
}

Code (Method 2):

#include<stdio.h>
int cnt[20];
int judge(int a,int b,int c,int d,int e,int f,int g,int h)
{
	int i;
	for(i=0;i<=9;i++)
		cnt[i]=0;
	cnt[a]++;cnt[b]++;cnt[c]++;cnt[d]++;
	cnt[e]++;cnt[f]++;cnt[g]++;cnt[h]++;
	for(i=0;i<=9;i++)
	{
		if(cnt[i]>1)
			return 0;
	}
	return 1;
}
int main()
{
	int a,b,c,d,e,f,g,h,x,y,z;
	for(a=0;a<=9;a++)//祥 
		for(b=0;b<=9;b++)//瑞 
			for(c=0;c<=9;c++)//生 
				for(d=0;d<=9;d++)//辉 
					for(e=1;e<=9;e++)//三 
						for(f=0;f<=9;f++)//羊 
							for(g=0;g<=9;g++)//献 
								for(h=0;h<=9;h++)//气 
								{ 
									x=a*1000+b*100+c*10+d;
									y=e*1000+f*100+g*10+b;
									z=e*10000+f*1000+c*100+b*10+h;
									if(x+y==z&&judge(a,b,c,d,e,f,g,h)==1)//不仅相等&&不能重复 
										printf("%d%d%d%d\n",e,f,g,b);
								}
	return 0;
}

4. Output in the grid

The StringInGrid function will print the specified string in a grid of a specified size.
The string is required to be centered in both the horizontal and vertical directions.
If the string is too long, it is truncated.
If it can't be centered, you can move it slightly to the left or up.

The following program implements this logic, please fill in the missing code in the underlined part.

#include <stdio.h>
#include <string.h>

void StringInGrid(int width, int height, const char* s)
{
	int i,k;
	char buf[1000];
	strcpy(buf, s);
	if(strlen(s)>width-2) buf[width-2]=0;

	printf("+");
	for(i=0;i<width-2;i++) printf("-");
	printf("+\n");

	for(k=1; k<(height-1)/2;k++){
		printf("|");
		for(i=0;i<width-2;i++) printf(" ");
		printf("|\n");
	}

	printf("|");

	printf("%*s%s%*s",_____________________________________________);  //填空
          
	printf("|\n");

	for(k=(height-1)/2+1; k<height-1; k++){
		printf("|");
		for(i=0;i<width-2;i++) printf(" ");
		printf("|\n");
	}	

	printf("+");
	for(i=0;i<width-2;i++) printf("-");
	printf("+\n");	
}

int main()
{
	StringInGrid(20,6,"abcd1234");
	return 0;
}

For the data in the title, it should be output: please see the picture

Insert picture description here

Note: Only fill in the missing content, do not write any existing codes or explanatory text on the title.

答案:(width-strlen(s)-2)/2,"",buf,(width-strlen(s)-2)/2,""

Ideas:

The printing is divided into five parts, the first line, the upper middle, the middle, the lower middle, and the last line.

note:

%*s中的*实际就是空格要输入对应个数  
如%*s    5,"" 五个空格

5. Nine group scores

1,2,3…9 These nine numbers form a score, and its value is exactly 1/3. How to group it?

The following program implements this function, please fill in the missing code in the underlined part.

#include <stdio.h>

void test(int x[])
{
	int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];
	int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];

	if(a*3==b) printf("%d / %d\n", a, b);
}

void f(int x[], int k)
{
	int i,t;
	if(k>=9){
		test(x);
		return;
	}

	for(i=k; i<9; i++){
		{t=x[k]; x[k]=x[i]; x[i]=t;}
		f(x,k+1);
		_____________________________________________ // 填空处
	}
}

int main()
{
	int x[] = {1,2,3,4,5,6,7,8,9};
	f(x,0);	
	return 0;
}

Note: Only fill in the missing content, do not write any existing codes or explanatory text on the title.

答案:{t=x[k]; x[k]=x[i]; x[i]=t;}

Ideas:

The title states that 9 numbers form a fraction, so it must be 4 digits in the numerator and 5 digits in the denominator. This question is actually a full arrangement question from 1 to 9. Think about it, if all the permutations can be known, then we can completely assume that the first 4 digits are the numerator and the last 4 digits are the denominator, then we only need to determine whether each permutation meets the conditions one by one.
The function f() in the question just simulates all the arrangement methods, because the recursive thought is used, there must be backtracking (return to the last state). f(x,k+1); The above line {t=x[k]; x[k]=x[i]; x[i]=t;}, then in order to return to the original state, the following line also Must {t=x[k]; x[k]=x[i]; x[i]=t;}

6. Addition to Multiplication

We all know: 1+2+3+… + 49 = 1225
Now you are required to turn the two non-adjacent plus signs into multiplication signs, making the result 2015

For example:
1+2+3+…+10 11+12+…+27 28+29+…+49 = 2015
is the answer that meets the requirements.

Please look for another possible answer and submit the number to the left of the multiplication sign in the front position (for example, submit 10).

Note: What you need to submit is an integer, do not fill in any extra content.

答案:16

Idea: Violent simulation

We already know that there are a total of 49 numbers and 48 symbols. Now let the symbols have 2 multiplication signs, and the positions of the multiplication signs cannot be continuous, then we will simulate the positions of all the multiplication signs to see which position is the most suitable.

Code:

#include<stdio.h>
int main()
{
	int i,j,a,b;
	int sum=1225;
	for(i=1;i<=48;i++)            //i表示乘号的第一个位置 
	{
		a=-(i+i+1)+(i*(i+1));     //加变乘所产生的变化 
		for(j=i+2;j<=48;j++)      //j表示乘号的第二个位置 
		{
			b=-(j+j+1)+(j*(j+1)); //加变乘所产生的变化 
			if(sum+a+b==2015)
			{
				printf("%d\n",i);
			}
		}
	}
	return 0;
}

7. Number of card types

Xiao Ming was hijacked to Casino X and forced to play cards with 3 other people.
A deck of playing cards (without the big and small trump cards, a total of 52), evenly distributed to 4 people, each with 13 cards.
At this time, a question suddenly popped up in Xiao Ming's mind:
If you don't consider suits, only points, and don't consider the order of the cards you get, how many kinds of initial card combinations can you get in your hand?

Please fill in the whole number, do not fill in any extra content or explanatory text.

Title:

Pay attention to the question not to misunderstand the meaning of the question. The focus is on the combination of cards. The
meaning of the question is that the suit is not considered. There are 13 kinds of cards, 4 of each card. The meaning of the question is that the order of the cards is not considered. Mathematical combination problem. Therefore, the intention of the question now is that 13 cards can be randomly drawn from 52 cards and asked how many different drawing methods are there.

Problem-solving ideas: violent simulation

For each card type, the number we draw is 0,1,2,3,4, and 13 variables are used to represent different card types, that is, 13 for loops, as long as the final number of cards drawn is 13. , Is a suitable drawing method. But if you use 13 levels of for loops, it will time out. In fact, you can use 12 kinds of for loops here. We only need to judge the card type 13.

Code:

#include<stdio.h>
int main()
{
	int i,j,k,l,m,n,o,p,q,r,s,t,cnt,ans=0;
	long long sum=0;
	for(i=0;i<=4;i++)
		for(j=0;j<=4;j++)
			for(k=0;k<=4;k++)
				for(l=0;l<=4;l++)
					for(m=0;m<=4;m++)
						for(n=0;n<=4;n++)
							for(o=0;o<=4;o++)
								for(p=0;p<=4;p++)
									for(q=0;q<=4;q++)
										for(r=0;r<=4;r++)
											for(s=0;s<=4;s++)
												for(t=0;t<=4;t++)
													{
														cnt=i+j+k+l+m+n+o+p+q+r+s+t;
														if(cnt<=13&&cnt>=9)
															ans++;
													}
	printf("%d\n",ans);
	return 0;
}
//3598180

The following introduces a one with less code and less time

Code:

#include<stdio.h>
int ans;
//必须把所有的堆数遍历一遍 
void dfs(int step,int sum)
{//step表示第几堆,sum表示手中的牌数
  if(sum>13)//剪枝 
		return;
	if(step==14)//走到了不存在的堆的面前再次查看自己手中的牌 
	{
 		if(sum==13)//如果手中的牌恰好是13,则是一种选择 
			ans++;
		return; 
	}

	int i;
 for(i=0;i<=4;i++)
 {
  	  sum+=i;//一次性在x堆拿i张牌
     dfs(step+1,sum);//去下一堆
   	 sum-=i;//把i张牌再放回去,选择再去拿i+1张牌
  }
  return;
}
int main()
{
 ans=0;
	dfs(1,0);//站在第一堆面前,起初手中为0张牌
  printf("%d\n",ans);
 return 0;
}

Guess you like

Origin blog.csdn.net/Helinshan/article/details/115263598