gushouchuan @ 2023-09-24 11:00:39
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996);
int main() {
int l = 0, m = 0, start = 0, end = 0;
scanf("%d %d", &l, &m);//马路长度以及区间数目
int tree[10005];
for (int i = 0; i < l; i++) {
*(tree + i) = 1;
}
int move_cnt = 0;
for (int j = 0; j < m; j++) {
scanf("%d %d", &start, &end);
for (int m = start-1; m < end; m++) {
if (*(tree + m) == 1) {
*(tree + m) = 0;
move_cnt++;
}
else continue;
}
}
printf("%d", l - move_cnt + 1);
return 0;
}
by ___A__ @ 2023-09-24 11:06:48
初始化要取等号 @gushouchuan
by xiaoxiaoyyds @ 2023-09-24 11:08:44
@gushouchuan 你要把第10行的i<l该成i<=l; 后面遍历m时不要把start和end减1
by yyfhs @ 2023-09-24 11:27:25
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[10005],m,n,l,r,ans=0;
cin>>m>>n;
for(int i=0;i<=m;i++)
{
a[i]=1;
}
for(int i=1;i<=n;i++)
{
cin>>l>>r;
for(int j=l;j<=r;j++)
{
if(a[j]==1)
{
a[j]=0;
}
}
}
for(int i=0;i<=m;i++)
{
if(a[i]==1)
{
ans++;
}
}
cout<<ans;
return 0;
}
以上代码
by gushouchuan @ 2023-09-24 17:55:42
@xiaoxiaoyyds 已解决谢谢!
by xiaoxiaoyyds @ 2023-09-24 19:03:11
@gushouchuan 不要脸的求个关