HDU 3573(详解+思路+代码)
Description
Imyourgod need 3 kinds of sticks(樹枝)?which have different sizes: 20cm, 28cm and 32cm. However the shop only sell 75-centimeter-long sticks. So he have to cut off the long stick. How many sticks he must buy at least.
Input
The first line of input contains a number t, which means there are t cases of the test data.?
There will be several test cases in the problem, each in one line. Each test cases are described by 3 non-negtive-integers separated by one space representing the number of sticks of 20cm, 28cm and 32cm. All numbers are less than 10^6.
Output
The output contains one line for each line in the input case. This line contains the minimal number of 75-centimeter-long sticks he must buy. Format are shown as Sample Output.
Sample Input
2
3 1 1
4 2 2
Sample Output
Case 1: 2
Case 2: 3
題意:XX需要3種不同尺寸的樹枝:20cm、28cm、和32cm。然而這商店僅僅銷售75厘米長的樹枝。
因此他不得不砍掉長得樹枝。問至少需要買多少樹枝?
輸入:
第一行包含一個數字t,表示t組測試數據,多組測試數據,每組測試數據占一行,每組通過
三個用一個空格間隔的非負數分別代表20cm、28cm和32cm樹枝的數量
輸出:
輸出占用一行,輸出至少買76cm長數的數量
思路:容易出錯的地方就是樹是可以切的但是并不能連接,好比75切成20 28cm那么剩下的22cm是沒有用的只能棄掉,因此有以下幾種情況:
1、一根76cm全切成20cm或者28cm最多3根
2、一根76cm全切成32cm最多兩根
3、一根76cm切成任意兩種樹枝,但出現三種類型的樹都有則至少需要兩根
因此我們用a b c 表示三種情況的樹枝的數量:
利用模擬法挨個遞減可做,但是有優先級需要考慮。
代碼:
#include<iostream> #include<algorithm> #include<cstdio> using namespace std;int main() {int t;cin>>t;for(int i=1;i<=t;i++){__int64 a,b,c;__int64 sum;int ans=0;cin>>a>>b>>c;while(a>=2&&c>=1){ //72a-=2;c--;ans++;}while(a>=2&&b>=1){ //68a-=2;b--;ans++;}while(a>=3){ //68a-=3;ans++;}while(c>=2){ //64c-=2;ans++;}while(b>=1&&c>=1){ //60b--;c--;ans++;}while(b>=2){ //56b-=2;ans++;}if(a||b||c)ans++;printf("Case %d: %d\n",i,ans);}return 0; }總結
以上是生活随笔為你收集整理的HDU 3573(详解+思路+代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mysql for linux 安装
- 下一篇: 饮料换购(蓝桥杯)