SRM675(div2) Level One LengthUnitCalculator

SRM675(div2) Level One LengthUnitCalculator


   Problem Statement  

 Problem Statement for LengthUnitCalculator

Problem Statement

  Your task is to write a calculator that will convert between four different length units: inches (in), feet (ft), yards (yd), and miles (mi).



The conversions between these units:
  • 1 ft = 12 in
  • 1 yd = 3 ft
  • 1 mi = 1760 yd
You are given an int amount and Strings fromUnit and toUnit. Compute and return the amount of toUnits that corresponds to amountfromUnits. (For example, if amount=41, fromUnit="mi", and toUnit="in", you are supposed to compute the number of inches in 41 miles.) 

Note that the result will not necessarily be an integer.
 

Definition

 
Class: LengthUnitCalculator
Method: calc
Parameters: int, String, String
Returns: double
Method signature: double calc(int amount, String fromUnit, String toUnit)
(be sure your method is public)
 
 
 

Notes

- Pay attention to the unusual time limit.
 

Constraints

- amount will be between 1 and 1,000, inclusive.
- fromUnit will be one of {"in", "ft", "yd", "mi"}.
- toUnit will be one of {"in", "ft", "yd", "mi"}.
 

Examples

0)  
 
1
"mi"
"ft"
Returns: 5280.0
We are asked to convert 1 mile into feet. From the information in the statement we know that 1 mi = 1760 yd = (1760 * 3) ft = 5280 ft.
1)  
 
1
"ft"
"mi"
Returns: 1.893939393939394E-4
Here we have 1 ft = 1/5280 mi, which is approximately 0.000189394 miles.
2)  
 
123
"ft"
"yd"
Returns: 41.0

3)  
 
1000
"mi"
"in"
Returns: 6.336E7

4)  
 
1
"in"
"mi"
Returns: 1.5782828282828283E-5

5)  
 
47
"mi"
"mi"
Returns: 47.0

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.

This problem was used for: 
       Single Round Match 675 Round 1 - Division II, Level One

题意:单位转换

直接模拟,注意不要搞错了

#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
using namespace std;

string str[4] = {"mi", "yd", "ft", "in"};


int a[4] = {1, 1760, 3, 12};

class LengthUnitCalculator
{
    public:
    double calc(int amount, string fromUnit, string toUnit)
    {
        int i;
        double ans = amount;
        for(i = 0; i < 4; i++) if(str[i] == fromUnit) break;
        int l = i;
        for(i = 0; i < 4; i++) if(str[i] == toUnit) break;
        int r = i;
        if(l == r) return ans;
        if(l < r)
        {
            for(i = l+1; i <= r; i++)
            {
                ans *= a[i];
            }
        }
        else
        {
            for(i = r+1; i <= l; i++)
            {
                ans /= a[i];
            }
        }
        return ans;
    }
};
      
      
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值