C语言问题,为什么会显示Runtime Error
AfterShawnseesthefollowingpicture,hedecidestogiveuphiscareerinITandturntosellfruits.S...
AfterShawn sees the following picture, he decides to give up his career in IT andturn to sell fruits. Since Shawn is a lazy guy, he doesn’t like to do any extrawork. When he starts selling fruits, he finds that he always needs to takechanges for customers. He wants you to write a program to give the least numberof changes. Shawn is also a strange guy, because he takes changes by 6,5,3,1.
Input
Thefirst line of input is an integer T which indicates the sum of test case. Eachof the following T lines contains an integer n(1≤n≤105), which isthe total money Shawn need to give to the customer.
Output
Foreach test case, output "Case i: Result" in one line whereiis the case number andResultis the least number of changes Shawnneed to give customer.
SampleInput
3
4
10
19
Sampleoutput
Case 1: 2
Case 2: 2
Case 3: 4
下面是我的:
#include <stdio.h>
int main()
{
int i,j,a[10],s;
scanf("%d",&j);
for(i=0;i<j;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<j;i++)
{
if(a[i]%5==0)
s=a[i]/5;
else
s=a[i]/6+(a[i]%6)/5+((a[i]%6)%5)/3+((a[i]%6)%5)%3;
printf("Case %d: %d\n",i+1,s);
}
return 0;
} 展开
Input
Thefirst line of input is an integer T which indicates the sum of test case. Eachof the following T lines contains an integer n(1≤n≤105), which isthe total money Shawn need to give to the customer.
Output
Foreach test case, output "Case i: Result" in one line whereiis the case number andResultis the least number of changes Shawnneed to give customer.
SampleInput
3
4
10
19
Sampleoutput
Case 1: 2
Case 2: 2
Case 3: 4
下面是我的:
#include <stdio.h>
int main()
{
int i,j,a[10],s;
scanf("%d",&j);
for(i=0;i<j;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<j;i++)
{
if(a[i]%5==0)
s=a[i]/5;
else
s=a[i]/6+(a[i]%6)/5+((a[i]%6)%5)/3+((a[i]%6)%5)%3;
printf("Case %d: %d\n",i+1,s);
}
return 0;
} 展开
1个回答
更多追问追答
追答
ACM的题,嗯,我详细看了下,
如果你的j大于10时,程序的确是runtime error的,这个问题只能用动态内存分配来做:
#include "stdio.h"
#include "stdlib.h"
int main()
{
int i,j,s;
int *a=NULL;
scanf("%d",&j);
a=(int *)malloc(sizeof(int)*j);
for(i=0;i<j;i++)
{
scanf("%d",a+i);
}
for(i=0;i<j;i++)
{
if(a[i]%5==0)
s=a[i]/5;
else
s=a[i]/6+(a[i]%6)/5+((a[i]%6)%5)/3+((a[i]%6)%5)%3;
printf("Case %d: %d\n",i+1,s);
}
free(a);
a=NULL;
return 0;
}
这样才是最对的做法,你可以用这个去提交
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询