蒟蒻想不明白。

P1208 [USACO1.3] 混合牛奶 Mixing Milk

AC Code: ```cpp #include<bits/stdc++.h> using namespace std; struct node{int x,y;}a[5001]; bool cmp(node a,node b){return a.x<b.x;} int n,m,i,ans=0,xb=0; int main(){ cin>>m>>n; if(m==0&&n==0)putchar('0'),exit(0); for(int i=1;i<=n;i++)cin>>a[i].x>>a[i].y; sort(a+1,a+n+1,cmp); for(int i=1;i<=m;i++){ if(a[xb].y==0)xb++; ans+=a[xb].x; a[xb].y--; } cout<<ans; } ``` WA Code: ```cpp #include<bits/stdc++.h> using namespace std; struct node{int x,y;}a[5001]; bool cmp(node a,node b){return a.x<b.x;} int n,m,i,ans=0,xb=0; int main(){ cin>>m>>n; if(m==n==0)putchar('0'),exit(0); for(int i=1;i<=n;i++)cin>>a[i].x>>a[i].y; sort(a+1,a+n+1,cmp); for(int i=1;i<=m;i++){ if(a[xb].y==0)xb++; ans+=a[xb].x; a[xb].y--; } cout<<ans; } ```
by HopeIsntHere @ 2022-09-15 20:15:52


@[HopeIsHere](/user/615573) 因为连等于是错误的。 你的代码实际会变成 ``` a==(b==0) ``` 也就是如果b不等于0,a等于1时也会认为满足条件
by Chanter @ 2022-09-15 20:16:05


@[Chanter](/user/231938) 草。我总以为是a和b相等,且都等于零。。。
by HopeIsntHere @ 2022-09-15 20:18:27


建议自己编译一下 ``` int a = read(); int b = read(); if( a==b==0 ) cout << " a == b == 0 " << endl ; if( a == 0 ) cout << " a == 0 " << endl ; if( b == 0 ) cout << " b == 0 " << endl ; ```
by KS_tips_CN @ 2022-09-15 20:20:44


@[KS_tips_CN](/user/427120) 好吧,我真的连测试都懒得写,不然我早就知道了/kk
by HopeIsntHere @ 2022-09-15 20:23:42


@[HopeIsHere](/user/615573) ``` 也就是如果b不等于0,a等于1时也会认为满足条件 ``` 我并不认为这种数据出现的概率很高,我觉得这不是自己调试的问题。
by Chanter @ 2022-09-15 20:26:33


@[Chanter](/user/231938) 但事实是,有一组数据是`0 0`
by HopeIsntHere @ 2022-09-15 20:27:46


@[HopeIsHere](/user/615573) 那就是```(b==0)```返回1,然后```(a==(b==0))```报错。但是我并不认为上述**编译一下**就可以理解这个代码实质
by Chanter @ 2022-09-15 20:31:20


|