HDU 3777 Page Count 字符串处理

 

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3777

 

题意:要打印一本书中的几页,然后会给出几组数据,比如

10-15,25-28,8-4,13-20,9,8-8  但是8-4这个倒着的就是被忽视的,

而且超过书本页数的那部分也会被忽视。

先输入一个 数字(表示书的页数)  然后是一行字符串;

直到输入一个 0 结束。

 

就是处理字符串,然后扫一遍区间。

 

————————————————————————————————————

渣渣之见,随便转载。

 

#include<cstdio>
#include<cstring>
#define maxn 1005

int book[maxn];

int main()
{
//    freopen("C:\\Users\\ZDH\\Desktop\\a.txt","r",stdin);
    int n;
    while(~scanf("%d",&n) && n!=0)
    {
        memset(book,0,sizeof book);
        char s[maxn];
        scanf(" %s",s);
        for(int i=0;i<strlen(s);)
        {
            int a=0,j=i;
            for(;s[j]!='-' && s[j]!=',' && j<strlen(s) ;j++)
                a=a*10+s[j]-'0';
            if(s[j]=='-')
            {
                int b=0;
                for(j++;s[j]!=',' && j<strlen(s);j++)
                    b=b*10+s[j]-'0';
                if(b>n) b=n;    
                if(a<=b)
                    for(int k=a;k<=b;k++)
                        book[k]=1;
            }
            else
                book[a]=1;
            i=j+1;
        }
        int ans=0;
        for(int i=1;i<=n;i++)
            if(book[i])
                ans++;
        printf("%d\n",ans);
    }
}

 

posted @ 2016-03-15 20:51  后知后觉丶  阅读(155)  评论(0编辑  收藏  举报