【Acwing 周赛#91】4862. 浇花

目录

4861. 构造数列 - 贪心ac

4862. 浇花 - 差分ac


4861. 构造数列 - 贪心ac

4861. 构造数列 - AcWing题库

题目:

我们规定如果一个正整数满足除最高位外其它所有数位均为 0,则称该正整数为圆数。

例如,1,8,900,70,5000 都是圆数,120,404,333,8008 都不是圆数。

给定一个正整数 n,请你构造一个圆数数列,要求:

  • 数列中所有元素相加之和恰好为 n
  • 数列长度尽可能短

思路:

像5678这种,拆分输出5000,600,70,8

如果x%10=0,则不输出

import java.util.*;

class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        while(n-->0)
        {
            int x=sc.nextInt();
            int cnt=0,res=0;
            List<Integer> a=new ArrayList<>();
            while(x>0)
            {
                int t=x%10;
                if(t!=0)
                {
                    a.add((int)(t*Math.pow(10,cnt)));
                    res++;
                }
                cnt++;
                x/=10;
            }
            System.out.println(res);
            for(int p:a) System.out.print(p+" ");
            System.out.println();
        }
    }
}

 

4862. 浇花 - 差分ac

4862. 浇花 - AcWing题库

import java.util.*;

class Main
{
    static int N=100010;
    static int[] b=new int[N],a=new int[N];
    
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(),m=sc.nextInt();
        for(int i=1;i<=n;i++)
        {
            a[i]=0;
            b[i]=a[i]-a[i-1];
        }
        while(m-->0)
        {
            int l=sc.nextInt(),r=sc.nextInt();
            b[l]++;
            b[r+1]--;
        }
        for(int i=1;i<=n;i++) b[i]+=b[i-1];

        int i=1;
        for(i=1;i<=n;i++) 
            if(b[i]>1||b[i]==0) 
            {
                System.out.print(i+" "+b[i]);
                return;
            }
        if(i==n+1) System.out.print("OK");
        
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值