坑题
題目描述
小姐姐想要配?n?把鑰匙,她走過開鎖鋪問老板價(jià)錢,老板是這么說的:
小姐姐想知道完成配n把鑰匙的任務(wù)最少需要花費(fèi)多少錢
輸入描述:
題目有多組測(cè)試數(shù)據(jù)。第一行輸入一個(gè)整數(shù)T(組數(shù)少于100組),表示測(cè)試數(shù)據(jù)組數(shù),對(duì)于接下來每組測(cè)試數(shù)據(jù):
每組測(cè)試數(shù)據(jù)輸入只有一行,分別為4個(gè)整數(shù),表示n、m、A、B。
輸出描述:
對(duì)于每組測(cè)試數(shù)據(jù):在一行內(nèi)輸出一個(gè)整數(shù),表示答案?
示例1
輸入
1 5 2 2 3輸出
8
ac:
#include<bits/stdc++.h> #define ll long long using namespace std;int main() {int t;cin>>t;int n,m,a,b,c,d;while(t--){cin>>n>>m>>a>>b;c=n/m;d=n%m;if(a>b*1.0/m){cout<<c*b+min(d*a,b)<<endl;//可以多配鑰匙,坑}else cout<<a*n<<endl;}return 0; }2.院花最近在瀏覽中國的經(jīng)典數(shù)學(xué)著作,她看到了一個(gè)經(jīng)典的“雞兔同籠”問題
但她覺得每次要自己算結(jié)果,太麻煩了,于是她讓wmj同學(xué)做了一個(gè)程序?qū)iT解決這個(gè)問題
wmj同學(xué)很輕松地做成了
院花很感謝他,對(duì)他說道“謝謝你啊, 你真是個(gè)好人”
wmj同學(xué)開心地笑了。
Input
先輸入一個(gè)整數(shù)t(0<t<20),表示共有有t組測(cè)試數(shù)據(jù).
對(duì)于每組測(cè)試數(shù)據(jù),在一行給出兩個(gè)整數(shù)n,m(0<=n,m<=10^18),以一個(gè)空格分隔
其中n表示雞和兔頭的總數(shù),m表示雞和兔腳的總數(shù)
Output
對(duì)于每組測(cè)試數(shù)據(jù),在一行輸出兩個(gè)整數(shù)a,b,以一個(gè)空格分隔
其中a表示雞的數(shù)量,b表示兔的數(shù)量。
如果該情況無解,輸出一行“-1”。
Sample Input
3 2 6 1 1 20000000000000 60000000000000Sample Output
1 1 -1 10000000000000 10000000000000ac:
#include<bits/stdc++.h> #define inf 0x3f3f3f #define ll long long #define ls l,m,rt<<1 #define lr m+1,r,rt<<1|1 using namespace std;int main() {std::ios::sync_with_stdio(false);int t;cin>>t;ll n,m,x,y;while(t--){cin>>n>>m;x=(4*n-m)/2;y=n-x;if(x*2+4*y!=m)cout<<-1<<endl;else if(n<0||m<0)cout<<-1<<endl;else if(x<0||y<0) //公式題要想到負(fù)數(shù)的影響,負(fù)數(shù)無意義cout<<-1<<endl;//n=0,m=2 => x=-1,y=1(該結(jié)果無意義,但能過公式)else if(n==0&&m==0)cout<<0<<" "<<0<<endl;else if(n==0&&m>0)cout<<-1<<endl;else if(n>0&&m==0)cout<<-1<<endl;else if(m%2==1)cout<<-1<<endl;else cout<<x<<" "<<y<<endl;}return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/wangtao971115/p/10358263.html
總結(jié)
- 上一篇: 2018-2019-1 20165221
- 下一篇: C学习笔记-gdb