iT邦幫忙

0

電話費計算

  • 分享至 

  • xImage

1.題目
https://ithelp.ithome.com.tw/upload/images/20201102/20128427NXIEfqK2XK.png

https://ithelp.ithome.com.tw/upload/images/20201102/20128427xQNuuQn1vA.png

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.顯示畫面
https://ithelp.ithome.com.tw/upload/images/20201102/20128427ebE06kvISW.png

4.問題:為何會顯示0.00000,而不是圖中的答案?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-11-02 20:43:15
最佳解答
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]);

即可

憤怒鳥 iT邦新手 5 級 ‧ 2020-11-02 21:34:18 檢舉

謝謝,我了解了

寫程式,真的要很細心,尤其要寫很多行程式

/images/emoticon/emoticon12.gif

我要發表回答

立即登入回答