nyoj_t218(Dinner)

描述 Little A is one member of ACM team. He had just won the gold in World Final. To celebrate, he decided to invite all to have one meal. As bowl, knife and other tableware is not enough in the kitchen, Little A goes to take backup tableware in warehouse. There are many boxes in warehouse, one box contains only one thing, and each box is marked by the name of things inside it. For example, if "basketball" is written on the box, which means the box contains only basketball. With these marks, Little A wants to find out the tableware easily. So, the problem for you is to help him, find out all the tableware from all boxes in the warehouse.
輸入
There are many test cases. Each case contains one line, and one integer N at the first, N indicates that there are N boxes in the warehouse. Then N strings follow, each string is one name written on the box.
輸出
For each test of the input, output all the name of tableware.
樣例輸入
3 basketball fork chopsticks
2 bowl letter
樣例輸出
fork chopsticks
bowl

提示

The tableware only contains: bowl, knife, fork and chopsticks.

#include <cstring>
#include <string>
using namespace std;
int main()
{
	int n,i;
	char a[1010][20];
	//char *s1 = "bowl";
	while(scanf("%d",&n)!=EOF)
	{
		memset(a,0,sizeof(a));
		for (i=0;i<n;i++)
			scanf("%s",a[i]);
		for (i=0;i<n;i++)
		{
			if(strcmp(a[i],"bowl") == 0)
				printf("bowl ");
			if(strcmp(a[i],"knife") == 0)
				printf("knife ");
			if(strcmp(a[i],"fork") == 0)
				printf("fork ");
			if(strcmp(a[i],"chopsticks") == 0)
				printf("chopsticks ");
		}
		printf("\n");
	}
	return 0;
}
同樣的正常運行,下面的代碼就是提交不正確,

給出這樣的提示錯誤,不能明白這是什麼道理,在這裏,看似沒有使用指針,實際上是在無數的指針在使用着,指針的使用需要嚴格按照格式,不得出現任何的錯誤。

運行時出錯:
常見出錯的原因可能有以下幾種:
1、數組開得太小了,導致訪問到了不該訪問的內存區域
2、發生除零錯誤
3、大數組定義在函數內,導致程序棧區耗盡
4、指針用錯了,導致訪問到不該訪問的內存區域
5、還有可能是程序拋出了未接收的異常


 
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
	int n,i;
	char a[1010][20];
	//char *s1 = "bowl";
	while(scanf("%d",&n)!=EOF)
	{
		memset(a,0,sizeof(a));
		while (n--)
		{
			scanf("%s",a[i]);
			if(strcmp(a[i],"bowl") == 0)
				printf("bowl ");
			if(strcmp(a[i],"knife") == 0)
				printf("knife ");
			if(strcmp(a[i],"fork") == 0)
				printf("fork ");
			if(strcmp(a[i],"chopsticks") == 0)
				printf("chopsticks ");
		}
		printf("\n");
	}
	return 0;
}
        




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章