Page Count

来源:互联网 发布:噪音测量软件 编辑:程序博客网 时间:2024/06/05 08:49

描述:

When you execute a word processor's print command, you are normally prompted to specify the pages you want printed. You might, for example, enter:10-15,25-28,8-4,13-20,9,8-8

The expression you enter is a list of print ranges, separated by commas.Each print range is either a single positive integer, or two positive integers separated by a hyphen. In the latter case we call the first integer low and the second one high. A print range for which low > high is simply ignored. A print range that specifies page numbers exceeding the number of pages is processed so that only the pages available in the document are printed. Pages are numbered starting from 1.Some of the print ranges may overlap. Pages which are common to two or more print ranges will be printed only once. (In the example given, pages 13, 14 and 15 are common to two print ranges.)


输入:

The input will contain data for a number of problem instances. For each problem instance there will be two lines of input. The first line will contain a single positive integer: the number of pages in the document. The second line will contain a list of print ranges, as defined by the rules stated above. End of input will be indicated by 0 for the number of pages. The number of pages in any book is at most 1000. The list of print ranges will be not be longer than 1000 characters.


输出:

For each problem instance, the output will be a single number, displayed at the beginning of a new line. It will be the number of pages printed by the printcommand.


样例输入:

30
10-15,25-28,8-4,13-20,9,8-8
19
10-15,25-28,8-4,13-20,9,8-8
0


样例输出:

17

12



字符串的处理。大致意思为:输入第一行为文档中的页数,之后输入一串字符串代表打印的范围列表。其中字符串中的若A-B中A>B则不处理。最后输出按打印列表命令打印的页数。




#include<stdio.h>#include<math.h>#include<string.h>int a[1005];void combil(int A,int B){if(A<=B){for(int i=A;i<=B;i++){a[i]=1;}}}int main(){int len,n,count;char str[1005];while(scanf("%d",&n),n!=0){count=0;memset(a,0,sizeof(a));scanf("%s",str);len=strlen(str);for(int i=0;i<len;){int j=i,s[2];memset(s,0,sizeof(s));            for(;str[j]!='-' && str[j]!=',' && j<len ;j++)                             //从字符串中提取数字           s[0]=s[0]*10+str[j]-'0';  if(str[j]=='-')//判断为一个范围还是单一一页   {  int k=j+1;            for(;str[k]!='-' && str[k]!=',' && k<len ;k++)            s[1]=s[1]*10+str[k]-'0'; combil(s[0],s[1]);//根据范围将数组a中下标在该范围内的值置为1  i=k+1;       }else{a[s[0]]=1;i=j+1;}   }for(int i=1;i<=n;i++)//统计数组下标不大于文档页数的范围内值为1的个数即为打印页数{if(a[i]==1)count++;}printf("%d\n",count);}return 0;} 









原创粉丝点击