TCHS-5-250

Problem Statement

     When shopping for TVs consumers may notice that a TV's size is given by its diagonal in inches. The aspect ratio of a screen is the ratio of its width to its height. Given the aspect ratio and the diagonal size in inches of a TV screen, you must compute its width and height in inches (see the notes and the first example for more information). If the width or the height is not an integer, round down to the nearest integer; for example, 1.7 would become 1.

You are tasked with writing a method which accepts 3 arguments: an int diagonal specifying the TV's diagonal in inches, an int height specifying the aspect ratio height, and an int width specifying the aspect ratio width. Your program should compute the actual height and width of the TV in inches and return them in a int[] where the first element is the TV's height and the second element is the TV's width.

Definition

    
Class: TVSize
Method: calcSize
Parameters: int, int, int
Returns: int[]
Method signature: int[] calcSize(int diagonal, int height, int width)
(be sure your method is public)
    
 

Notes

- Let W denote the width, H denote the height and D denote the diagonal of the screen. Then W*W + H*H = D*D and W * height = H * width.

Constraints

- diagonal will be between 5 and 1000, inclusive.
- height will be between 1 and 99, inclusive.
- width will be between 2 and 100, inclusive.
- width will be greater than height.

Examples

0)  
    
52
 
9
 
16
 
Returns: {25, 45 }
 
W = (width/height) * H
 D*D = W*W + H*H 
 52^2 = (width/height)^2 * H^2 + H^2 
 52^2 = (16/9)^2 * H^2 + H^2 
 H = 25.49 
 W = 45.32 
 
1)  
    
7
 
2
 
3
 
Returns: {3, 5 }
 
 
2)  
    
13
 
7
 
10
 
Returns: {7, 10 }
 
 
3)  
    
7
 
32
 
47
 
Returns: {3, 5 }
 
 
4)  
    
11
 
15
 
16
 
Returns: {7, 8 }
 
 
 
public class TVSize {
	public int[] calcSize(int d, int h, int w) {
		double W = d * w / Math.sqrt(h * h + w * w);   
        double H = W * h / w;   
        return new int[] {(int)H, (int)W};
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值