BNUOJ27888:Baby Me
来源:互联网 发布:js正则表达式 i g 编辑:程序博客网 时间:2024/05/21 01:48
When I was born, I was 5斤2两。Sorry for non-Chinese people. Here's what it means:
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.
Input
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).
Output
For each test case, print the ACCURATE weight in kg (without trailing zeros).
Sample Input
35斤2两7斤3两6斤
Output for the Sample Input
Case 1: 2.6Case 2: 3.65Case 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;}
- BNUOJ27888:Baby Me
- UVA-12555 - Baby Me(控制精度)
- BABY
- baby
- baby
- Baby
- baby,baby
- Needy Baby Greedy Baby
- ME
- me
- ME
- Me
- ME
- me
- me
- me
- Me
- me
- Android 异步加载网络图片并缓存到本地 软引用 学习分享
- 今天立志成为黑马的一员
- C语言中sizeof 与strlen 区别
- 最长上升子序列的nlogn算法实现(用栈)
- 为什么选择java
- BNUOJ27888:Baby Me
- 字符串替换
- HEVC码流简单分析
- Ubuntu - 修改默认引导顺序
- linux-可重入与不可重入函数
- uva 11825 - Hackers' Crackdown
- Linux Directory Structure and Important Files Paths Explained
- uva 10891 - Game of Sum
- 一维数组中最长递增子序列的长度(DP做法)