- 輸入
- 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;
}