HUST Online Judge WebBoard
Problem 4038 >> 输入一行算一行,为什么不行?显示编译错误
201803010120 @ 2019-03-19 21:10:47
[ Quote ] [ Edit ] [ Delete ] 1#
/*#include<stdio.h>
int prime(int n)
{
int a=2;
while(n>=a)
if(!(n%a++))
break;
if(a==n+1&&n!=1)
return 1;
return 0;
}
int main()
{
int n,m;
int a[10];
scanf("%d",&n);
while(n--)
{
int sum=0;
scanf("%d",&m);
for(int i = 0;i<m;++i)
scanf("%d",&a[i]);
for(i = 0;i<m;++i)
if(prime(a[i]))
sum+=a[i];
printf("%d\n",sum);
}

return 0;
}
*/
201703120121 @ 2019-03-20 22:03:33
[ Quote ] [ Edit ] [ Delete ] 2#
To:201803010120
字段有自己的作用域
看这段代码
for(int i = 0;i<m;++i) //这里声明的i,只能在该循环内有用
scanf("%d",&a[i]);
for(i = 0;i<m;++i) // i 没有声明
if(prime(a[i]))
sum+=a[i];
printf("%d\n",sum);