1.題目
2.程式碼
# include <stdio.h>
# include <stdlib.h>
int main(void)
{
int n;//資料數目
scanf("%d", &n);
float inside[n]; //網內
float outside[n]; //網外
float sum[n]; //最後總金額
int i;//counter
for(i = 0; i < n; i = i + 1) //輸入網內 網外 通話時數
scanf("%f %f", &inside[n], &outside[n]);
for(i = 0; i < n; i = i + 1) //將網內通話時數轉成金額
{
if((inside[i] >= 0) && (inside[i] <= 300))
inside[i] = inside[i] * 3;
else if((inside[i] >= 301) && (inside[i] <= 500))
inside[i] = inside[i] * 3 * 0.9;
else if((inside[i] >= 501) && (inside[i] <= 1000))
inside[i] = inside[i] * 3 * 0.75;
else
inside[i] = inside[i] * 3 * 0.5;
}
for(i = 0; i < n; i = i + 1) //將網外通話時數轉成金額
{
if((outside[i] >= 0) && (outside[i] <= 300))
outside[i] = outside[i] * 6;
else if((outside[i] >= 301) && (outside[i] <= 500))
outside[i] = outside[i] * 6;
else if((outside[i] >= 501) && (outside[i] <= 1000))
outside[i] = outside[i] * 6 * 0.9;
else
outside[i] = outside[i] * 6 * 0.75;
}
printf("\n");
for(i = 0; i < n; i = i + 1) //得出總金額,並顯示
{
sum[i] = inside[i] + outside[i];
printf("%f\n", sum[i]);
}
system("pause");
return 0;
}
3.顯示畫面
4.問題:為何會顯示0.00000,而不是圖中的答案?
for(i = 0; i < n; i = i + 1) //輸入網內 網外 通話時數
scanf("%f %f", &inside[n], &outside[n]);
改成
for(i = 0; i < n; i = i + 1) //輸入網內 網外 通話時數
scanf("%f %f", &inside[i], &outside[i]);
即可