2018 9th Lanqiao Cup C/C++ Group A

first question

Title: Score

1/1 + 1/2 + 1/4 + 1/8 + 1/16 + .... 
Each item is half of the previous item. If there are 20 items in total,
what is the sum and the result is expressed as a fraction .
Similar:
3/2
Of course, this is just adding the first 2 items. The numerator and denominator must be relatively prime.

Note:
The scores that need to be submitted are the scores that have been subscribed, and no spaces are allowed in the middle.
Please do not fill in any extra words or symbols.

1048575/524288 

Second question

Title: Monday

Throughout the 20th century (between January 1, 1901 and December 31, 2000), how many Mondays were there?
(Don't tell me you don't know what day it is today)

Note: What needs to be submitted is only an integer, do not fill in any extra content or explanatory text.
5217 Do with excel

Third question

Title: End of product zero

The following 10 rows of data have 10 integers in each row. How many zeros are there at the end of their product?

5650 4542 3554 473 946 4114 3871 9073 90 4329 
2758 7949 6113 5659 5245 7432 3051 4434 6704 3594 
9937 1173 6866 3397 4759 7557 3070 2287 1453 9899 
1486 5722 3135 1170 4014 5510 5120 729 2880 9019 
2049 698 4582 4346 4427 646 9742 7340 1230 7683 
5693 7015 6887 7381 4172 4341 2909 2027 7355 5649 
6701 6645 1671 5978 2704 9926 295 3125 3878 6785 
2066 4247 4800 1578 6652 4616 1113 6205 3264 2915 
3966 5291 2904 1285 2193 1428 2265 8730 9436 7074 
689 5510 8243 6114 337 4096 8199 7313 3685 211 

Note: What needs to be submitted is an integer, which represents the number of zeros at the end. Don't fill in any redundant content.

31 Find the number of 0 at the end, and then find the number of factors of 2, 5

Fourth question

Title: What is the lucky number

Tourists who travel to planet x are issued an integer as the tourist number.
The king of X star has a quirk, he only likes the numbers 3, 5 and 7.
The king stipulated that if the number of a visitor only contains factors: 3, 5, 7, they can get a prize.

Let’s look at the first 10 lucky numbers:
3 5 7 9 15 21 25 27 35 45,
so the 11th lucky number is: 49

Xiao Ming received a lucky number 59847709587505. When he went to receive the prize, he was asked to say exactly which lucky number this was, otherwise he would not receive the prize.

Please help Xiao Ming to calculate, 5904709587505 is the lucky number.

What needs to be submitted is an integer, please do not fill in any extra content.

1905

Analysis: Put the lucky numbers into the team one by one, and whether the map records are repeated.

#include<iostream>
#include<map>
#include<queue>
using namespace std;
#define Max 59084709587505
int main()
{
    queue<long long int> q;
    map<long long int,bool> m;
    int a[]={3,5,7};
    for(int i=0;i<3;i++)
    {
        q.push(a[i]);
        m[a[i]]=true;
    }
    int cnt=3;
    while(!q.empty())
    {
        long long int node=q.front();
        q.pop();
        for(int i=0;i<3;i++)
        {
            if(m[node*a[i]]!=true && node*a[i]<=Max)
            {
                m[node*a[i]]=true;
                cnt++;
                if(node*a[i]*3<=Max)
                    q.push(node*a[i]);
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}

 

Fifth question

Title: Print graphics

The following program will draw a fractal graph (that is, the overall and partial self-similar graphs) on the console.

When n=1,2,3, the output is as follows:
Please analyze the program carefully and fill in the missing codes in the underlined part.

When n=1:
 o 
ooo
 o 

n=2时:
    o    
   ooo   
    o    
 o  o  o 
ooooooooo
 o  o  o 
    o    
   ooo   
    o    

n = 3时:
             o             
            ooo            
             o             
          o o o          
         power         
          of the o          
             o             
            ooo            
             o             
    o o o    
   ooo ooo ooo   
    o o o    
 o o o o o o o o o 
wipers
 o o o o o o o o o 
    o o o    
   ooo ooo ooo   
    o o o    
             o             
            ooo            
             o             
          o o o          
         ooooooooo         
          o o o          
             o             
            ooo            
             o             

Source program:


#include <stdio.h>
#include <stdlib.h>

void show(char* buf, int w){
    int i,j;
    for(i=0; i<w; i++){
        for(j=0; j<w; j++){
            printf("%c", buf[i*w+j]==0? ' ' : 'o');
        }
        printf("\n");
    }
}

void draw(char* buf, int w, int x, int y, int size){
    if(size==1){
        buf[y*w+x] = 1;
        return;
    }
    
    int n = size/3; //填空
    draw(buf, w, x, y, n);
    draw(buf, w, x-n, y ,n);
    draw(buf, w, x+n, y ,n);
    draw(buf, w, x, y-n ,n);
    draw(buf, w, x, y+n ,n);
}

int main()
{
    int N = 3;
    int t = 1;
    int i;
    for(i=0; i<N; i++) t *= 3;
    
    char* buf = (char*)malloc(t*t);
    for(i=0; i<t*t; i++) buf[i] = 0;
    
    draw(buf, t, t/2, t/2, t);
    show(buf, t);
    free(buf);
    
    return 0;
}

Sixth question

Title: Flight Time

[Problem background]
Xiao h went to the United States to participate in the Blue Bridge International Cup. Xiao H’s girlfriend discovered that Xiao H departed at 10 am and arrived in the United States at 12 in the morning, so she sighed that “the plane is flying so fast now, and it will take two hours to reach the United States”.

Xiao H is very terrified of supersonic flight. After careful observation, it was found that the take-off and landing times of the aircraft were all local time. As there is a 12-hour time difference between Beijing and the eastern United States, the aircraft needs a total of 14 hours of flight time.

Soon H's girlfriend went to the Middle East to exchange. Little h does not know the time difference between the Middle East and Beijing. But Xiao H got his girlfriend's return flight time. Little h wants to know the flight time of his girlfriend's flight.

[Problem description]
For a flight that may cross time zones, given the round-trip take-off and landing time. Assuming that the flight time of the plane is the same, find the flight time of the plane.

[Input format]
Read in data from standard input.
One input contains multiple sets of data.

The first line of input is a positive integer T, which represents the number of input data groups.
Each set of data contains two lines, the first line is the take-off and landing time of the outbound journey, and the second line is the take-off and landing time of the return journey.
The format of take-off and landing time is as follows

h1:m1:s1 h2:m2:s2
or
h1:m1:s1 h3:m3:s3 (+1)
or
h1:m1:s1 h4:m4:s4 (+2)
means that the flight is at h1, m1, local time s1 seconds to take off,

The first format means landing at h2 hour, m2 minutes, s2 seconds,
local time, and the second format, landing at h3 hour, m3 minutes, s3 seconds, the next day, local time.
The third format means that it will land at h4, m4:s4 on the third day of local time.

For all the time given in the form of h:m:s in this topic, guarantee (0<=h<=23, 0<=m, s<=59 ).

[Output format]
Output to standard output.

For each set of data, output a line of time hh:mm:ss, which means that the flight time is hh hours, mm, minutes, and seconds.
Note that when the time is one digit, the leading zeros should be filled. For example, three hours, four minutes and five seconds should be written as 03:04:05.

[Sample input]
3
17:48:19
21:57:24 11:05:18
15:14:23 17:21:07 00:31:46 (+1)
23:02:41 16:13:20 (+1)
10:19:19 20:41:24 22:19:04 16:41:09
(+1)

[Sample output]
04:09:05
12:10:39
14:22:05

[Restrictions and Agreements]
Ensure that the input time is legal and the flight time does not exceed 24 hours.

Analysis: a departure time + flight time + time difference between two places = b landing time

            b Departure time + flight time-time difference between two places = a landing time

            Flight duration = ((landing-departure) + (landing-departure)) / 2

#include<iostream>
using namespace std;
#define hour 3600
#define minite 60
int h[4];
int m[4];
int s[4];
int day[2];
void test()
{
    if(day[0]!=0)
        h[1]+=24*day[0];
    if(day[1]!=0)
        h[3]+=24*day[1];
    
    int time;
    time=h[1]*hour+m[1]*minite+s[1]-(h[0]*hour+m[0]*minite+s[0]);
    time=(time+(h[3]*hour+m[3]*minite+s[3]-(h[2]*hour+m[2]*minite+s[2])))/2;
    int a=time/hour;
    time=time-a*hour;
    int b=time/minite;
    time=time-b*minite;
    printf("%02d:%02d:%02d\n",a,b,time);
    return;
}

int main()
{
    int T;
    cin>>T;
    char ch;
    for(int i=0;i<T;i++)
    {
        for(int j=0;j<2;j++)
        {
            scanf("%d:%d:%d %d:%d:%d",&h[j*2],&m[j*2],&s[j*2],&h[j*2+1],&m[j*2+1],&s[j*2+1]);
            while((ch=getchar())!='\n')
            {
                if(ch=='+')
                    cin>>day[j];
                if(ch=='-')
                {
                    cin>>day[j];
                    day[j]=-day[j];
                }
            }
        }
        test();
        day[0]=day[1]=0;
    }
    return 0;
}

Seventh question

Title: Three Body Attack

[Title Description] The
Trisolaran will launch an attack on the earth. In order to resist the attack, the Earthlings sent A × B × C warships to form a cube with A layer, B row and C column in space. Among them, the life value of the battleship (denoted as battleship (i, j, k)) in the jth row and kth column of the i-th layer is d(i, j, k).

The Trisolaran will launch m rounds of "cube attacks" on the earth, and each attack will cause the same damage to all warships in a small cube. Specifically, the t-th attack is described by 7 parameters lat, rat, lbt, rbt, lct, rct, ht;
all satisfy i ∈ [lat, rat], j ∈ [lbt, rbt], k ∈ [lct, rct ]’S battleship (i, j, k) will be damaged by ht. If the total damage suffered by a battleship exceeds its defense power, the battleship will explode.

The commander of the earth wants you to tell him in which round of attack the first ship to explode exploded.

[Input format]
Read in data from standard input.

The first row contains 4 positive integers A, B, C, m; the
second row contains A × B × C integers, where the th ((i − 1)×B + (j − 1)) × C + (k − 1)+1 number is d(i, j, k); in
the 3rd to m + 2, row (t − 2) contains 7 positive integers lat, rat, lbt, rbt, lct, rct , ht.

[Output format]
Output to standard output.

After which round of attack did the first exploded battleship exploded. It is guaranteed that there must be such a warship.

[Sample input]
2 2 2 3
1 1 1 1 1 1 1 1 1
1 2 1 2 1 1 1
1 1 1 2 1 2 1
1 1 1 1 1 1 2

【Sample output】
2

[Sample explanation]
After the second round of attack, the battleship (1,1,1) received a total of 2 points of damage, exceeding its defense power and causing an explosion.

[Data Convention]
For 10% of the data, B = C = 1;
for 20% of the data, C = 1;
for 40% of the data, A × B × C, m ≤ 10,000;
for 70% of the data, A, B, C ≤ 200;
for all data, A × B × C ≤ 10^6, m ≤ 10^6, 0 ≤ d(i, j, k), ht ≤ 10^9.


Resource agreement:
peak memory consumption (including virtual machines) <256M

There is no AC for this question. It should be 70% of the data.

#include<iostream>
using namespace std;
#define max 1000000
int airship[202][202][202];
int main()
{
    int A,B,C,m;
    cin>>A>>B>>C>>m;
    for(int i=1;i<=A;i++)
    {
        for(int j=1;j<=B;j++)
        {
            for(int k=1;k<=C;k++)
            {
                cin>>airship[i][j][k];
            }
        }
    }
    int lat,rat,lbt,rbt,lct,rct,ht;
    int cnt1=0;
    for(int cnt=1;cnt<=m;cnt++)
    {
        
        cin>>lat>>rat>>lbt>>rbt>>lct>>rct>>ht;
        if(cnt1!=0)
            continue;
        for(int i=lat;i<=rat;i++)
        {
            for(int j=lbt;j<=rbt;j++)
            {
                for(int k=lct;k<=rct;k++)
                {
                    airship[i][j][k]-=ht;
                    if(airship[i][j][k]<0)
                    {
                        cnt1=cnt;
                        j=rbt+1;
                        i=rat+1;
                        break;
                    }
                }
            }
        }
    }
    cout<<cnt1<<endl;
    return 0;
}

Question 8

Title: Global Warming


[Title description]
You have a picture of a certain sea area with NxN pixels, "." means ocean and "#" means land, as shown below:

.......
.##....
.##....
....##.
..####.
...###.
.......

Among them, a piece of land connected in four directions "up, down, left, and right" forms an island. For example, there are 2 islands in the picture above.  

As global warming has caused the sea to rise, scientists predict that in the next few decades, a pixel area on the edge of the island will be submerged by sea water. Specifically, if a land pixel is adjacent to the ocean (there is an ocean among the four adjacent pixels up, down, left, and right), it will be submerged.  

For example, the sea area in the above picture will become as follows in the future:

.......
.......
.......
.......
....#..
.......
.......

Please calculate: According to the prediction of scientists, how many islands in the photo will be completely submerged.  

[Input format] The
first line contains an integer N. (1 <= N <= 1000) The  
following N rows and N columns represent a sea area photo.  

The picture guarantees that the pixels in row 1, column 1, row N, and column N are all oceans.  

[Output format]
An integer represents the answer.

[Sample input]

.......
.##....
.##....
....##.
..####.
...###.
.. .....  

【Sample output】
1  


Resource agreement:
Peak memory consumption (including virtual machines) <256M
CPU consumption <1000ms


Please output strictly according to the requirements, and don't superfluously print the extra content like: "please enter...".

Note: The
main function needs to return 0;
only use ANSI C/ANSI C++ standards;
do not call special functions that depend on the compilation environment or operating system.
All dependent functions must be #include <xxx> explicitly in the source file.
Common header files cannot be omitted through project settings.

Analysis: traverse the sequence again, change all the ones that are by the sea to false, and then dfs again.

#include<iostream>
using namespace std;

int map[1010][1010];
bool geted[1010][1010];
int X[]={-1,0,0,1};
int Y[]={0,-1,1,0};
int n;
void test(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        if(x+X[i]>=0 && x+X[i]<n && y+Y[i]>=0 && y+Y[i]<n && map[x+X[i]][y+Y[i]]==0)
        {
            map[x][y]=-1;
            return;
        }
    }
}
void dfs(int x,int y)
{
    geted[x][y]=true;
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            if(map[x+X[i]][y+Y[i]]==1 && geted[x+X[i]][y+Y[i]]==false)
            {
                dfs(x+X[i],y+Y[i]);
            }
        }
    }
    return;
}
int main()
{
    cin>>n;
    char ch;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>ch;
            if(ch=='#')
            {
                map[i][j]=1;
            }
        }
    }
    int cnt=0;
    //去掉靠海的部分
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(map[i][j]==1)
            {
                test(i,j);
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(map[i][j]==1 && geted[i][j]==false)
            {
                dfs(i,j);
                cnt++;
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}

Tenth question

Title: Bill Payment Problem

[Title description] It
is common for several people to go out to eat together. But at the time of checkout, there are often some disputes.

Now there are n people eating out, and they have consumed S yuan in total. Among them, the i-th person brought ai yuan. Fortunately, the total amount of money everyone brings is enough to pay the bill, but now the question is: how much does each person pay?

For the sake of fairness, we hope that on the premise that the total payment amount is exactly S, the standard deviation of the final payment for everyone is the smallest. Here we agree that the amount of money paid by each person can be any non-negative real number, that is, it may not be an integer multiple of 1 cent. You need to output the smallest standard deviation.

Introduction to standard deviation: Standard deviation is the squared mean of the difference between multiple numbers and their averages. It is generally used to describe the "how much deviation" between these numbers. Formally speaking, suppose the i-th person pays bi yuan, then the standard deviation is: [see p1.png]

[Input format]
Read in data from standard input.

The first line contains two integers n and S; the
second line contains n non-negative integers a1, ..., an.

[Output format]
Output to standard output.

Output the smallest standard deviation, rounded to 4 decimal places.
Make sure that the correct answer will not change the rounded result after adding or subtracting 10^−9.

[Sample 1 input]
5 2333
666 666 666 666 666

【Sample output】
0.0000

[Sample explanation]
Everyone pays 2333/5 yuan, with a standard deviation of 0.

Another example:
[Sample input]
10 30
2 1 4 7 4 8 3 6 4 7

[Sample output]
0.7928

[Data Description]
For 10% of data, all ai are equal;
for 30% of data, all non-zero ai are equal;
for 60% of data, n ≤ 1000;
for 80% of data, n ≤ 10^5;
for All data, n ≤ 5 × 10^5, 0 ≤ ai ≤ 10^9.


Resource agreement:
Peak memory consumption (including virtual machines) <256M
CPU consumption <1000ms

Analysis: Find the mean, and then sort the money from small to large. Those with less money pay all, and those with more money will get the average + (the people with less money owe/the number of people with more money) to ensure that the standard deviation is small.

#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
bool cmp(int a,int b)
{
    return a<b;
}
int main()
{
    int n,S;
    cin>>n>>S;
    double standard_lack=0.0;//标准差相关
    double average=S*1.0/n;
    double lack=0.0;//当第i个人的钱少于平均值时的差值补充到lack中,需要钱多的人补交
    int flag=0;
    vector<int> money(n);
    int i;
    for(i=0;i<n;i++)
    {
        cin>>money[i];
    }
    sort(money.begin(),money.end(),cmp);
    if(money[0]>=average)
    {
        cout<<"0.0000"<<endl;
        return 0;
    }
    for(i=0;i<n;i++)
    {
        if(money[i]>=average+lack/(n-i))
        {//剩下的人交average+lack/(n-i)的钱
            flag=1;
            break;
        }
        else
        {//这些人的钱有的少于均值,有的少于 均值+钱少的人欠的钱的均值 ...话很绕口。
            standard_lack+=(money[i]-average)*(money[i]-average);
            lack=lack-(money[i]-average);
        }
    }
    if(flag==1)
    {
        double need1=lack/(n-i);
        standard_lack=standard_lack+(n-i)*need1*need1;
    }
    standard_lack=sqrt(standard_lack/n);
    printf("%0.4f",standard_lack);
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_41242380/article/details/88656910