zoukankan      html  css  js  c++  java
  • codeforces 803B Distances to Zero

    Distances to Zero

    题目链接:http://codeforces.com/problemset/problem/803/B

    题目大意:

    给一串数字,求每个数字到离他最近数字0的距离。。。水题

    例:2 1 0 3 0 0 3 2 4 输出:2 1 0 1 0 0 1 2 3

    思路:

    每次读入一个数字要么是0要么不是0

    ①如不是0 到0最近距离等于左边那位距离+1

    ②如是0 本身距离为0 向左循环,如果到现在输入0距离 小于 到上一个0的距离 就更新距离,到不小于位置即可

    AC代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 int a[200010]={0};
     5 bool tf=false;
     6 inline void func(bool b,int n)//is 0
     7 {
     8     if(b==true)
     9     {
    10         if(!tf)
    11         {
    12             a[n]=0;
    13             for(int j=n-1;j>0;j--)
    14                 a[j]=a[j+1]+1;
    15             tf=true;
    16         }
    17         else{
    18         int l=1;
    19         a[n]=0;
    20         for(int j=n-1;a[j]!=0&&a[j]>l;j--)
    21         {
    22             a[j]=l++;
    23         }}
    24     }
    25     else
    26     {
    27 
    28      a[n]=a[n-1]+1;
    29     }
    30 }
    31 int main()
    32 {
    33     int n,b;
    34     cin>>n;
    35     for(int i=1;i<=n;i++)
    36     {
    37         scanf("%d",&b);
    38         func(b==0,i);
    39     }
    40     for(int i=1;i<=n;i++)
    41         printf("%d ",a[i]);
    42     cout<<endl;
    43     return 0;
    44 }

    2017-05-03 22:39:55

  • 相关阅读:
    51.try块和catch块中return语句的执行
    17. 处理日期
    16.查找特定字符出现的次数
    15.字符串长度
    14.字符串拆分
    13.字符串比较
    12.幸运抽奖
    11.使用枚举
    10.获取系统时间
    MSSQL 判断临时表是否存在
  • 原文地址:https://www.cnblogs.com/Twobox/p/6804405.html
Copyright © 2011-2022 走看看