今日头条杯 牛客

今天补一下今日头条杯那场比赛。感觉题目都挺好的,挺考验思维。

第一道题是一道很考验基础的题目,大概初中生对这类题目最敏感了吧。

题目大意:给定一个等边三角形,然后在中间任意取一个点,然后给了三个角度,然后问有这三条新边构成的三角形是什么样子的(角度是多少)。

思路:


代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{int a,b,c;
    while(~scanf("%d %d %d",&a,&b,&c))
{
     int a1[3];
   a1[0]=a-60;
     a1[1]=b-60;
     a1[2]=c-60;
    sort(a1,a1+3);
    int t=0;
    for(int i=0;i<3;i++)
       {
      if(a1[i]<0)
    {
        t=1;
        printf("-1 -1 -1\n");
    }
       }
    if(!t)
     printf("%d %d %d\n",a1[0],a1[1],a1[2]);
 
 
}
return 0;
 
}

第二道题也蛮好的,大概是数学的组合公式的运用。

题目大意:就是给定一棵树,问相连的子树有多少种。

这道题就是利用组合公式。

其实直接分析每一个结点假如 i 结点有k个子分支(每一个分支有ai个集合),其中第i个结点是一个集合,然后第i各节点到每一个分支子集就有ai个,那么第i个分支到第j个分支有ai*aj个集合,那么i和j和k三个连起来,,,,k个分支通过结点就是a1*a2*...ak个。


具体代码如下:

#incldue<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
const int mod = 1e7+7;
ll ans[maxn];
vector<ll>p[maxn];
ll n;
ll DFS(ll x,ll fa)
{
    for(int i=0;i<p[x].size();i++)
    {
        ll y=p[x][i];
        if(y==fa){
            continue;
        }
        ans[x]=(ans[x]*(DFS(y,x)+1))%mod;
    }
    return ans[x];
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        p[x].push_back(y);
        p[y].push_back(x);
    }
    for(int i=1;i<=n;i++){
        ans[i]=1;
    }
    DFS(1,-1);
    ll sum=0;
    for(int i=1;i<=n;i++)
    {
        sum=(sum+ans[i])%mod;
    }
    printf("%lld\n",sum);
}




输入

4
1 2
2 3
3 4
输出

10
说明

For a chain, there are ten different connected subgraphs: 

第三道题是。

给了n种速度,一段长为l的路,k个加油站(可以在加油站改变速度),m个宝藏。

现在所在位置是随机的,加油站的位置也是随机的,宝藏也是随机的,而且不知道宝藏在哪。(随机跑),问这样找完所有宝藏花费的数学期望是多少。

因为是全部随机,所以就是求一种平均情况。

B—Salty Fish Go!



A few days ago, WRD was playing a small game called Salty Fish Go. We can simplify the rules of the game as follows.
The road can be abstracted into a one-dimensional axis, and the length of the road is L. There are two magic doors at the ends of the road, which can instantly transfer WRD from position L to position 0, or from location 0 to location L, without spending time.
WRD can select the initial position, the initial direction of movement, and the initial speed (from the speed set).
There are some amazing gas stations on the way, whose location is random. At the gas station WRD can change speed to one of the speed set, without spending time. (Do not change direction!)
There are some jewels on the road, whose location is random. WRD needs to take away all the jewels to win the game.
How long does it take WRD to win the game?
It’s an easy game. But considering that WRD has become a salty fish without brain, all his operations are completely random. Can you calculate the expected time for him to win the game?
输入描述:
Input contains multiple test cases, please process to the end of input.
For each test case, the first line of the input contains four positive integers V (), L (), n (), m () to indicate the size of speed set, the length of the road, the number of the gas stations, the number of the jewels.
Then, there are V integers in a line, the speed set, each integer is between 1 and .
输出描述:
The output is a real number, let your answer be a, and jury's answer be b, your answer will be considered as correct if and only if .
示例1
输入


2 8 1 1
2 4
2 8 1 1
2 4
输出


1.333333333

1.333333333


具体看代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int v,l,n,m;
    while(cin>>v>>l>>n>>m)
    {
         int sum=0;
    for(int i=1;i<=v;i++)
    {
        int x;
        cin>>x;
        sum+=x;
    }
    double lala=sum*1.0/v;
    printf("%.12f\n",m*1.0*l/(m+1)/lala);//因为加油站随机,所以可以认为m各加油站把l长的路等分为m+1等份,每一份都用平均速度跑,然后m个宝藏
    }
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值