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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

/ 卡路里_最大卡路里

發布時間:2025/3/11 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 / 卡路里_最大卡路里 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/ 卡路里

Problem statement:

問題陳述:

Shivang is very foodie but he has a diet plan. He has an array of elements indicating the calorie of food he can consume on that day. In his diet plan, he can’t eat on for three consecutive days. But since Shivang is very foodie, so he decided to take as much as calorie while remembering the fact that he cannot eat on three consecutive days. Help him in finding the number of calories he could take.

Shivang是位美食家,但他有飲食計劃。 他具有一系列元素,這些元素指示他當天可以吃的食物的熱量。 在他的飲食計劃中,他連續三天不能進食。 但是由于Shivang是一位非常美食的人,因此他決定攝取盡可能多的卡路里,同時記住自己連續三天不能進食的事實。 幫助他找到可以攝取的卡路里數量。

Input:

輸入:

n i.e number of days. In the next line is n space-separated values of ai indicating the intake of calorie per day.

n即天數。 在下一行中, i的 n個以空格分隔的值表示每天的卡路里攝入量。

Output:

輸出:

Print the maximum amount of calorie, Shivang can take in a new line.

打印最大卡路里,Shivang可以換行。

Constraints:

限制條件:

1<=n<=100000 1<=ai<=100000

Example:

例:

Input: 2 n=5 Array input, calorie for each day: 3 2 1 10 3Output: 18Explanation: Shivang will eat on 1st, 2nd, 4th and 5th day So, total he intakes 18 which is maximumInput: 8 3 2 3 2 3 5 1 3 Output: 17Explanation: Shivang will eat on 1st, 3rd, 5th , 6th and 8th day which totals (3+3+3+5+3) = 17

Solution Approach:

解決方法:

Let say,

說,

f(n) = maximum calorie can be taken till nth day under the constraint

No, let's think of the case,

不,讓我們考慮一下情況,

  • If Shivang takes food on ith day, he need to skip on (i-1)th day and sum with f(i-2)

    如果Shivang在 i 一天需要吃的,他需要跳過(I-1) 天,和與F(I-2)

  • Tale food on ith day, (i-1)th day and skip on (i-2)th day, sum up with f(i-3)

    i上故事食品天, 第(i-1) 天,并跳過對第(i-2) 天,總結和f(I-3)

  • Skip ith day, so take f(i-1)

    跳過 i 天,所以采取F(I-1)

  • So,

    所以,

    f(i)=maximum(f(i-1),f(i-2)+arr[i],arr[i]+arr[i-1]+f(i-3) i>=3For base case, f(0)=arr[0] f(1)=arr[0]+arr[1] f(2)=maximum(arr[0]+arr[1],arr[1]+arr[2],arr[2]+arr[0])

    Converting to Dynamic programming

    轉換為動態編程

    Now, we need to convert the above recursion into dynamic programming to avoid the overlapping sub-problem re-computation

    現在,我們需要將上述遞歸轉換為動態編程,以避免重疊的子問題重新計算

    1) Declare int DP[n]; 2) Fill with the base caseDP[0]=arr[0];DP[1]=arr[0]+arr[1];DP[2]=std::max(arr[1]+arr[2],std::max(arr[0]+arr[2],DP[1])); 3) Compute the other valuesfor i=3 to n-1DP[i]=maximum(arr[i]+arr[i-1]+DP[i-3],arr[i]+DP[i-2],DP[i-1]);end for 4) Return the result, DP[n-1];

    C++ Implementation:

    C ++實現:

    #include <bits/stdc++.h> using namespace std;int maximumCalorie(vector<int> arr, int n) {int DP[n];// base caseDP[0] = arr[0];DP[1] = arr[0] + arr[1];DP[2] = std::max(arr[1] + arr[2], std::max(arr[0] + arr[2], DP[1]));for (int i = 3; i < n; i++) {DP[i] = std::max(arr[i] + arr[i - 1] + DP[i - 3], std::max(arr[i] + DP[i - 2], DP[i - 1]));}return DP[n - 1]; }int main() {int t, n, item;cout << "Enter number of days\n";cin >> n;cout << "Enter calories\n";vector<int> a;for (int j = 0; j < n; j++) {scanf("%d", &item);a.push_back(item);}cout << "Maximum calorie sudhir can take: " << maximumCalorie(a, n) << endl;return 0; }

    Output:

    輸出:

    Enter number of days: 8 Enter calories: 3 2 3 2 3 5 1 3 Maximum calorie sudhir can take: 17

    翻譯自: https://www.includehelp.com/icp/maximum-calorie.aspx

    / 卡路里

    總結

    以上是生活随笔為你收集整理的/ 卡路里_最大卡路里的全部內容,希望文章能夠幫你解決所遇到的問題。

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

    主站蜘蛛池模板: 国产女人与zoxxxx另类 | 亚洲乱码视频在线观看 | 亚洲女人久久久 | 国产一级自拍视频 | 精品人妻无码在线 | 绿帽h啪肉np辣文 | 欧美在线视频一区二区 | 亚洲自拍第三页 | 亚洲字幕| 日韩欧美二区三区 | 强睡邻居人妻中文字幕 | 成人h网站 | 99产精品成人啪免费网站 | 成人精品免费 | 爱就操| 欧美丰满bbw| 国产成人精品免费看视频 | 超碰人人国产 | 中文字幕一区二区三 | 一区二区三区日韩精品 | 国产91清纯白嫩初高中在线观看 | 萌白酱喷水视频 | 欧美久久综合网 | 99热最新在线 | av在线不卡免费 | 国产黄色一区二区 | 天天综合天天做天天综合 | 性色av一区二区 | 色狠狠操| 午夜精品网站 | 人人干网站| 免费一级片视频 | av大帝| 日韩福利视频网 | 国产欧美视频一区二区三区 | 成人公开视频 | 少妇一级淫片 | 中文字幕av日韩 | 人人爽视频 | 久久久夜色精品 | 中文字幕亚洲欧美 | 人人妻人人澡人人爽欧美一区双 | 精品久久无码视频 | 成人听书哪个软件好 | 国产色婷婷一区二区三区竹菊影视 | 亚洲人成亚洲人成在线观看 | 国产精品女同一区二区 | 手机在线免费av | 亚洲视频欧洲视频 | 亚洲成网站| 青娱乐极品视频在线 | 国产免费一区二区三区视频 | 亚洲一二三区视频 | 国产精品一区二区入口九绯色 | 91久久影院 | 99re这里只有精品在线观看 | 六月丁香婷婷激情 | 色综合中文 | 亚洲国产成人精品一区二区三区 | 欧美老女人性生活视频 | 99re在线观看视频 | 美女xx00 | 欧美一区视频在线 | 成人在线观看网 | 快色在线观看 | 人妻少妇被粗大爽9797pw | 欧美怡红院视频一区二区三区 | 强行无套内谢大学生初次 | 真人抽搐一进一出视频 | 中出视频在线观看 | 最新黄色av网站 | 性色av浪潮| 91九色偷拍 | 好吊色这里只有精品 | 国产成年网站 | 国产欧美综合一区二区三区 | 无码人妻一区二区三区在线 | 日本系列第一页 | 17草在线 | 久久精品99久久久久久久久 | 久久久久91 | 亚洲交性网 | 精品在线视频免费 | 亚洲熟妇一区二区 | 欧美性白人极品1819hd | 国产成人精品一区二区 | 电影《两个尼姑》免费播放 | 男人天堂2020 | 黑色丝袜吻戏亲胸摸腿 | 一起草av在线 | 国产女人叫床高潮大片免费 | 亚洲美女性视频 | 中国av毛片 | 美女性高潮视频 | 免费看国产曰批40分钟 | 以女性视角写的高h爽文 | 国产免费午夜 | 国模吧无码一区二区三区 | 亚洲中文在线一区 |