這是我寫的程式,但 while 迴圈只執行一圈就中斷了,還顯示RE,幫幫忙,感恩!!
# include <bits/stdc++.h>
using namespace std;
int main()
{
int w,p,money;
cin >> w >> p;
vector<int> v;
for (int i=0; i<p; i++){
cin >> money;
v.push_back(money);
}
sort(v.begin(),v.end());
int Total=0 ,Count=0;
while (v.size() > 1){
int Index = v.size() - 1;
Total = v[Index];
if (Total + v[0] <= w){
Total += v[0];
v.erase(v.begin());
v.erase(v.end());
for (int i=0; i<v.size(); i++){
Total += v[0];
if (Total >= w)
break;
else
v.erase(v.begin());
}
}
else
v.erase(v.end());
Count += 1;
}
cout << Count+1 << '\n';
return 0;
}
這是我寫的程式,但 while 迴圈只執行一圈就中斷了,還顯示RE,幫幫忙,感恩!!
# include <bits/stdc++.h>
using namespace std;
int main()
{
int w,p,money;
cin >> w >> p;
vector v;
for (int i=0; i<p; i++){
cin >> money;
v.push_back(money);
}
sort(v.begin(),v.end());
int Total=0 ,Count=0;
while (v.size() > 1){
int Index = v.size() - 1;
Total = v[Index];
if (Total + v[0] <= w){
Total += v[0];
v.erase(v.begin());
v.erase(v.end());
for (int i=0; i<v.size(); i++){
Total += v[0];
if (Total >= w)
break;
else
v.erase(v.begin());
}
}
else
v.erase(v.end());
Count += 1;
}
cout << Count+1 << '\n';
return 0;
}
每组最多只能包括两件纪念品
這是我寫的程式,但 while 迴圈只執行一圈就中斷了,還顯示RE,幫幫忙,感恩!!
# include <bits/stdc++.h>
using namespace std;
int main()
{
int w,p,money;
cin >> w >> p;
vector v;
for (int i=0; i<p; i++){
cin >> money;
v.push_back(money);
}
sort(v.begin(),v.end());
int Total=0 ,Count=0;
while (v.size() > 1){
int Index = v.size() - 1;
Total = v[Index];
if (Total + v[0] <= w){
Total += v[0];
v.erase(v.begin());
v.erase(v.end());
for (int i=0; i<v.size(); i++){
Total += v[0];
if (Total >= w)
break;
else
v.erase(v.begin());
}
}
else
v.erase(v.end());
Count += 1;
}
cout << Count+1 << '\n';
return 0;
}
我從來沒寫過"v.erase(v.begin());"欸 用deque不香?
這是我寫的程式,但 while 迴圈只執行一圈就中斷了,還顯示RE,幫幫忙,感恩!!
v.erase(v.end());
應該是這裡RE,end()是最後一個元素再往後面一位,是沒有東西的。可以改成v.pop_back()
這是我寫的程式,但 while 迴圈只執行一圈就中斷了,還顯示RE,幫幫忙,感恩!!
# include <bits/stdc++.h>
using namespace std;
int main()
{
int w,p,money;
cin >> w >> p;
vector v;
for (int i=0; i<p; i++){
cin >> money;
v.push_back(money);
}
sort(v.begin(),v.end());
int Total=0 ,Count=0;
while (v.size() > 1){
int Index = v.size() - 1;
Total = v[Index];
if (Total + v[0] <= w){
Total += v[0];
v.erase(v.begin());
v.erase(v.end());
for (int i=0; i<v.size(); i++){
Total += v[0];
if (Total >= w)
break;
else
v.erase(v.begin());
}
}
else
v.erase(v.end());
Count += 1;
}
cout << Count+1 << '\n';
return 0;
}
我從來沒寫過"v.erase(v.begin());"欸 用deque不香?
我沒用過deque,可是我覺得這題不一定要用到erase()或deque,只要用兩個iterator,一個由前往後,另一個由後往前就好了