xm_and_xh @ 2024-07-19 15:08:49
#include <iostream>
int main()
{
using namespace std;
int a[10003], b, m, f = 0;
cin >> b >> m;
for (int i = 0; i < b; i++)
a[i] = 1;
int A, B;
for (m; m > 0; m--)
{
cin >> A >> B;
for (A; A <= B; A++)
a[A] = 0;
}
for (int i = 0; i < b; i++)
f += a[i];
cout << f;
return 0;
}
我的代码比预料结果少1,请问是哪个步骤出现问题?
by byf2011 @ 2024-07-19 15:18:17
@xm_and_xh
可以像这样:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int m, l, tre = 0;
cin >> l >> m;
vector<bool> t(l, true);
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
for (int j = u; j <= v; j++)
t[j] = false;
}
for (int i = 0; i <= l; i++)
{
if (t[i])
tre++;
}
cout << tre;
return 0;
}
求关注!!!
by w_gaoch @ 2024-08-25 21:54:26
这是我的
#include<bits/stdc++.h>
using namespace std;
int a[10005],x,y,l,r,cnt=0;
int main(){
cin>>x>>y;
for(int i=0;i<=x;i++){
a[i]=1;
}
for(int i=1;i<=y;i++){
cin>>l>>r;
for(int j=l;j<=r;j++){
a[j]=0;
}
}
for(int i=0;i<=x;i++){
if(a[i]==1){
cnt++;
}
}
cout<<cnt;
return 0;
}
你把它抽象化成一个零一组合,1为没移走的,0就是移走的,选定每一组数据的首尾做替换即可 求关注!!