Winter holiday camp 1.20

Winter holiday camp 1.20

A - Birthday Cake

Insert picture description here

The main idea of ​​the topic: divide the cherries on the cake equally, not eccentric

Idea: All the cherries on the cake are regarded as points, with coordinates, and the cut is regarded as a line, and the problem is converted into a coefficient to satisfy that there are n points above the line and n points below the line. There should be no points on the line

The main code:

for( i=-500;i<510;i++)
	{
		int num=0;
		
        for( j=-500;j<500;j++)
		{
			int num=0;  
                if(i==0&&j==0)
					continue;  
                for(  k=0;k<2*n;k++)  
                {  
                    if((i*p[k].x+j*p[k].y)>0)
						num++;  
                    if((i*p[k].x+j*p[k].y)==0)
						break;  
                }  
                if(k==2*n&&num==n)  
                {  
                    x=0;  
                    break;  
                }  
		}
		if(x==0)
			break;
	}

Complete code:

#include<iostream>
#include<stdio.h>
#include<cmath>
 
using namespace std;
struct note
{
	int x,y;
}p[500];
int main()
{
	int n,d,i=0,j,k;
	while(scanf("%d",&n)&&n)
	{
	for( i=0;i<2*n;i++)
	{
		scanf("%d%d",&p[i].x,&p[i].y);
	}
	int x=1;
	for( i=-500;i<510;i++)
	{
		int num=0;
		
        for( j=-500;j<500;j++)
		{
			int num=0;  
                if(i==0&&j==0)
					continue;  
                for(  k=0;k<2*n;k++)  
                {  
                    if((i*p[k].x+j*p[k].y)>0)
						num++;  
                    if((i*p[k].x+j*p[k].y)==0)
						break;  
                }  
                if(k==2*n&&num==n)  
                {  
                    x=0;  
                    break;  
                }  
		}
		if(x==0)
			break;
	}
	printf("%d %d\n",i,j); 
	}
}

B - Is This Integration ?

Insert picture description here
Insert picture description here

The main idea of ​​the topic: find the area of ​​the three shaded parts

Idea: Make an auxiliary line to find the formula [External link image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-YxvLoWOx-1611306381252)(C:\Users\29252\Pictures\title\ B1.png)]

If I were to do it myself, I would never think of making auxiliary lines. . . There are still too few questions and lack of experience. It is very simple to formulate the questions, and it is impossible to push it out, and it is nothing. .

Insert picture description here

Complete code:

#include<iostream>
#include<cmath>
using namespace std;
double a;
#define PI acos(-1.0)
int main() {
	while (cin >> a) {
		printf("%.3f %.3f %.3f\n", a * a * (1 + PI / 3 - sqrt(3.0)),a * a * (PI / 3 + 2 * sqrt(3.0) - 4), a * a * (-2 * PI / 3 + 4 - sqrt(3.0)));
	}
	return 0;
}

C - Simple division

Insert picture description here

The main idea of ​​the topic: According to the given series of data, find the largest integer, so that the remainder of each integer divided by the number is the same

Idea: If the remainder of dividing two different numbers by a divisor is the same, the difference between the two different numbers must be a multiple of the divisor. (I really thought about it at first)

Use the difference to enumerate the divisor. So first find the first difference sequence of the original sequence, and then find the GCD of all non-zero elements

The main code:

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}
while (true) {
		n = 0;
		ans = 0;
		cin >> f[++n];

		if (f[n] == 0)break;
		while (f[n] != 0) cin >> f[++n];
			n--;
		for (int i = 1; i < n; ++i) {
			f[i] = f[i] - f[i + 1];
		}

Complete code:

#include<iostream>
#include<cmath>
using namespace std;
const int N = 1e6 + 10;
int f[N];
int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}
int  main() {
	int a, b,n,ans;
	while (true) {
		n = 0;
		ans = 0;
		cin >> f[++n];

		if (f[n] == 0)break;
		while (f[n] != 0) cin >> f[++n];
			n--;
		for (int i = 1; i < n; ++i) {
			f[i] = f[i] - f[i + 1];
		}
		ans = f[1];
		for (int i = 2; i < n; ++i) {
			ans = gcd(f[i] == 0 ? ans : f[i], ans);
		}
		cout << fabs(ans) << endl;
	}

	return 0;
}

D - Euclid Problem

Knowledge point: expand Euclidean algorithm

The main idea of ​​the topic: from Euclid's toss and turns, we know that for any positive integers A and B , there are integers X and Y , AX+BY=D , where D is the greatest common divisor of A and B. This question requires that for a given A and B, find the corresponding X , Y and D

Idea: Solve by using extended Euclidean algorithm

The main code:

int exgcd(int a, int b, int& x, int& y) {
	if (b == 0) {
		x = 1; y = 0;
		return a;
	}
	int d = exgcd(b, a % b, x, y);
	int tmp = x;
	x = y;
	y = tmp - (a / b) * y;
	return d;
}

Complete code:

#include<iostream>

using namespace std;

int exgcd(int a, int b, int& x, int& y) {
	if (b == 0) {
		x = 1; y = 0;
		return a;
	}
	int d = exgcd(b, a % b, x, y);
	int tmp = x;
	x = y;
	y = tmp - (a / b) * y;
	return d;
}
int main() {
	int a, b;
	while (cin >> a >> b) {
		int x = 0, y = 0;
		int d = exgcd(a, b, x, y);
		cout << x << ' ' << y << ' ' << d << endl;
	}
	return 0;
}

E - Dead Fraction

Mike is frantically scrambling to finish his thesis at the last minute. He needs to assemble all his research notes into vaguely coherent form in the next 3 days. Unfortunately, he notices that he had been extremely sloppy in his calculations. Whenever he needed to perform arithmetic, he just plugged it into a calculator and scribbled down as much of the answer as he felt was relevant. Whenever a repeating fraction was displayed, Mike simply reccorded the first few digits followed by “…”. For instance, instead of “1/3” he might have written down “0.3333…”. Unfortunately, his results require exact fractions! He doesn’t have time to redo every calculation, so he needs you to write a program (and FAST!) to automatically deduce the original fractions.
To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).

Input

There are several test cases. For each test case there is one line of input of the form “0.dddd…” where dddd is a string of 1 to 9 digits, not all zero. A line containing 0 follows the last case.

Output

For each case, output the original fraction.

Sample Input

0.2...
0.20...
0.474612399...
0

Sample Output

2/9
1/5
1186531/2500000

The main idea of ​​the topic: Convert the recurring decimal to a fraction. If there are multiple situations in the recurring part, convert it to the fraction with the smallest denominator

Insert picture description here

Idea: rational numbers can be written in the form of m/n, infinite loop decimal to fraction (0.xxxx) decimal part n bits, loop part a bit, non-circular part na bit

Numerator: n*10^n -n/10^ a Denominator: (10 (a)-1)*(10 (na)) and then reduced to the simplest fraction by gcd

This question looked at Brother Run’s code. He only understood the idea after he talked about it. When he did it, he didn’t know how to convert decimals into fractions, and he didn’t find the rules...

Complete code:

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
typedef long long ll;
ll z[11]={1};
string s;
void pre(){
	for(int i = 1;i<=10;++i)
	    z[i] = (z[i-1]<<3)+(z[i-1]<<1);
}
ll gcd(ll a,ll b){
	ll c;
	while(b){
		c=a%b;
		a=b;
		b=c;
	}
	return a;
}
int main(){
	pre();
	while((cin>>s)&&s.size()>1){
		ll num = 0;
		ll b =0;
		for(int i = 2;s[i]!='.';++i){
			num = num*10+s[i]-'0';
			b++;
		}
		ll minfz = -1;
		ll minfm = -1;
		ll fz,fm;
		for(int i = 1;i<=b;++i){
			fz  = num-num/z[i];
			fm = (z[i]-1)*z[b-i];
			ll g = gcd(fz,fm);
			if(fm/g < minfm||minfm==-1){
				minfz = fz/g;
				minfm = fm/g;
			}
		}
		printf("%lld/%lld\n",minfz,minfm);
	}
	
	return 0;
}

F - What is the Probability ?

Insert picture description here

Main idea: Calculate the probability of the i-th player winning in the k-th round

Idea: The i-th player wins in the first round, then the i-1 players before the i-th player did not win, then the probability of the i-th player winning in the first round is (1-p)^(i-1) *p

Similarly, the i-th player wins in the k-th round, and the probability of winning is (1-p)^{n(k-1)+i-1} *p. Based on the principle of addition and the geometric sequence summation formula, the first The probability of i players winning is {(1-p) (i-1)*p}*(1/(1-(1-p) n))

Complete code:

#include<iostream>
#include<cmath>
using namespace std;
int s, x, i;
double p;
double ans;
int main() {
	cin >> s;
	while (s--) {
		cin >> x >> p >> i;
		if(p==0) ans = 0;
		else ans = pow(1 - p, i - 1) * p * (1.0 / (1 - pow(1 - p, x)));
		printf("%.4f\n", ans);
	}
	return 0;
}

G - burger

Insert picture description here

The main idea of ​​the topic: 20 people attend the party, including ben and bill, 10 beef burgers, 10 cheeseburgers, coin toss, the coin head is headed, the person who tossed eats beef, otherwise, the cheese is eaten. The question asks ben and bill to eat Probability of the same burger

Idea: If you ask positively, there are many situations, so ask for the probability that the two people eat different burgers

Insert picture description here

Insert picture description here

I am still a good senior, and I know a lot about this kind of recursive problem. When I did it by myself, I was a little bit inflexible in thinking. I didn't expect to think in reverse.

The main code:

#include<iostream>
using namespace std;
const int N = 1e6+6;
int s,n;
double p[N];
int main(){
	cin>>s;
	p[1] = 1;
	for(int i = 1;i<1e6;++i)
	    p[i+1] = (2*i-1)*p[i]/(2*i);
	while(s--){
		cin>>n;
		n = n/2;
		printf("%.4f\n",1-p[n]);//反向思维 
	}
	return 0;
} 

H - Coin Toss

In a popular carnival game, a coin is tossed onto a table with an area that is covered with square tiles in a grid. The prizes are determined by the number of tiles covered by the coin when it comes to rest: the more tiles it covers, the better the prize. In the following diagram, the results from five coin tosses are shown:

img

In this example:

  • coin 1 covers 1 tile
  • coin 2 covers 2 tiles
  • coin 3 covers 3 tiles
  • coin 4 covers 4 tiles
  • coin 5 covers 2 tiles

Notice that it is acceptable for a coin to land on the boundary of the playing area (coin 5). In order for a coin to cover a tile, the coin must cover up a positive area of the tile. In other words, it is not enough to simply touch the boundary of the tile. The center of the coin may be at any point of the playing area with uniform probability. You may assume that (1) the coin always comes to a rest lying flat, and (2) the player is good enough to guarantee that the center of the coin will always come to rest on the playing area (or the boundary).

The probability of a coin covering a certain number of tiles depends on the tile and coin sizes, as well as the number of rows and columns of tiles in the playing area. In this problem, you will be required to write a program which computes the probabilities of a coin covering a certain number of tiles.

Input

The first line of input is an integer specifying the number of cases to follow. For each case, you will be given 4 integers m, n, t, and c on a single line, separated by spaces. The playing area consists of m rows and n columns of tiles, each having side length t. The diameter of the coin used is c. You may assume that 1 <= m, n <= 5000, and 1 <= c < t <= 1000.

Output

For each case, print the case number on its own line. This is followed by the probability of a coin covering 1 tile, 2 tiles, 3 tiles, and 4 tiles each on its own line. The probability should be expressed as a percentage rounded to 4 decimal places. Use the format as specified in the sample output. You should use double-precision floating-point numbers to perform the calculations. “Negative zeros” should be printed without the negative sign.

Separate the output of consecutive cases by a blank line.

Sample Input

3
5 5 10 3
7 4 25 20
10 10 10 4

Sample Output

Case 1:
Probability of covering 1 tile  = 57.7600%
Probability of covering 2 tiles = 36.4800%
Probability of covering 3 tiles = 1.2361%
Probability of covering 4 tiles = 4.5239%

Case 2:
Probability of covering 1 tile  = 12.5714%
Probability of covering 2 tiles = 46.2857%
Probability of covering 3 tiles = 8.8293%
Probability of covering 4 tiles = 32.3135%

Case 3:
Probability of covering 1 tile  = 40.9600%
Probability of covering 2 tiles = 46.0800%
Probability of covering 3 tiles = 2.7812%
Probability of covering 4 tiles = 10.1788%

The main idea of ​​the topic: toss a coin and calculate the probability that the coin will occupy 1, 2, 3, and 4 bricks respectively

Idea: [External link image transfer failed, the source site may have an anti-leech link mechanism, it is recommended to save the image and upload it directly (img-CkxoULSM-1611306381261)(C:\Users\29252\Pictures\topic\H.png)]

It’s a derivation formula question again. Before I watched the teacher’s explanation, I really felt unable to start. The question was understood and I couldn’t calculate the area occupied by coins.

The main code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const double PI=acos(-1.0),eps=1e-8;
int main()
{
    int x,cnt=0;
    cin>>x;
    while(x--){
        double n,m,t,c,A[5];
        scanf("%lf%lf%lf%lf",&n,&m,&t,&c);
        A[0]=t*t*n*m;//棋盘面积 
        A[2] = c*(t-c)*(2*m*n-n-m)+c*(c/2.0)*(2*n+2*m-4);
		A[4] = (m-1)*(n-1)*(PI*c*c)/4.0;
		A[3] = (m-1)*(n-1)*c*c-(m-1)*(n-1)*(PI*c*c)/4.0;
		A[1] = A[0]-A[2]-A[3]-A[4];
        printf("Case %d:\n",++cnt);
        for(int i=1;i<=4;i++)
            printf("Probability of covering %d tile%s = %.4lf%%\n",i,(i==1)?" ":"s",A[i]/A[0]*100.0);
        printf("\n");
    }
    return 0;
}

I - 498-bis

Insert picture description here

Main idea:

Derivative based on a given polynomial

Idea: The difficult part of this question is that the upper limit of the input number is not given, and it cannot be saved in an array. I used the stream input before, and the result is correct. There may be some parts that have not been considered. The submission is wrong. The seniors use recursive search. Regular. After reading it, I feel like I'm too food. Recurrence: A(i+1)=A(i)*x+a1;

a0

a0x + a1;

(a0x+a1)x+a2 = a0x^2+a1x+a2 derivation->2a1x+a1

(a0x^2+a1x+a2)x+a3= a0x^3 +a1x^2+a2x+a3 ->求导 3a0x^2+2a1x+a2 ->(2a0x+a1)x+a0x^2+a1x+a2;

The derivative of the latter stage is composed of the derivative of the previous stage and the original function

The latter stage is multiplied by one more than the previous stage, so its derivative is 1 more in coefficient than the previous stage

The derivative of the latter stage has an extra constant relative to the previous stage

Complete code:

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

int main(){
	int x,a;
	char c;
	while(~scanf("%d",&x)){
		int sum=0,ans = 0;
		while(~scanf("%d",&a)){
			ans = ans*x+sum;
			sum = sum*x+a;
			scanf("%c",&c);
			if(c=='\n') break;
		}
		cout<<ans<<endl;
	}
} 

;

(a0x+a1)x+a2 = a0x^2+a1x+a2 derivation->2a1x+a1

(a0x^2+a1x+a2)x+a3= a0x^3 +a1x^2+a2x+a3 ->求导 3a0x^2+2a1x+a2 ->(2a0x+a1)x+a0x^2+a1x+a2;

The derivative of the latter stage is composed of the derivative of the previous stage and the original function

The latter stage is multiplied by one more than the previous stage, so its derivative is 1 more in coefficient than the previous stage

The derivative of the latter stage has an extra constant relative to the previous stage

Complete code:

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

int main(){
	int x,a;
	char c;
	while(~scanf("%d",&x)){
		int sum=0,ans = 0;
		while(~scanf("%d",&a)){
			ans = ans*x+sum;
			sum = sum*x+a;
			scanf("%c",&c);
			if(c=='\n') break;
		}
		cout<<ans<<endl;
	}
} 

Guess you like

Origin blog.csdn.net/qq_45719435/article/details/112991061