社区讨论

关于 TZOJ 3410(蒟蒻跪求大佬指点QwQ)

学术版参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lo2pkip8
此快照首次捕获于
2023/10/23 17:41
2 年前
此快照最后确认于
2023/10/23 17:41
2 年前
查看原帖
题面:
3410: Page Count
时间限制(普通/Java):1000MS/3000MS
内存限制:65536KByte
描述
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 print command.
样例输入
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

代码:
CPP
#include <bits/stdc++.h>
using namespace std;
int p;
int change(string q){//str转int 
	int l=q.length(),k=1,K=0;
	for(int i=l-1;i>=0;i--){
		K+=(q[i]-'0')*k;
		k*=10;
		if(K>=p)return p+1;
	}
	return K;
}
int page[1002],num[1002];
int main(){
	int l,ll;string s,h;
	while(cin>>p){
		memset(page,0,sizeof(page));
		if(p==0) return 0;
		cin>>s;
		l=s.length();
		int o=0;
		for(int i=0;i<l;i++){
			if(s[i]=='-')num[o]=i,o++;
		}
		h="";
		string A,B;//A存储'-'前,B存储'-'后 
		int ke,AA,BB,sum=0,hh;//ke存储'-'下表 
		for(int i=0;i<l;i++){
			if(s[i]==','||i==l-1){
				hh=i;
				A="",B="";
				ll=h.length();
				for(ke=0;ke<ll;ke++)if(h[ke]=='-')break;
				for(int j=0;j<ke;j++)A+=h[j];
				for(int j=ke+1;j<ll;j++)B+=h[j];
				AA=change(A),BB=change(B);
				if(AA<=BB){
					for(int j=AA;j<=min(BB,p);j++){
						page[j]=1;
					}
				}
				h="";
			}
			else h+=s[i];
		}
		for(int i=1;i<=p;i++){
			if(page[i]==1)sum++;	
		}
		cout<<sum<<'\n';
	}
	return 0;
}
蒟蒻没看出什么问题
求大佬指点qwq

回复

0 条回复,欢迎继续交流。

正在加载回复...