Xin chào các anh chị, em là mem mới ạ.
Em có một vấn đề thắc mắc, và vô cùng biết ơn nếu có anh chị nào giải thích hộ em ạ.
Đây là code của em, và khi em chạy thì nó dính lỗi stack overlow (lỗi tràn ngăn xếp). Xin có vị cao nhân nào đi ngang qua chỉ em cách sửa lỗi với ạ.
#include<fstream>
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
int Dem(int a[], int l, int r) {
int d=0;
if ((l == r) && a[l]>0) {
d++;
}
else {
int mid = (l + r) / 2;
d += Dem(a, l, mid);
d += Dem(a, mid + 1, r);
}
return d;
}
int main()
{
int n;
int d;
fstream f;
f.open("input.txt", ios::in);
string data;
string line;
f >> n;
int *arr = new int[n];
//int size = -1;
int x;
while (!f.eof())
{
getline(f, line);
for (x = 0;getline(f, line);x++) {
string element;
for (int t = 0; t <= line.length();t++) {
if (line[t] != ' ')
element += line[t];
}
arr[x] = atoi(element.c_str());
}
}
f.close();
for (int i = 0; i < n;i++) {
cout << arr[i] << endl;
}
fstream f1;
f1.open("output.txt", ios::out);
int l1 = arr[0];
int r1 = arr[3];
int data1=Dem(arr, l1, r1);
f1 <<data1;
system("pause");
}
Đã hỏi |
1 tháng trước |
Đã xem |
140 |
Hoạt động |