練習字串的分析與處理
輸入說明 :
一個字串,內部包含數組資料,每組資料有一個序號,及一個實數。
格式如下: 序號:實數
請注意,序號有可能跳號
輸出說明 :
請求出所有序號為奇數的實數的和減掉所有序號為偶數的實數,並印出結果。
範例輸入 :
1:12.5 2:12 3:13.1 4:13.0
1:1.2 3:2.3
範例輸出 :
0.6
3.5
程式碼 :
#include <stdio.h>
int main()
{
int n;
float f,total=0.0;
char nouse;
while(scanf("%d%c%f%c",&n,&nouse,&f,&nouse)==4)
{
if(n%2 == 0)
total = total - f;
else
total = total + f;
if(nouse == '\n')
{
printf("%g\n",total);
total = 0.0;
}
}
return 0;
}
http://zerojudge.tw/ShowProblem?problemid=d018
沒有留言:
張貼留言