以下是我的C代码。我不知道为什么数组mins(以及meds和maxs)中索引0处的值在最后一次循环迭代中发生了变化。请帮忙。
很抱歉添加了太多的打印声明。这些主要用于调试。
#include<stdio.h>
#include<stdlib.h>
#define SZ 10000
typedef long long LL;
LL min(LL x, LL y)
{
return x<y?x:y;
}
LL max(LL x, LL y)
{
return x>y?x:y;
}
struct triangle
{
LL sm, md, lg;
};
int main()
{
int n, i, ans;
struct triangle obj[n];
LL mins[n], meds[n], maxs[n];
LL a, b, c;
scanf("%d%*c", &n);
for(i=0;i<n;i++)
{
scanf("%lld%lld%lld%*c", &a, &b, &c);
printf("Scanned : %lld%lld%lld\n", a, b, c);
obj[i].sm = mins[i] = min(a, min(b,c));
obj[i].lg = maxs[i] = max(a, max(b, c));
obj[i].md = meds[i] = max(min(a,b), min(max(a,b),c));
printf("min : %lld\t med = %lld\t max = %lld\n", mins[i], meds[i], maxs[i]);
printf("min[0] = %lld\n", mins[0]);
}
printf("Outside : min[0] = %lld\n", mins[0]);
printf("mins : ");
for(i=0;i<n;i++)
printf("%lld ", mins[i]);
printf("\n");
printf("meds : ");
for(i=0;i<n;i++)
printf("%lld ", meds[i]);
printf("\n");
printf("maxs : ");
for(i=0;i<n;i++)
printf("%lld ", maxs[i]);
printf("\n");
//printf("%d\n", ans);
return 0;
}
答案 0 :(得分:4)
您使用未初始化且不确定的n
来确定数组的大小。在阅读n
之后声明数组:
int main()
{
int n, i, ans;
LL a, b, c;
if(scanf("%d%*c", &n) != 1) {
fputs("failed to read n\n", stderr);
return 1;
}
struct triangle obj[n];
LL mins[n], meds[n], maxs[n];