C language algorithm learning (mathematical algorithm, Euclidean, probability theory)

I only solved four questions today, which is uncomfortable

solved

A Birthday Cake

Problem restatement:

• Lucy and Lily are twins and today is their birthday. Mother bought them a birthday cake. Now the cake is placed in a Cartesian coordinate system, the center of the cake is at (0,0), and the radius of the cake is 100.
• There are 2N (N is an integer, 1≤N≤50) cherries on the cake. Mom will cut the cake in half with a knife (in a straight line of course). Twins naturally have to be treated fairly, that is, the two halves of the cake must have the same shape (that is, the straight line must pass through the center of the cake), and each half of the cake must have N cherries. Can you help her?
• Note here that the coordinates (x, y) of the cherry are two integers. You want to give this line in the form of two integers A, B (representing Ax+By=0), and A and B are in [−500, 500]. Cherries cannot be in a straight line. For each test case, there is at least one solution.

enter

• The input contains multiple test cases. Each test case consists of two parts: the first part gives a number N in one line, and the second part consists of 2N lines, each line has two numbers, representing (x, y). There is only one space between the two numbers. The input ends with N=0.

Output

• For each test case, output one line, giving two numbers A and B, with a space between the two numbers. If there are multiple solutions, just output one of them.

Question analysis

• Enter N in the first line of this question, which means there are 2N cherries on the cake. Each of the next 2N rows gives the coordinates of a cherry. Because the cake is a circle with the origin at the center and a radius of 100, the range of coordinate values ​​is [-100, 100]. The output of this question is a straight line equation Ax+By=0 A and B, the range is [-500, 500].
• This question adopts the enumeration method, enumerate A and B in the range of [-500, 500], and substitute the cherry coordinates into the linear equation Ax+By. If Ax+By is greater than 0, the cherry is above the line; if it is less than 0, then The cherry is below the straight line; if it is equal to 0, it is not allowed because the cherry cannot be on the straight line. Enumerate until the first solution is produced.

Source code

#include <iostream>

using namespace std;

const int LEFT = -500, RIGHT = 500;
const int N = 50;
int x[N * 2], y[N * 2];

int main()
{
    
    
    int n;
    while(~scanf("%d", &n) && n) {
    
    
        n *= 2;

        for(int i = 0; i < n; i++)
            scanf("%d%d", &x[i], &y[i]);

        bool flag = true;
        for(int a = LEFT; a <= RIGHT && flag; a++)
            for(int b = LEFT; b <= RIGHT ; b++) {
    
    
                if(a==0 && b==0 ) continue;
                int i, cnt = 0;     // 统计满足AX+BY>0的点数目
                for(i = 0; i < n; i++) {
    
    
                    if(a * x[i] + b * y[i] > 0) ++cnt;
                    else if(a * x[i] + b * y[i] == 0) break;
                }
                if( i < n) continue;
                if(cnt == n / 2) {
    
    
                    printf("%d %d\n", a, b);
                    flag = false;
                    break;
                }
            }
    }

    return 0;
}

B Is This Integration ?

Problem restatement

In the figure, there is a square ABCD, where AB=BC=CD=DA=a. With the four vertices A, B, C, D as the center, and a as the radius, draw four arcs: the arc with A as the center starts from the adjacent vertex B and ends with the adjacent vertex D; all others The arcs are drawn in a similar way. As shown in the figure, three differently shaped areas are drawn in the square in this way, and each area is represented by a different shade. Please calculate the total area of ​​the different shaded parts.
Insert picture description here

enter

• Each line of input is given a floating point number a (0<a<10000), which represents the length of the sides of the square. The input ends with EOF.

Output

• For each line of input, output one line, giving the total area of ​​three different shaded parts:
give three floating-point numbers with three decimal places, the first number represents the total area of ​​the striped area, and the second number represents The total area of ​​the dotted area, the third number indicates the area of ​​the remaining areas.

Sample Input

0.1
0.2
0.3

Sample Output

0.003 0.005 0.002
0.013 0.020 0.007
0.028 0.046 0.016

Problem resolution

Insert picture description here

Question analysis

• This question gives the side length a of a square and requires calculating the total area of ​​three different shaded parts. As shown in the figure, draw an equilateral triangle with auxiliary lines, and the areas of the three different shaded parts are represented by x, y and z.

Source code

#include<iostream>
#include<cmath>
using namespace std;
const double pi=acos(-1);
int main() {
    
    
	double a;
	while(cin>>a) {
    
    
		double z=a*a-pi*a*a/6.0-sqrt(3.0)/4.0*a*a;
		double y=(a*a-pi*a*a/4.0-2.0*z);
		double x=(a*a-4.0*y-4.0*z);
		printf("%.3lf %.3lf %.3lf\n",x,y*4.0,z*4.0);
	}
}/*
        double z = r * r * (1 - PI / 6.0 - sqrt(3.0) / 4);
        double y = r * r * (1 - PI / 4.0 ) - 2 * z;
        double x = r * r  * (PI / 2.0 - 1) - 2 * y;
		*/

C Simple division

Problem restatement

Insert picture description here
The integer division operation between the dividend n and the divisor d produces the quotient q and the remainder r. q is an integer that maximizes q d such that q d ≤ n and r=n−q*d.
• Given a set of integers, there is an integer d, so that when each given integer is divided by d, the remainder is the same.

enter

• Each line of input gives a sequence of non-zero integers separated by spaces. The last number in each line is 0, which does not belong to this sequence. There are at least 2 in a sequence and at most 1000 numbers; the numbers in a sequence are not all equal. The last line of input gives a single 0 and the program does not need to process this line.

Output

• For each line of input, output the largest integer so that each integer of the input is divided by the number with the same remainder.

Sample Input

701 1059 1417 2312 0
14 23 17 32 122 0
14 -22 17 -31 -124 0
0

Sample Output

179
3
3

Problem resolution

Source code

#include <algorithm>
#include<iostream>
using namespace std;
int gcd(int x,int y) {
    
    
	int r;
	while (x%y!=0) {
    
    
		r=x%y;
		x=y;
		y=r;
	}
	return y;
}
int main() {
    
    
	int a1, a;
	while(~scanf("%d", &a1) && a1) {
    
    
		int g = 0;
		while(scanf("%d", &a) == 1 && a) {
    
    
			int d = a - a1;
			if(d) {
    
    
				if(g) g = gcd(g, d);
				else g = d;
			}
			a1 = a;
		}
		printf("%d\n", abs(g));
	}

	return 0;
}

G - burger

Problem restatement

• The twin sons of the Clintons, Ben and Bill, celebrated their 10th birthday and the party was held at the McDonald’s restaurant at 202 South Broadway in New York. The party was attended by 20 children, including Ben and Bill. Ronald McDonald made 10 beef burgers and 10 cheeseburgers. When he served the children, he started with the girl sitting on Bill's left and Ben sitting on Bill's right. Ronald tossed a coin to decide whether the girl would eat a beef burger or a cheeseburger. The side of the coin's head is the beef burger, and the opposite is the cheeseburger. Before it was Ben and Bill's turn, Ronald repeated the process with the other 17 children. When Ronald came to Ben, he didn't need to toss a coin anymore, because there was no cheeseburger, only two beef burgers.
• Ronald McDonald was very surprised by this, so he wanted to know the probability of this kind of thing happening. For the above process, please calculate the probability that Ben and Bill will eat the same burger. Ronald McDonald always grills the same number of beef burgers and cheeseburgers.

problem analysis

Insert picture description here
As you can see from the above figure, you can use offline to solve, (the first time to use offline method, not skilled)

Source code

#include<iostream>
#include<cmath>
using namespace std;
const int N=5e4+7;
int cas,n;
double p[N];
void solve()
{
    
    
	p[1]=1;
	for(int i=1;i<N-1;++i)
		p[i+1]=(2*i-1)*p[i]/(2*i);
}
int main(){
    
    
	solve();
	cin>>cas;
	while(cas--){
    
    
		cin >>n;
		n/=2;
		printf("%.4lf\n",1-p[n]);
	}
	return 0;
}

Unresolved part

D - Euclid Problem

From Euclid it is known that for any positive integers A and B there exist such integers X and Y thatAX + BY = D, where D is the greatest common divisor of A and B. The problem is to find for givenA and B corresponding X, Y and D.

Input

The input will consist of a set of lines with the integer numbers A and B, separated with space
(A, B < 1000000001).

Output

For each input line the output line should consist of three integers X, Y and D, separated with space.
If there are several such X and Y , you should output that pair for which |X| + |Y | is the minimal. Ifthere are several X and Y satisfying the minimal criteria, output the pair for which X ≤ Y .

Sample Input

4 6
17 17

Sample Output

-1 1 2
0 1 17

problem analysis

Obviously, this problem should be done with the extended Euclidean algorithm

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

}

However, I don’t have AC and I can’t find the reason

Source code

#include <cstdio>
#include <cmath>
#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 t=exgcd(b, a%b, x, y);
	int x0=x, y0=y;
	x=y0;
	y=x0-(a/b)*y0;
	return t;
}
int main() {
    
    
	int a, b, x, y, d;
	while(~scanf("%d%d", &a, &b)) {
    
    
		d = exgcd(a, b, x, y);
		printf("%d %d\n",x,y);
	}
	return 0;
}

F - What is the Probability ?

Probability has always been an integrated part of computer algorithms. Where the deterministicalgorithms have failed to solve a problem in short time, probabilistic algorithms have come to the rescue. In this problem we are not dealing with any probabilistic algorithm. We will just try to determine the winning probability of a certain player.
A game is played by throwing a dice like thing (it should not be assumed that it has six sides like an ordinary dice). If a certain event occurs when a player throws the dice (such as getting a 3, getting green side on top or whatever) he is declared the winner. There can be N such player. So the first
player will throw the dice, then the second and at last the N-th player and again the first player and so on. When a player gets the desired event he or she is declared winner and playing stops. You will have to determine the winning probability of one (The I-th) of these players.

Input

Input will contain an integer S (S ≤ 1000) at first, which indicates how many sets of inputs are there.The next S lines will contain S sets of inputs. Each line contain an integer N (N ≤ 1000) which denotes the number players, a floating point number p which indicates the probability of the happening of a successful event in a single throw (If success means getting 3 then p is the probability of getting 3 in a single throw. For a normal dice the probability of getting 3 is 1/6), and I (I ≤ N) the serial of the player whose winning probability is to be determined (Serial no varies from 1 to N). You can assume that no invalid probability § value will be given as input.

Output

For each set of input, output in a single line the probability of the I-th player to win. The output
floating point number will always have four digits after the decimal point as shown in the sample
output.

Sample Input

2
2 0.166666 1
2 0.166666 2

Sample Output

0.5455
0.4545

problem analysis

Insert picture description here
Although I substituted the formula, I don’t have AC and I can’t find the reason

Source code

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main() {
    
    
	int N,i,n;
	double p;
	scanf("%d",&N);
	for(int j=0; j<N; j++) {
    
    
		cin >>n >>p >>i;
		float proba =(pow((1-p),i-1)*p/(1-pow((1-p),n)));
		printf("%.4f\n",proba);
	}
}

There are still many problems left today, I don’t know when I will have time to solve them

Guess you like

Origin blog.csdn.net/seekerzhz/article/details/112910421