fupincheng @ 2022-12-04 17:01:59
代码如下:
l,m=map(int,input().split())
tr=[]
s=0
for h in range(l+1):
tr.append(1)
for j in range(m):
a,b=map(int,input().split())
for i in range(a,b):
tr[i]=0
for k in tr:
s+=k
print(s)
为什么头两个点没跑过???
by fupincheng @ 2022-12-04 17:03:42
等等,代码发错了
l,m=map(int,input().split())
tr=[]
for h in range(l):
tr.append(1)
for j in range(m):
a,b=map(int,input().split())
for i in range(a,b):
tr[i]=0
s=0
for k in tr:
s+=k
print(s)
by polygonolly @ 2022-12-09 19:45:38
Python我没学过,但我用C++一开始头三个点都报错了,应该是区间少了一位,比如500棵树,可能需要501个0来代表树。
C++报错原程序:
#include<iostream>
#include<vector>
using namespace std;
int main(){
int s,t,tt,m,n,ss=0;
cin>>s>>tt;
vector<int>a(s,0);
for(t=1;t<=tt;t++){
cin>>m>>n;
a.erase(a.begin()+m,a.begin()+1+n);
a.insert(a.begin()+m,n,1);}
for(t=0;t<s;t++) {
if(a[t]==0) ss++;}
cout<<ss;
return 0;
}
修改后:(就是把向量a设成s+1位了)
#include<iostream>
#include<vector>
using namespace std;
int main(){
int s,t,tt,m,n,ss=0;
cin>>s>>tt;
vector<int>a(s+1,0);
for(t=1;t<=tt;t++){
cin>>m>>n;
a.erase(a.begin()+m,a.begin()+n+1);
a.insert(a.begin()+m,n-m+1,1);}
for(t=0;t<=s;t++) if(a[t]==0) ss++;
cout<<ss;
return 0;
}
抱歉,我不懂Python,希望对你有帮助