牛客练习赛9

题目描述
珂朵莉想每天都给威廉送礼物,于是她准备了n个自己的本子
她想送最多的天数,使得每天至少送一个本子,但是相邻两天送的本子个数不能相同
珂朵莉最多送几天礼物呢
输入描述:
第一行一个整数n
输出描述:
第一行输出一个整数,表示答案
示例1
输入
4
输出
3
说明
第一天送1个本子
第二天送2个本子
第三天送1个本子
备注:
对于100%的数据,有1 <= n <= 1000000000

思路:%3判断条件


代码:

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

int main()
{
    long long n;
    while(~scanf("%lld",&n))
    {
        long long sum=n/3;
        if(n%3==0)printf("%lld",sum*2);
        else printf("%lld",sum*2+1);
    }
    return 0;
}

题目描述
珂朵莉给你一个有根树,求有多少个子树满足其内部节点编号在值域上连续
一些数在值域上连续的意思即其在值域上构成一个连续的区间
输入描述:
第一行有一个整数n,表示树的节点数。
接下来n–1行,每行两个整数x,y,表示存在一条从x到y的有向边。
输入保证是一棵有根树。
输出描述:
输出一个数表示答案
示例1
输入
5
2 3
2 1
2 4
4 5
输出
5
说明
节点1子树中编号为1,值域连续
节点3子树中编号为3,值域连续
节点5子树中编号为5,值域连续
节点4子树中编号为4,5,值域连续
节点2子树中编号为1,2,3,4,5,值域连续
备注:
对于100%的数据,有n <=100000

思路:dfs递归求解

代码:


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<int>a[100005];
int f[100005];
int maa=0;
void dfs(int foot,int &mi,int &ma,int &ans)
{
    mi=ma=foot;ans=1;
    for(int i=0;i<a[foot].size();i++)
    {
        int q,w,e;
        dfs(a[foot][i],q,w,e);
        if(w-q+1==e)maa++;
        if(q<mi)mi=q;
        if(w>ma)ma=w;
        ans+=e;
    }


}
int main()
{
int n;

    while(~scanf("%d",&n))
    {

        for(int i=1;i<=n-1;i++)
        {
            int q,w;
            scanf("%d%d",&q,&w);
            a[q].push_back(w);
            f[w]=q;
        }

        int foot=1;
        for(int i=1;i<=n;i++)
        {
            if(f[i]==0){foot=i;break;}
        }
        int q,w,e;
        dfs(foot,q,w,e);
        if(w-q+1==e)maa++;
        cout<<maa<<endl;
    }
    return 0;
}


题目描述
珂朵莉想求123456789101112131415...的第n项
输入描述:
第一行一个整数n
输出描述:
第一行输出一个整数,表示答案
示例1
输入
3
输出
3
示例2
输入
11
输出
0
说明
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4...
第3个是3
第11个是0
备注:
对于100%的数据,有1 <= n <= 1000


思路:可以把所有数据都存在一个string里,也可以和我一样找出需要输出元素所在数字。


代码:


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

int main()
{
    int n;


    while(~scanf("%d",&n))
    {
        int ans=(n-9)/2;
        int k=(n-9)%2;
        if(n<=9)cout<<n<<endl;
        else if(ans<90||(ans==90&&k==0))
        {
            int sum=(n-9)/2+9;
            if(k==0)cout<<sum%10<<endl;
            else cout<<(sum+1)/10<<endl;
        }
        else
        {
            int sum=(n-189)/3+99;
            //cout<<sum<<" ";
            int su=(n-189)%3;
            int sum1=(sum+1)/100;
            //cout<<sum1<<" ";
            if(su==0)cout<<sum%10<<endl;
            else if(su==1)
            {
                cout<<sum1<<endl;

            }
            else cout<<((sum+1)-(sum1*100))/10<<endl;
        }

    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值