LingShuyao606 @ 2024-08-16 20:54:04
#include<bits/stdc++.h>
using namespace std;
int l,m;
bool tree[10011];
int main()
{
cin>>l>>m;
for(int i=1;i<=10011;i++) tree[i]=1;
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d %d",&u,&v);
for(int j=u;j<=v;j++)
tree[j]=0;
}
int ans=0;
for(int i=1;i<=l;i++)
if(tree[i]==1) ans++;
cout<<ans;
return 0;
}
by Lg2307 @ 2024-08-16 20:57:56
@LingShuyao606
#include <bits/stdc++.h>
using namespace std;
int main(){
int l,m,t[10010] = {0},a,b,s = 0;
cin >> l >> m;
for (int i = 0; i < m; i ++){
cin >> a >> b;
for (int j = a; j <= b; j ++)
t[j] = l;
}
for (int i = 0; i <= l; i ++)
if (t[i] == 0) s ++;
cout << s << "\n";
return 0;
}
by LingShuyao606 @ 2024-08-17 13:30:31
@Lg2307 谢谢哇
by zhongjisailuo @ 2024-08-18 18:48:51
循环要从0开始
by LingShuyao606 @ 2024-08-19 16:42:44
@zhongjisailuo 谢谢
by mingzhen_ikun @ 2024-08-29 21:01:41
可以像我一样定义一个bool型的数组,全设为false把没树的变成true,最后统计false的数量就行
送你一个小网站
代码:
#include <bits/stdc++.h>
using namespace std;
bool a[10001];
int main()
{
int l,m,u1,u2,sum=0;
cin>>l;
cin>>m;
for (int i=1;i<=m;i++)
{
cin>>u1>>u2;
for (int j=u1;j<=u2;j++)
a[j]=true;
}
for (int i=0;i<=l;i++)
if (!a[i])
sum++;
cout<<sum;
} ```
最后可以关注我吗