Team competition on April 11, 2020

C - Game of Taking Stones

Title meaning: The title probably means two people, two piles of stones, and the following methods of taking stones:

  • Take any stone that is not 0 from one of the piles;
  • Take an equal number of stones from the two piles.

 If anyone finishes the two piles of stones first, he will win. If the first hand wins, the output: 1; if the second hand wins, the output: 2. There is a solution to the problem.

Problem Solution: This is a typical game naked problem- Wizov game + high precision.

  Regarding what is the Wizov game, you can refer to the following blog to understand:

https://blog.csdn.net/qq_41311604/article/details/79980882

Also pay attention to one issue here: high precision

import java.util.*;
import java.math.*;
 
 
public class Main 
{
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        BigDecimal l=BigDecimal.valueOf(2);
        BigDecimal r=BigDecimal.valueOf(3);
        BigDecimal tmp=BigDecimal.valueOf(1);
        for(int i=1;i<=500;i++)
        {
            BigDecimal mid=l.add(r).divide(BigDecimal.valueOf(2));
            tmp=mid;
            if(mid.multiply(mid).compareTo(BigDecimal.valueOf(5))<0)
            {
                l=mid;
            }
            else
            {
                r=mid;
            }
        }
        tmp=tmp.add(BigDecimal.valueOf(1));
        tmp=tmp.divide(BigDecimal.valueOf(2));
        BigDecimal a,b;
        while(cin.hasNext())
        {
            a=cin.nextBigDecimal();
            b=cin.nextBigDecimal();
            if(a.compareTo(b)>0)
            {
                BigDecimal t=a;
                a=b;
                b=t;
            }
            BigDecimal k=b.subtract(a);
            k=k.multiply(tmp);
            k=k.setScale(0,BigDecimal.ROUND_DOWN);
            if(k.compareTo(a)==0)
            {
                System.out.println(0);
            }
            else
            {
                System.out.println(1);
            }
        }
    }
}

Reprinted from: https://blog.csdn.net/Lngxling/article/details/82905107

D - A Simple Math Problem

Title: The title of this question is relatively easy to understand, given the following two conditions:

  • X + Y = a
  • Least Common Multiple (X, Y) =b

Find X and Y that meet the two conditions.

Solution: This is a typical number thesis, there is also an important mathematical conclusion,

According to the following formula:

  1. X + Y = a
  2. lcm(X,Y)=b
  3. gcd (X, Y) * lcm (X, Y) = X * Y

    From (1), (2), conclusions can be drawn:

    4. gcd(X,Y)=gcd(a,b)

Then the following equations can be derived in combination with the above relevant formulas, and the following equations can be solved:

  X*(X-a)=b*gcd(a,b)

Code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include <iostream>
#include <algorithm>
#include <cmath>
#define ll long long
using namespace std;
int main() {
    ll a,b;
    while(cin>>a>>b) {
        ll c=__gcd(a,b);
        ll t=a*a-4*b*c;
        if(t<0) cout<<"No Solution"<<endl;
        else {
            ll e=(ll)sqrt(t*1.0);
            if(e*e!=t) {
                cout<<"No Solution"<<endl;
            } else {
                ll x1=(a+e)/2;
                ll x2=(a-e)/2;
                ll x=min(x1,x2);
                cout<<x<<" "<<a-x<<endl;
            }
        }

    }
    return 0;
}

H - To begin or not to begin

Title: The title of this question is relatively simple, two people take turns to draw the ball, whoever wins the red ball will win.

Solution: This problem is also a simple game. Similar to buying a lottery ticket, the more you buy, the higher the winning rate. Of course, the only difference is that there is no "cost" in this subject.

Code:

#include<iostream>
using namespace std;
int main(){
    int n;
    while(cin>>n){
        int t=n+1;
        if(t%2==0){
            cout<<0<<endl;;
        }else{
            cout<<1<<endl;;
        }
    }
    return 0;
}

I - Convex

Title: Find the area of ​​the convex shape.

Solution: Sign in the question, pay attention to the conversion method of radian and angle.

Code:

#include<iostream>
#include<cstring>
#include<algorithm> 
#include<stdio.h> 
#include<math.h>
#define ll long long
using namespace std;
const double pi=3.14159265358979323;
int main(){
    int n,d;
    int temp;
    while(cin>>n>>d){
        double ans=0;
        for(int i=0;i<n;i++){
            cin>>temp;
            ans=ans+d*d*sin(pi*temp/180)*0.5;
        }
        printf("%.3f\n",ans);
    }
    return 0;
}

J - Find Small A

Title: The ASCII code of the character 'a' is 97. Now, find out how many times 'a' appears in the given array. Note that the numbers here are 32-bit integers in the computer. This means that a number consists of four characters (a character consists of 8 binary digits). Output an integer indicating the number of occurrences of 'a'

Solution: This question is interesting. I ca n’t understand it at the time of the game. For my teammates, my understanding is: for each element that appears in the array, convert it to a binary form, and then every 8 bits is One group, determine whether the decimal representation of the 8-bit data is 97, and then count.

Code:

#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main() {
    int N, n, cnt = 0;
    int x = 255;
    cin >> N;
    for (int i = 0; i < N; ++i) {
        cin >> n;
        do {
            if ((n & x) == 'a') {
                cnt += 1;
            }
            n >>= 8;
        } while (n);
    }
    cout << cnt << endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/blogxsc/p/12709855.html