1斤=0.5kg
1两=0.05kg
So 5斤2两 means 0.5*5+0.05*2=2.6kg.
Given similar information for other babies, your task is to find out their weights in kg.
The first line contains the number of test cases T(T<=100). Each test case contains a string in format "a斤b两"(1<=a<=10, 1<=b<=9) or "a斤"(1<=a<=10).
The input file will be encoded with UTF-8 without BOM (if you don't know what it is, you can safely ignore it).
For each test case, print the ACCURATE weight in kg (without trailing zeros).
3 5斤2两 7斤3两 6斤
Case 1: 2.6 Case 2: 3.65 Case 3: 3
Rujia Liu's Present 6: Happy 30th Birthday to Myself
Special thanks: Md. Mahbubul Hasan, Feng Chen
题意:将斤两转化为千克,注意小数后面无意义的0不能留
思路:将整数部分与小数部分分开计算
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; char str[100]; int main() { int n,len,i,j,head,end,cas = 1; double jin,liang,t; scanf("%d",&n); while(n--) { jin = liang = 0; head = end = 0; scanf("%s",str); len = strlen(str); for(i = 0; i<len; i++)//计算斤 { if(str[i]>='0' && str[i]<='9') jin = jin*10+str[i]-'0'; else break; } jin = jin*0.5;//斤化为kg head = (int)jin;//取出整数部分 t = jin - (int)jin;//小数部分保留在t中 for(j = i; j<len; j++)//找出两 { if(str[j]>='0' && str[j]<='9') { liang = str[j]-'0'; break; } } liang*=0.05;//两化为kg t = t+liang;//这是整个kg的小数部分 while(t - (int)t>1e-6)//将小数转化为整数 { t*=10; } end = (int)t; printf("Case %d: ",cas++); if(end) printf("%d.%d\n",head,end); else printf("%d\n",head); } return 0; }