[問題] 加了cout 就不會segmentation fault 不加就會=.= ????
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Linux g++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
#include <algorithm>
問題(Question):
一個程式加了cout 就不會segmentation fault 不加就會= =
餵入的資料(Input):
3
4087 2750 12768
9085 12061 32226
17544 25090 21184
預期的正確結果(Expected Output):
跑完
錯誤結果(Wrong Output):
segmentation fault
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
class Tower{
public:
int x;
int y;
int z;
Tower(int a,int b,int c):x(a),y(b),z(c){}
};
// 把所有可能都push 到vector V 裡面
void permu(Tower a,vector<Tower> & V){
int x,y,z;
x=a.x;
y=a.y;
z=a.z;
V.push_back(Tower(x,y,z));
V.push_back(Tower(x,z,y));
V.push_back(Tower(y,x,z));
V.push_back(Tower(y,z,x));
V.push_back(Tower(z,x,y));
V.push_back(Tower(z,y,x));
return ;
}
//sort 的 compare function
bool cmp(Tower l , Tower r){
if(!(l.x<r.x && l.y<r.y))
return true;
return false;
}
// overload <
bool operator<(Tower l ,Tower r){
if(l.x<r.x&&l.y<r.y)return true;
return false;
}
//DP
int LIS(vector<Tower> V,int N){
int i,j,k;
int max[N+2];
int ans=-100;
for(i=0;i<N;i++)
max[i] = V[i].z;
for(i=0;i<N;i++){
for(j=0;j<i;j++){
if((V[i] < V[j])){
max[i]=max[j]+V[i].z;
}
}
}
for(i=0;i<N;i++)
if(max[i] > ans)
ans=max[i];
return ans;
}
// main program
int main(){
int i,j,k;
int T,N;
int n,l,m;
int in_x,in_y,in_z;
int Case=1;
vector<Tower> V;
while(scanf("%d",&n)==1){
if(n==0)break;
V.clear();
for(i=0;i<n;i++){
cin >> in_x >> in_y >> in_z;
Tower a(in_x,in_y,in_z);
permu(a,V);
}
// cout <<"Before Sort!!! "<<endl;
sort(V.begin(), V.end(),cmp);
int ans = LIS(V,V.size());
printf("Case %d: maximum height = %d\n",Case++,ans);
}
return 0;
}
補充說明(Supplement):
這是ACM437的題目
用LIS解 丟上去一個RE =.=
不過這不是重點
重點是我在測這個測資的時候
3
4087 2750 12768
9085 12061 32226
17544 25090 21184
沒加紅色的那一段code 就會segmentation fault
加了就不會了 可以正常的跑完
不知道這到底是怎麼回事= = 不就只是個cout 而已= =
是說我以前也有遇過這樣的狀況 不過還是無法理解
是跟buffer 有關的嗎?
謝謝回答@@@
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.244.131
→
05/25 21:45, , 1F
05/25 21:45, 1F
→
05/25 21:45, , 2F
05/25 21:45, 2F
→
05/25 21:59, , 3F
05/25 21:59, 3F
→
05/25 22:00, , 4F
05/25 22:00, 4F
→
05/25 22:06, , 5F
05/25 22:06, 5F
→
05/25 22:11, , 6F
05/25 22:11, 6F
→
05/25 22:18, , 7F
05/25 22:18, 7F
→
05/25 22:18, , 8F
05/25 22:18, 8F
→
05/25 22:29, , 9F
05/25 22:29, 9F
推
05/25 22:50, , 10F
05/25 22:50, 10F
→
05/25 22:50, , 11F
05/25 22:50, 11F
→
05/25 22:54, , 12F
05/25 22:54, 12F
→
05/25 22:55, , 13F
05/25 22:55, 13F
→
05/25 22:56, , 14F
05/25 22:56, 14F
→
05/25 22:59, , 15F
05/25 22:59, 15F
→
05/25 22:59, , 16F
05/25 22:59, 16F
→
05/25 22:59, , 17F
05/25 22:59, 17F
→
05/25 22:59, , 18F
05/25 22:59, 18F
→
05/25 23:01, , 19F
05/25 23:01, 19F
推
05/25 23:01, , 20F
05/25 23:01, 20F
→
05/25 23:02, , 21F
05/25 23:02, 21F
→
05/25 23:02, , 22F
05/25 23:02, 22F
→
05/25 23:03, , 23F
05/25 23:03, 23F
→
05/25 23:03, , 24F
05/25 23:03, 24F
→
05/25 23:03, , 25F
05/25 23:03, 25F
→
05/25 23:25, , 26F
05/25 23:25, 26F
→
05/25 23:27, , 27F
05/25 23:27, 27F
推
05/25 23:37, , 28F
05/25 23:37, 28F
→
05/25 23:45, , 29F
05/25 23:45, 29F
→
05/25 23:46, , 30F
05/25 23:46, 30F
推
05/25 23:48, , 31F
05/25 23:48, 31F
→
05/25 23:48, , 32F
05/25 23:48, 32F
→
05/25 23:49, , 33F
05/25 23:49, 33F
→
05/25 23:52, , 34F
05/25 23:52, 34F
→
05/25 23:52, , 35F
05/25 23:52, 35F
推
05/26 00:03, , 36F
05/26 00:03, 36F
推
05/26 00:17, , 37F
05/26 00:17, 37F
→
05/26 08:36, , 38F
05/26 08:36, 38F
→
05/26 08:37, , 39F
05/26 08:37, 39F