Page Count

描述:

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;
} 









### 已知问题分析 `pdf2image.exceptions.PDFInfoNotInstalledError: Unable to get page count` 错误通常表明 `pdf2image` 无法找到 Poppler 的可执行文件,或者这些文件未被正确配置到系统的 PATH 环境变量中。此错误的核心原因是缺少必要的依赖项——Poppler 工具集。 --- ### 解决方案概述 以下是针对该问题的具体解决方法: #### 1. 安装 Poppler Poppler 是一个开源工具库,专门用于处理 PDF 文件。为了使 `pdf2image` 正常工作,必须先安装 Poppler 并确保其路径已被添加到系统的环境变量中[^3]。 对于 Windows 用户: - 下载适用于 Windows 的预编译版本的 Poppler 可执行文件(可以从第三方网站获取,例如 [https://github.com/oschwartz10612/poppler-windows](https://github.com/oschwartz10612/poppler-windows))。 - 将下载后的解压目录中的 `bin` 子目录路径添加到系统的 PATH 环境变量中。 对于 Linux 用户: - 使用包管理器安装 Poppler。例如,在 Ubuntu 上可以运行以下命令来完成安装: ```bash sudo apt-get install poppler-utils ``` 对于 macOS 用户: - 使用 Homebrew 来安装 Poppler: ```bash brew install poppler ``` #### 2. 验证 Poppler 是否可用 在终端或命令提示符下输入以下命令以验证 Poppler 是否成功安装并能正常访问: ```bash pdftoppm -v ``` 如果返回了 Poppler 版本号,则说明安装成功[^4]。 #### 3. 修改 Python 脚本 (如有必要) 如果你不想修改全局 PATH 环境变量,也可以通过指定 Poppler 的路径来初始化 `pdf2image`。例如: ```python from pdf2image import convert_from_path # 显式提供 Poppler 的 bin 目录位置 poppler_path = r"C:\path\to\poppler\bin" images = convert_from_path('example.pdf', poppler_path=poppler_path) for i, image in enumerate(images): image.save(f'page_{i + 1}.jpg', 'JPEG') ``` 上述代码片段展示了如何显式传递 Poppler 的路径参数给 `convert_from_path()` 函数[^1]。 #### 4. 检查其他潜在问题 即使完成了以上步骤仍出现问题时,请确认以下几点: - 所使用的 `pdf2image` 和 Poppler 版本兼容; - 输入的 PDF 文件本身无损且格式有效。 --- ### 总结 通过安装 Poppler 工具集并将之加入系统 PATH 或者直接传入具体路径至 `pdf2image.convert_from_path()`, 即可消除 `PDFInfoNotInstalledError` 这一异常情况[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值