#22080: 求解 記憶體區段錯誤! Segmentation fault (core dumped)


ktpss97094@gmail.com (彭星樺)

學校 : 不指定學校
編號 : 86608
來源 : [49.216.40.170]
最後登入時間 :
2022-07-12 22:31:17
a271. 彩色蘿蔔 -- 兔子 | From: [101.10.7.12] | 發表日期 : 2020-08-11 16:11

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#pragma warning(disable:4996)
#define max 100

bool is_ask_day1_weight(char*, int*);
void analyze_turnips(char*, int*, int*);  //分析蘿蔔字串
void clear_string(char*);
bool weight_gain_and_loss(int*, int*, int*, int*, int*, int*, int*, int*, char*);
bool is_die(int*);

int main(void)
{
	int test = 0, x = 0, y = 0, z = 0, w = 0, n = 0, m = 0, turnips[max] = { 0 }, turnips_number = 0;
	char days[max] = { '\0' };
	scanf("%d", &test);
	getchar();
	for (int i = 0; i < test; i++)
	{
		scanf("%d %d %d %d %d %d", &x, &y, &z, &w, &n, &m);
		getchar();
		gets(days);

		if (is_ask_day1_weight(&days[0], &m))
		{
			continue;
		}
		else
		{
			analyze_turnips(&days[0], &turnips[0], &turnips_number);
			if (weight_gain_and_loss(&x, &y, &z, &w, &n, &m, turnips, &turnips_number, days))
			{
				printf("%dg\n", m);
			}
		}

	}

	return 0;
}

bool is_ask_day1_weight(char* days, int* m)
{
	if (days[0] == '\0')
	{
		printf("%d\n", *m);
		return true;
	}
	else
	{
		return false;
	}
}

void analyze_turnips(char* days, int* turnips, int* turnips_number)
{
	char register1[max] = { '\0' };
	int i = 0, j = 0;

	while (i != strlen(days) + 1)
	{
		if (days[i] == ' ' || days[i] == '\0')
		{
			turnips[*turnips_number] = atoi(register1);
			(*turnips_number)++;
			clear_string(&register1[0]);
			j = 0;
		}
		else
		{
			register1[j] = days[i];
			j++;
		}
		i++;
	}
}

void clear_string(char* str)
{
	for (int i = 0; i < max; i++)
	{
		str[i] = '\0';
	}
}

bool weight_gain_and_loss(int* x, int* y, int* z, int* w, int* n, int* m, int* turnips, int* turnips_number, char* days)
{
	int poisoning[max] = { 0 }, counter = 0, j = 0;

	for (int i = 0; i < *turnips_number; i++)
	{
		j = 0;
		//早上:中毒
		while (poisoning[j] != 0)
		{
			*m -= *n;
			j++;
		}

		if (is_die(m))
		{
			return false;
		}

		//晚上:吃蘿蔔
		switch (turnips[i])
		{
		case 0:
			break;
		case 1:
			*m += *x;
			break;
		case 2:
			*m += *y;
			break;
		case 3:
			*m -= *z;
			break;
		case 4:
			*m -= *w;
			poisoning[counter] = 1;
			counter++;
			break;
		}

		if (is_die(m))
		{
			return false;
		}

	}
	return true;
}

bool is_die(int* m)
{
	if (*m <= 0)
	{
		printf("bye~Rabbit\n");
		return true;
	}
	else
	{
		return false;
	}
}
 
#22086: Re:求解 記憶體區段錯誤! Segmentation fault (core dumped)


kkmomo (kkmomo)

學校 : 不指定學校
編號 : 29247
來源 : [223.137.94.20]
最後登入時間 :
2024-06-28 12:05:12
a271. 彩色蘿蔔 -- 兔子 | From: [118.166.127.89] | 發表日期 : 2020-08-12 01:58

1. turnips_number 值沒有 reset,一直累加超過 buffer size
2. max 設 100 如果超過 50 天就會超過 buffer size
 
#22096: Re:求解 記憶體區段錯誤! Segmentation fault (core dumped)


10851074 (JUJU IS CUTE)

學校 : 臺北市私立延平高級中學
編號 : 125103
來源 : [203.72.178.252]
最後登入時間 :
2020-09-29 16:58:36
a271. 彩色蘿蔔 -- 兔子 | From: [203.72.178.252] | 發表日期 : 2020-08-12 14:18

#include 
#include 
#include 
#include 
#pragma warning(disable:4996)
#define max 100

bool is_ask_day1_weight(char*, int*);
void analyze_turnips(char*, int*, int*);  //分析蘿蔔字串
void clear_string(char*);
bool weight_gain_and_loss(int*, int*, int*, int*, int*, int*, int*, int*, char*);
bool is_die(int*);

int main(void)
{
	int test = 0, x = 0, y = 0, z = 0, w = 0, n = 0, m = 0, turnips[max] = { 0 }, turnips_number = 0;
	char days[max] = { '\0' };
	scanf("%d", &test);
	getchar();
	for (int i = 0; i < test; i++)
	{
		scanf("%d %d %d %d %d %d", &x, &y, &z, &w, &n, &m);
		getchar();
		gets(days);

		if (is_ask_day1_weight(&days[0], &m))
		{
			continue;
		}
		else
		{
			analyze_turnips(&days[0], &turnips[0], &turnips_number);
			if (weight_gain_and_loss(&x, &y, &z, &w, &n, &m, turnips, &turnips_number, days))
			{
				printf("%dg\n", m);
			}
		}

	}

	return 0;
}

bool is_ask_day1_weight(char* days, int* m)
{
	if (days[0] == '\0')
	{
		printf("%d\n", *m);
		return true;
	}
	else
	{
		return false;
	}
}

void analyze_turnips(char* days, int* turnips, int* turnips_number)
{
	char register1[max] = { '\0' };
	int i = 0, j = 0;

	while (i != strlen(days) + 1)
	{
		if (days[i] == ' ' || days[i] == '\0')
		{
			turnips[*turnips_number] = atoi(register1);
			(*turnips_number)++;
			clear_string(&register1[0]);
			j = 0;
		}
		else
		{
			register1[j] = days[i];
			j++;
		}
		i++;
	}
}

void clear_string(char* str)
{
	for (int i = 0; i < max; i++)
	{
		str[i] = '\0';
	}
}

bool weight_gain_and_loss(int* x, int* y, int* z, int* w, int* n, int* m, int* turnips, int* turnips_number, char* days)
{
	int poisoning[max] = { 0 }, counter = 0, j = 0;

	for (int i = 0; i < *turnips_number; i++)
	{
		j = 0;
		//早上:中毒
		while (poisoning[j] != 0)
		{
			*m -= *n;
			j++;
		}

		if (is_die(m))
		{
			return false;
		}

		//晚上:吃蘿蔔
		switch (turnips[i])
		{
		case 0:
			break;
		case 1:
			*m += *x;
			break;
		case 2:
			*m += *y;
			break;
		case 3:
			*m -= *z;
			break;
		case 4:
			*m -= *w;
			poisoning[counter] = 1;
			counter++;
			break;
		}

		if (is_die(m))
		{
			return false;
		}

	}
	return true;
}

bool is_die(int* m)
{
	if (*m <= 0)
	{
		printf("bye~Rabbit\n");
		return true;
	}
	else
	{
		return false;
	}
}

簡單

 
#22097: Re:求解 記憶體區段錯誤! Segmentation fault (core dumped)


10851074 (JUJU IS CUTE)

學校 : 臺北市私立延平高級中學
編號 : 125103
來源 : [203.72.178.252]
最後登入時間 :
2020-09-29 16:58:36
a271. 彩色蘿蔔 -- 兔子 | From: [203.72.178.252] | 發表日期 : 2020-08-12 14:18

1. turnips_number 值沒有 reset,一直累加超過 buffer size
2. max 設 100 如果超過 50 天就會超過 buffer size


簡單

 
#22098: Re:求解 記憶體區段錯誤! Segmentation fault (core dumped)


10851074 (JUJU IS CUTE)

學校 : 臺北市私立延平高級中學
編號 : 125103
來源 : [203.72.178.252]
最後登入時間 :
2020-09-29 16:58:36
a271. 彩色蘿蔔 -- 兔子 | From: [203.72.178.252] | 發表日期 : 2020-08-12 14:19

1. turnips_number 值沒有 reset,一直累加超過 buffer size
2. max 設 100 如果超過 50 天就會超過 buffer size


簡單


簡單

 
ZeroJudge Forum