sj143714 @ 2022-10-02 20:33:55
#include <bits/stdc++.h>
using namespace std;
bool a[10005];
int main()
{
int n;
cin >> n;
int m;
cin >> m;
for (int i = 0 ; i < n ; i++)
{
a[i] = true;
}
int cnt = 0;
for (int i = 1 ; i <= m ; i++)
{
int c, b;
cin >> c >> b;
for (int i = c - 1; i < b ;i++)
{
if(a[i] == true)
{
cnt++;
a[i] = false;
}
}
}
cout << n - cnt + 1 << endl;
}
by Wy_x @ 2022-10-02 21:01:48
改动地方加了注释,改后AC代码:
#include <bits/stdc++.h>
using namespace std;
bool a[10005];
int main()
{
int n;
cin >> n;
int m;
cin >> m;
for (int i = 0 ; i <= n ; i++)//改为 i<=n
{
a[i] = true;
}
int cnt = 0;
for (int i = 1 ; i <= m ; i++)
{
int c, b;
cin >> c >> b;
for (int i = c; i <= b ;i++)
{//从 c-b 枚举
if(a[i] == true)
{
cnt++;
a[i] = false;
}
}
}
cout << n + 1 - cnt << endl;
}
by Wy_x @ 2022-10-02 21:01:59
@sj143714
by sj143714 @ 2022-10-03 08:13:38
@wangyixuan20090401 谢谢,过了
by RenSiYu6 @ 2022-10-03 16:11:37
#include <iostream>
using namespace std;
int main ()
{
int l,m;
cin>>l>>m;
int *p=new int [l+1];
for(int i=0;i<l+1;i++)
*(p+i)=1;
for(int i=0;i<m;i++)
{
int temp1,temp2;
cin>>temp1>>temp2;
for(int j=temp1;j<=temp2;j++)
*(p+j)=0;
}
int count=0;
for(int i=0;i<l+1;i++)
if(*(p+i)) count++;
cout<<count<<endl;
return 0;
}