699 RE
Posted: Fri Aug 08, 2003 11:45 am
I wrote a prog, solving problem #699, but it causes RuntimeError (SIGSEGV):(. What may cause it in muy prog? Please, find out an error in this listing:
[cpp]
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <fstream.h>
#include <string.h>
#ifndef ONLINE_JUDGE
#include <conio.h>
#endif
int finish;
long caseNo;
#ifdef ONLINE_JUDGE
const long MAX = 20000;
#else
const long MAX = 100;
#endif
struct node{
node *left, *right, *root;
long num, pos;
};
node *tree;
long res[MAX], rescnt, min, max;
node *create(node *root = NULL, int num = 0){//creates new node
node *nd = new node;
nd->left = NULL;
nd->right = NULL;
nd->root = root;
nd->num = num;
nd->pos = 0;
return nd;
}
void Input(node *root, int l = 1){//l == 1 means left child, else - right
int x;
node *nd;
cin >> x;
if (x == -1)
return;
nd = create(root, x);
if (l){
root->left = nd;
nd->pos = root->pos - 1;//pos may be a negative
}
else{
root->right = nd;
nd->pos = root->pos + 1;
}
if (nd->pos < min)
min = nd->pos;// I store min and max pos to calculate offset(see below)
if (nd->pos > max)
max = nd->pos;
Input(nd, 1);//Input for left subtree
Input(nd, 0);//Input for right subtree
}
void calc(node *r){
if (!r)
return;
res[r->pos - min] += r->num;//in res I store summary of leaves weight
calc(r->left);//for left subtre
calc(r->right);//for right subtree
}
void Solve(){
memset(res, 0, sizeof res);
rescnt = max - min + 1;//count of positions
calc(tree);//fills res with result values
}
void Output()
{
cout << "Case " << caseNo + 1 << ":" << endl;
for (int i = 0; i < rescnt; i++){
if (i)
cout << " ";
cout << res;
}
delete tree;
}
//#define DEBUG
#define MULTIINPUT
int main(){
#ifndef ONLINE_JUDGE
ifstream is = "input.txt";
cin = is;
#ifdef DEBUG
clrscr();
cout << endl;
#else
ofstream os = "output.txt";
cout = os;
#endif
#endif
#ifdef MULTIINPUT
for (caseNo = 0; !finish; caseNo++){
#endif
int x;
min = 0xFFFFF;
max = -min;
cin >> x;
if (x == -1)//end of file
return 0;
tree = create();//creates tree root
tree->num = x;//weight
Input(tree, 1);//Input for left subtree
Input(tree, 0);//Input for right subtree
#ifdef MULTIINPUT
if (finish)
return 0;
#endif
Solve();
if (caseNo)
cout << endl;
Output();
#ifdef MULTIINPUT
cout << endl;
}
#endif
return 0;
}
//@END_OF_SOURCE_CODE
[/cpp]
[cpp]
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <fstream.h>
#include <string.h>
#ifndef ONLINE_JUDGE
#include <conio.h>
#endif
int finish;
long caseNo;
#ifdef ONLINE_JUDGE
const long MAX = 20000;
#else
const long MAX = 100;
#endif
struct node{
node *left, *right, *root;
long num, pos;
};
node *tree;
long res[MAX], rescnt, min, max;
node *create(node *root = NULL, int num = 0){//creates new node
node *nd = new node;
nd->left = NULL;
nd->right = NULL;
nd->root = root;
nd->num = num;
nd->pos = 0;
return nd;
}
void Input(node *root, int l = 1){//l == 1 means left child, else - right
int x;
node *nd;
cin >> x;
if (x == -1)
return;
nd = create(root, x);
if (l){
root->left = nd;
nd->pos = root->pos - 1;//pos may be a negative
}
else{
root->right = nd;
nd->pos = root->pos + 1;
}
if (nd->pos < min)
min = nd->pos;// I store min and max pos to calculate offset(see below)
if (nd->pos > max)
max = nd->pos;
Input(nd, 1);//Input for left subtree
Input(nd, 0);//Input for right subtree
}
void calc(node *r){
if (!r)
return;
res[r->pos - min] += r->num;//in res I store summary of leaves weight
calc(r->left);//for left subtre
calc(r->right);//for right subtree
}
void Solve(){
memset(res, 0, sizeof res);
rescnt = max - min + 1;//count of positions
calc(tree);//fills res with result values
}
void Output()
{
cout << "Case " << caseNo + 1 << ":" << endl;
for (int i = 0; i < rescnt; i++){
if (i)
cout << " ";
cout << res;
}
delete tree;
}
//#define DEBUG
#define MULTIINPUT
int main(){
#ifndef ONLINE_JUDGE
ifstream is = "input.txt";
cin = is;
#ifdef DEBUG
clrscr();
cout << endl;
#else
ofstream os = "output.txt";
cout = os;
#endif
#endif
#ifdef MULTIINPUT
for (caseNo = 0; !finish; caseNo++){
#endif
int x;
min = 0xFFFFF;
max = -min;
cin >> x;
if (x == -1)//end of file
return 0;
tree = create();//creates tree root
tree->num = x;//weight
Input(tree, 1);//Input for left subtree
Input(tree, 0);//Input for right subtree
#ifdef MULTIINPUT
if (finish)
return 0;
#endif
Solve();
if (caseNo)
cout << endl;
Output();
#ifdef MULTIINPUT
cout << endl;
}
#endif
return 0;
}
//@END_OF_SOURCE_CODE
[/cpp]