日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于【贪心算法】

發布時間:2025/3/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于【贪心算法】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

FatMouse

題目描述:

FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.?

輸入:

The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.

輸出:

For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.

樣例輸入:
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
樣例輸出:
13.333 31.500

Code: #include <cstdio> #include <algorithm>using namespace std;struct goods{double weight; //該物品總重double totalValue; //該物品總價值double price; //該物品性價比 };bool cmp(goods a,goods b){return a.price>=b.price; }int main() {double n; //一共多少錢const int arrSize=1010;int m; //有幾樣物品 goods arr[arrSize];while(scanf("%lf%d",&n,&m)!=EOF){if(n==-1&&m==-1)break;for(int cnt=0;cnt<m;++cnt){scanf("%lf%lf",&arr[cnt].weight,&arr[cnt].totalValue);arr[cnt].price=arr[cnt].weight/arr[cnt].totalValue;}sort(arr,arr+m,cmp);double ans=0;int index=0;while(n>0&&index<m){if(n>arr[index].totalValue){ //若能買下全部該商品ans+=arr[index].weight;n-=arr[index].totalValue;}else{ans+=n/arr[index].totalValue*arr[index].weight;n=0;}++index;}printf("%.3lf\n",ans);}return 0; }/**************************************************************Problem: 1433User: lcyvinoLanguage: C++Result: AcceptedTime:10 msMemory:1020 kb ****************************************************************/

?

今年暑假不AC

題目描述:

“今年暑假不AC?”“是的。”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%...”確實如此,世界杯來了,球迷的節日也來了,估計很多ACMer也會拋開電腦,奔向電視作為球迷,一定想看盡量多的完整的比賽,當然,作為新時代的好青年,你一定還會看一些其它的節目,比如新聞聯播(永遠不要忘記關心國家大事)、非常6+7、超級女生,以及王小丫的《開心辭典》等等,假設你已經知道了所有你喜歡看的電視節目的轉播時間表,你會合理安排嗎?(目標是能看盡量多的完整節目)

輸入:

輸入數據包含多個測試實例,每個測試實例的第一行只有一個整數n(n<=100),表示你喜歡看的節目的總數,然后是n行數據,每行包括兩個數據Ti_s,Ti_e (1<=i<=n),分別表示第i個節目的開始和結束時間,為了簡化問題,每個時間都用一個正整數表示。n=0表示輸入結束,不做處理。

輸出:

對于每個測試實例,輸出能完整看到的電視節目的個數,每個測試實例的輸出占一行。

樣例輸入:
12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 0
樣例輸出:
5

Code: #include <cstdio> #include <algorithm>using namespace std;struct program{int startTime;int endTime; };bool cmp(program a,program b){return a.endTime<=b.endTime; }int main() {const int arrSize=110;program arr[arrSize];int n;while(scanf("%d",&n)!=EOF){if(n==0)break;for(int cnt=0;cnt<n;++cnt){scanf("%d%d",&arr[cnt].startTime,&arr[cnt].endTime);}sort(arr,arr+n,cmp);int currentTime=0;int ans=0;for(int index=0;index<n;++index){if(currentTime<=arr[index].startTime){currentTime=arr[index].endTime;++ans;}}printf("%d\n",ans);}return 0; }/**************************************************************Problem: 1434User: lcyvinoLanguage: C++Result: AcceptedTime:10 msMemory:1020 kb ****************************************************************/

?

迷瘴

題目描述:

通過懸崖的yifenfei,又面臨著幽谷的考驗——
幽谷周圍瘴氣彌漫,靜的可怕,隱約可見地上堆滿了骷髏。由于此處長年不見天日,導致空氣中布滿了毒素,一旦吸入體內,便會全身潰爛而死。
幸好yifenfei早有防備,提前備好了解藥材料(各種濃度的萬能藥水)。現在只需按照配置成不同比例的濃度。
現已知yifenfei隨身攜帶有n種濃度的萬能藥水,體積V都相同,濃度則分別為Pi%。并且知道,針對當時幽谷的瘴氣情況,只需選擇部分或者全部的萬能藥水,然后配置出濃度不大于 W%的藥水即可解毒。
現在的問題是:如何配置此藥,能得到最大體積的當前可用的解藥呢?
特別說明:由于幽谷內設備的限制,只允許把一種已有的藥全部混入另一種之中(即:不能出現對一種藥只取它的一部分這樣的操作)。

輸入:

輸入數據的第一行是一個整數C,表示測試數據的組數;
每組測試數據包含2行,首先一行給出三個正整數n,V,W(1<=n,V,W<=100);
接著一行是n個整數,表示n種藥水的濃度Pi%(1<=Pi<=100)。

輸出:

對于每組測試數據,請輸出一個整數和一個浮點數;
其中整數表示解藥的最大體積,浮點數表示解藥的濃度(四舍五入保留2位小數);
如果不能配出滿足要求的的解藥,則請輸出0 0.00。

樣例輸入:
3 1 100 10 100 2 100 24 20 30 3 100 24 20 20 30
樣例輸出:
0 0.00 100 0.20 300 0.23

Code: #include <cstdio> #include <algorithm>using namespace std;int main() {int C;int n,V,W;const int arrSize=100;int arr[arrSize];while(scanf("%d",&C)!=EOF){for(int cnt=0;cnt<C;++cnt){scanf("%d%d%d",&n,&V,&W);for(int j=0;j<n;++j)scanf("%d",&arr[j]);sort(arr,arr+n);int current_V=0;double current_W=0;for(int k=0;k<n;++k){if(((current_W*current_V+arr[k]*V)/(current_V+V))<=W){current_W=(current_W*current_V+arr[k]*V)/(current_V+V);current_V+=V;}}printf("%d %.2lf\n",current_V,current_W/100);}}return 0; }/**************************************************************Problem: 1435User: lcyvinoLanguage: C++Result: AcceptedTime:0 msMemory:1020 kb ****************************************************************/

?

轉載于:https://www.cnblogs.com/Murcielago/p/4188271.html

總結

以上是生活随笔為你收集整理的关于【贪心算法】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。