动态规划的用法——01背包问题
最后更新于:2022-04-01 20:19:56
问题主题:著名的01背包问题 |
问题描述: 有n个重量和价值分别为wi、vi的物品,现在要从这些物品中选出总重量不超过W的物品,求所有挑选方案中的价值最大值。 限制条件: 1<=N<=100 1<=wi 、vi<=100 1<=wi<=10000 |
样例: 输入 N=4 W[N] = {2, 1, 3, 2} V[N] = {3, 2, 4, 2} 输出 W = 5(选择0,1,3号) |
#include <stdio.h> #include <tchar.h> #include <queue> #include "iostream" using namespace std; const int N = 4; const int W = 5; int weight[N] = {2, 1, 3, 2}; int value[N] = {3, 2, 4, 2}; int solve(int i, int residue) { int result = 0; if(i >= N) return result; if(weight[i] > residue) result = solve(i+1, residue); else { result = max(solve(i+1, residue), solve(i+1, residue-weight[i]) + value[i]); } } int main() { int result = solve(0, W); cout << result << endl; return 0; } |
#include <stdio.h> #include <tchar.h> #include <queue> #include "iostream" using namespace std; const int N = 4; const int W = 5; int weight[N] = {2, 1, 3, 2}; int value[N] = {3, 2, 4, 2}; int record[N][W]; void init() { for(int i = 0; i < N; i ++) { for(int j = 0; j < W; j ++) { record[i][j] = -1; } } } int solve(int i, int residue) { if(-1 != record[i][residue]) return record[i][residue]; int result = 0; if(i >= N) return result; if(weight[i] > residue) { record[i + 1][residue] = solve(i+1, residue); } else { result = max(solve(i+1, residue), solve(i+1, residue-weight[i]) + value[i]); } return record[i + 1][residue] = result; } int main() { init(); int result = solve(0, W); cout << result << endl; return 0; } |
package greed; /** * User: luoweifu * Date: 14-1-21 * Time: 下午5:13 */ public class Knapsack { private int maxWeight; private int[][] record; private Stuff[] stuffs; public Knapsack(Stuff[] stuffs, int maxWeight) { this.stuffs = stuffs; this.maxWeight = maxWeight; int n = stuffs.length + 1; int m = maxWeight+1; record = new int[n][m]; for(int i = 0; i < n; i ++) { for(int j = 0; j < m; j ++) { record[i][j] = -1; } } } public int solve(int i, int residue) { if(record[i][residue] > 0) { return record[i][residue]; } int result; if(i >= stuffs.length) { return 0; } if(stuffs[i].getWeight() > residue) { result = solve(i + 1, residue); } else { result = Math.max(solve(i + 1, residue), solve(i + 1, residue - stuffs[i].getWeight()) + stuffs[i].getValue()); } record[i][residue] = result; return result; } public static void main(String args[]) { Stuff stuffs[] = { new Stuff(2, 3), new Stuff(1, 2), new Stuff(3, 4), new Stuff(2, 2) }; Knapsack knapsack = new Knapsack(stuffs, 5); int result = knapsack.solve(0, 5); System.out.println(result); } } class Stuff{ private int weight; private int value; public Stuff(int weight, int value) { this.weight = weight; this.value = value; } int getWeight() { return weight; } void setWeight(int weight) { this.weight = weight; } int getValue() { return value; } void setValue(int value) { this.value = value; } } |
贪心算法——字典序最小问题
最后更新于:2022-04-01 20:19:53
问题主题:字典序最小 |
问题描述: 给定长度为N的字符串S,要构造一个长度为N字符串T。T是一个空串,反复执行下列任意操作: l 从S的头部删除一个字符,加到T的尾部; l 从S的尾部删除一个字符,加到T的尾部; 目标是要构造字典序尽可能小的字符串T。 限制条件: 1<=N<=2000 字符串都在大写字符 |
样例: 输入 N=8 ADHCACBD 输出 ADBCACDH |
#include <stdio.h> #include <tchar.h> #include <queue> #include "iostream" using namespace std; const int N = 8; char chs[N+1] = "ADHCACBD"; char* solve(char chs[]) { int start = 0, end = N - 1; bool isLeft = false; char dest[N]; while(start <= end) { for(int i = 0; start + i < end; i ++) { if(chs[start + i] < chs[end - i]) { isLeft = true; break; } else if(chs[start + i] > chs[end -i]) { isLeft = false; break; } else continue; } if(isLeft) { dest[N-(end - start + 1)] = chs[start ++]; //putchar(chs[start ++]); } else { dest[N-(end - start + 1)] = chs[end --]; //putchar(chs[end --]); } } return dest; } int main() { char *result = solve(chs); for(int i=0; i<N; i++) { putchar(result[i]); } return 0; }
package greed; /** * User: luoweifu * Date: 14-1-20 * Time: 上午9:41 */ public class BestCowLine { public static String cowLine(String s) { char[] chs = new char[s.length()]; char[] dest = new char[s.length()]; s.getChars(0, s.length(), chs, 0); int start = 0, end = s.length() - 1; while(start <= end) { boolean isLeft = false; for(int i=0; i <= end - start; i++) { if(chs[start + i] < chs[end - i]) { isLeft = true; break; } else if(chs[start + i] > chs[end - i]) { isLeft = false; break; } } if(isLeft) { dest[s.length() - (end - start + 1)] = chs[start ++]; } else { dest[s.length() - (end - start + 1)] = chs[end --]; } } return new String(dest); } public static void main(String args[]) { System.out.println(cowLine("ADHCACBD")); } } |
贪心算法——区间调度问题
最后更新于:2022-04-01 20:19:51
问题主题:区间调度问题 |
问题描述: 有n项工作,每项工作分别在si开始,ti结束。对每项工作,你都可以选择参加或不参加,但选择了参加某项工作就必须至始至终参加全程参与,即参与工作的时间段不能有重叠(即使开始的时间和结束的时间重叠都不行)。 限制条件: 1<=n<=100000 1<=si<=ti,=109 |
样例: 输入 n=5 s={1,2,4,6,8} T={3,5,7,9,10} 输出 3(选择工作1, 3, 5) |
#include <stdio.h> #include <tchar.h> #include <queue> #include "iostream"
using namespace std;
const int N = 5; int s[N]={1,2,4,6,8}; int t[N]={3,5,7,9,10};
int solve() { pair<int, int> itv[N]; for(int i = 0; i < N; i ++) { itv[i].first = s[i]; itv[i].second = t[i]; } sort(itv, itv + N); int count = 0; int t = 0; for(int i = 0; i < N; i ++) { if(t < itv[i].first) { count ++; t = itv[i].second; } } return count; }
int main() { cout << solve() << endl; return 0; } |
package greed; import java.util.Arrays; /** * Created with IntelliJ IDEA. * User: shihuichao * Date: 14-1-14 * Time: 下午10:43 * To change this template use File | Settings | File Templates. */ public class Interval { public static int interval(Work[] works) { Arrays.sort(works); int count = 0; //当前工作的结束时间 int t = 0; for (int i = 0; i < works.length; i++) { if(t < works[i].getStart()) { count ++; t = works[i].getTerminate(); } } return count; } public static void main(String args[]) { Work[] works = { new Work(1, 3), new Work(2, 5), new Work(4, 7), new Work(6, 9), new Work(8, 10) }; int result = interval(works); System.out.println(result); } } class Work implements Comparable { private int start; private int terminate; Work(int start, int terminate) { this.start = start; this.terminate = terminate; } int getStart() { return start; } void setStart(int start) { this.start = start; } int getTerminate() { return terminate; } void setTerminate(int terminate) { this.terminate = terminate; } @Override public int compareTo(Object o) { Work work = (Work) o; if (this.terminate > work.getTerminate()) return 1; else if (this.terminate == work.getTerminate()) return 0; else return -1; } } |
贪心算法——找纸币问题
最后更新于:2022-04-01 20:19:49
问题主题:找钱 |
问题描述: 假设有1元、2元、5元、10元、20元、50元、100的纸币分别为c0, c1, c2, c3, c4, c5, c6,张。现在要用这些钱来支付K元,至少要用多少张纸币?如果能找,则输出纸币的张数,不能找则输出No 限制条件: 0<=c0, c1,c2,c3,c4,c5,c6<=109 0<=K<=109 |
样例: 输入 C0=3, c1=0, c2=2, c3=1, c4=0, c5=3, c6=5 输出 6 |
#include "iostream"
using namespace std;
const int N = 7; static int K = 6200; int min(int num1, int num2);
int momeyCount[N] = {3, 0, 2, 1, 0, 3, 5}; int value[N] = {1, 2, 5, 10, 20, 50, 100};
int giveChange() { int num = 0; for(int i = N-1; i >= 0; i --) { int c = min(K/value[i], momeyCount[i]); K = K - c * value[i]; num += c; } if(K > 0) num = -1; return num; }
int min(int num1, int num2) { return num1 < num2 ? num1 : num2; }
int main() { int result = giveChange(); if(result != -1) cout << result << endl; else cout << "NO" << endl; return 0; }
|
深度优先搜索的用法——lake counting
最后更新于:2022-04-01 20:19:47
问题主题:Lake Counting |
问题描述: 有一个大小为N*M的园子,雨后积了很多水。八连通的积水被认为是在一起的。请求出园子里共有多少个水洼?(八连通是指下图中相对+的*部分) +++ +*+ +++ 限制条件: N,M <= 100 |
样例: 输入 N=10, M=12 园子如下图(‘+’表示积水,’*’表示没有积水) +****++* *+++***+++ **++***++* *****++* *****+** **+****+** *+*+***++* +*+*+***+* *+*+****+* **+*****+* 输出 3 |
#include "iostream"
using namespace std;
const int N = 10; const int M = 12;
char garden[N][M+1] = { "+****++*", "*+++***+++", "**++***++*", "*****++*", "*****+**", "**+****+**", "*+*+***++*", "+*+*+***+*", "*+*+****+*", "**+*****+*" };
void dfs(int x, int y);
void solve() { int count = 0; for(int j=0; j<N; j++) { for(int i=0; i<M; i++){ if(garden[j][i] == '+') { dfs(j, i); count ++; } } } cout << count << endl; }
void dfs(int y, int x) { garden[y][x] = '*'; for(int dy=y-1; dy<=y+1; dy++) { for(int dx=x-1; dx<=x+1; dx++) { if(dx >=0 && dy >= 0 && dx < M && dy < N && garden[dy][dx] == '+') { dfs(dy, dx); } } } }
int main() { solve(); return 0; }
|
/** * User: luoweifu * Date: 14-1-7 * Time: 下午4:53 */ public class LakeCounting { public static final int N = 10; public static final int M = 12; private String garden[] = { "+****++*", "*+++***+++", "**++***++*", "*****++*", "*****+**", "**+****+**", "*+*+***++*", "+*+*+***+*", "*+*+****+*", "**+*****+*" }; public void solve() { int count = 0; for(int j=0; j<N; j++) { for(int i=0; i<M; i++){ if(garden[j].charAt(i) == '+') { dfs(j, i); count ++; } } } System.out.println(count); } public void dfs(int y, int x) { StringBuilder stringY = new StringBuilder(garden[y]); stringY.setCharAt(x, '*'); garden[y] = stringY.toString(); for(int dy=y-1; dy<=y+1; dy++) { for(int dx=x-1; dx<=x+1; dx++) { if(dx >=0 && dy >= 0 && dx < M && dy < N && garden[dy].charAt(dx) == '+') { dfs(dy, dx); } } } } public static void main(String args[]) { LakeCounting lakeCounting = new LakeCounting(); lakeCounting.solve(); } } |
深度优先搜索的用法——求数组部分和
最后更新于:2022-04-01 20:19:44
问题主题:求数组部分和 |
问题描述: 给定整数a1,a2, … an,判断能否从中选出若干个数,使得它们的和为k。 限制条件: 1<=n<=20 -108<=ai<=108 -108<=k<=108 |
样例: 输入 n=4 a={1,2,4,7} k=13 输出 Yes (13=2+4+7) 输入 n=4 a={1,2,4,7} k=15 输出 No |
#include "stdafx.h" #include "iostream"
using namespace std;
const int MAX = 10; int result; int a[MAX]; int sum = 0;
bool resolve(int a[], int n, int i, int sum) ;
int _tmain(int argc, _TCHAR* argv[]) { int n; cout << "请?输º?入¨?n和¨ª结¨¢果?的Ì?大䨮小?:" <<endl; cin >> n >> result; cout << "请?输º?入¨?n个?数ºy:" <<endl; for(int i=0; i<n; i++) { cin >> a[i]; } if(resolve(a, n, 0, sum)) cout << "Yes" <<endl; else cout << "No" << endl; return 0; }
bool resolve(int a[], int n, int i, int sum) { if(i >= n) return sum == result; //不?选?a[i] if(resolve(a, n, i+1, sum)) return true; //加¨®上¦?a[i] if(resolve(a, n, i+1, sum+a[i])) return true; return false; }
|
/** * User: luoweifu * Date: 14-1-7 * Time: 上午11:33 */ public class PartSum { private int array[]; private int result; public PartSum() { } public PartSum(int[] array, int result) { this.array = array; this.result = result; } public void setArray(int[] array) { this.array = array; } public void setResult(int result) { this.result = result; } public boolean resolve(int[] array, int i, int sum) { int n = array.length; if(i >= n) return sum == result; if(resolve(array, i+1, sum)) return true; if(resolve(array, i+1, sum+array[i])) return true; return false; } public void partsumResolve() { if(resolve(array, 0, 0)) System.out.println("Yes"); else System.out.println("No"); } public static void main(String args[]) { PartSum partSum = new PartSum(new int[]{1,2,4,7}, 13); partSum.partsumResolve(); partSum.setResult(15); partSum.partsumResolve(); } } |
c++实现二分查找
最后更新于:2022-04-01 20:19:42
** 抽签问题 解决方案,复杂度n^4 */ void drawLots() { //从标准输入读入 int numOfCard, sum; int k[MAX_N]; cout<<"输入numOfCard和sum"<<endl; cin>>numOfCard>>sum; cout<<"请输入这sum张卡片的数字"<<endl; for(int i=0; i<numOfCard; i++) { cin>>k[i]; } bool result = false; bool isBreakLoop = true; int _sum = 0; for(int a = 0; a < numOfCard && isBreakLoop; a ++) { for(int b = 0; b < numOfCard && isBreakLoop; b ++) { for(int c = 0; c < numOfCard && isBreakLoop; c++) { for(int d = 0; d < numOfCard && isBreakLoop; d ++) { _sum = k[a] + k[b] + k[c] + k[d]; if(_sum == sum) { result = true; isBreakLoop = false; } } } } } cout << "_sum:" << _sum << " " << "sum:" << sum << endl; if(result){ cout<<"Yes"<<endl; } else cout<<"No"<<endl; } |
/** 抽签问题 解决方案,复杂度n^3 * log(n) */ void drawLots2() { int numOfCard, sum; int k[MAX_N]; cout<<"输入numOfCard和sum"<<endl; cin>>numOfCard>>sum; cout<<"请输入这sum张卡片的数字"<<endl; for(int i=0; i<numOfCard; i++) { cin>>k[i]; } //对数组进行排序 sort(k, k + numOfCard); int index = -1; bool isBreakLoop = true; for(int a = 0; a < numOfCard && isBreakLoop; a ++) { for(int b = 0; b < numOfCard && isBreakLoop; b ++) { for(int c = 0; c < numOfCard && isBreakLoop; c++) { index = binarySearch2(k, numOfCard, sum - (k[a] + k[b] + k[c])); if(index >= 0) { isBreakLoop = false; } } } } if(index >= 0){ cout<<"Yes"<<endl; } else cout<<"No"<<endl; } |
/** 抽签问题 解决方案,复杂度n^2 * log(n) */ void drawLots3() { int numOfCard, sum; int k[MAX_N]; cout<<"输入numOfCard和sum"<<endl; cin>>numOfCard>>sum; cout<<"请输入这sum张卡片的数字"<<endl; for(int i=0; i<numOfCard; i++) { cin>>k[i]; } int cdNum = numOfCard*(numOfCard+1)/2; int cdSum[cdNum]; int i = 0; for(int a=0; a<numOfCard; a++) { for(int b=i; b<numOfCard; b++) { cdSum[i ++] = k[a] + k[b]; } } //对数组进行排序 sort(cdSum, cdSum + cdNum); int index = -1; bool isBreakLoop = true; for(int a = 0; a < numOfCard && isBreakLoop; a ++) { for(int b = 0; b < numOfCard && isBreakLoop; b ++) { for(int c = 0; c < numOfCard && isBreakLoop; c++) { index = binarySearch2(cdSum, cdNum, sum - (k[a] + k[b])); if(index >= 0) { isBreakLoop = false; } } } } if(index >= 0){ cout<<"Yes"<<endl; } else cout<<"No"<<endl; } |
/** 抽签问题 解决方案,复杂度n^2 * log(n) */ void drawLots3_1() { int numOfCard, sum; int k[MAX_N]; cout<<"输入numOfCard和sum"<<endl; cin>>numOfCard>>sum; cout<<"请输入这sum张卡片的数字"<<endl; for(int i=0; i<numOfCard; i++) { cin>>k[i]; } int cdNum = numOfCard*numOfCard; int cdSum[cdNum]; for(int a=0; a<numOfCard; a++) { for(int b=0; b<numOfCard; b++) { cdSum[a*numOfCard + b] = k[a] + k[b]; } } //对数组进行排序 sort(cdSum, cdSum + cdNum); int index = -1; bool isBreakLoop = true; for(int a = 0; a < numOfCard && isBreakLoop; a ++) { for(int b = 0; b < numOfCard && isBreakLoop; b ++) { for(int c = 0; c < numOfCard && isBreakLoop; c++) { index = binarySearch2(cdSum, cdNum, sum - (k[a] + k[b])); if(index >= 0) { isBreakLoop = false; } } } } if(index >= 0){ cout<<"Yes"<<endl; } else cout<<"No"<<endl; } |
C++如何跳出多层循环
最后更新于:2022-04-01 20:19:40
问题主题:抽签 |
问题描述: 将写有数字的numOfCard个卡片放入口袋中,从口袋中抽取4次卡片,每次记录卡片的数字后将其放回口袋中。设卡片上写的数字为k1、k2、k3...kn,如果这4个数字的和为sum,则输出”Yes”,否则输出”No” 限制条件: 1<=numOfCard<=50 1<=sum<=108 1<=ki<=108 |
样例1: 输入 numOfCard = 3 Sum = 10 K = {1, 3, 5} 输出 Yes 样例2: 输入 numOfCard = 3 Sum = 9 K = {1, 3, 5} 输出 No |
void drawLots() { //从标准输入读入 int numOfCard, sum; int k[MAX_N]; cout<<"输入numOfCard和sum"<<endl; cin>>numOfCard>>sum; cout<<"请输入这sum张卡片的数字"<<endl; for(int i=0; i<numOfCard; i++) { cin>>k[i]; } bool result = false; int _sum = 0; for(int a = 0; a < numOfCard; a ++) { for(int b = 0; b < numOfCard; b ++) { for(int c = 0; c < numOfCard; c++) { for(int d = 0; d < numOfCard; d ++) { _sum = k[a] + k[b] + k[c] + k[d]; if(_sum == sum) { result = true; break; } } } } } if(result){ cout<<"Yes"<<endl; } else cout<<"No"<<endl; } |
void drawLots() { //从标准输入读入 int numOfCard, sum; int k[MAX_N]; cout<<"输入numOfCard和sum"<<endl; cin>>numOfCard>>sum; cout<<"请输入这sum张卡片的数字"<<endl; for(int i=0; i<numOfCard; i++) { cin>>k[i]; } bool result = false; int _sum = 0; for(int a = 0; a < numOfCard; a ++) { for(int b = 0; b < numOfCard; b ++) { for(int c = 0; c < numOfCard; c++) { for(int d = 0; d < numOfCard; d ++) { _sum = k[a] + k[b] + k[c] + k[d]; if(_sum == sum) { result = true; break; } } } } } cout << "_sum:" << _sum << " " << "sum:" << sum << endl; if(result){ cout<<"Yes"<<endl; } else cout<<"No"<<endl; } |
void drawLots() { //从标准输入读入 int numOfCard, sum; int k[MAX_N]; cout<<"输入numOfCard和sum"<<endl; cin>>numOfCard>>sum; cout<<"请输入这sum张卡片的数字"<<endl; for(int i=0; i<numOfCard; i++) { cin>>k[i]; } bool result = false; int _sum = 0; for(int a = 0; a < numOfCard; a ++) { for(int b = 0; b < numOfCard; b ++) { for(int c = 0; c < numOfCard; c++) { for(int d = 0; d < numOfCard; d ++) { _sum = k[a] + k[b] + k[c] + k[d]; if(_sum == sum) { result = true; goto breakLoop; } } } } } breakLoop: cout << "_sum:" << _sum << " " << "sum:" << sum << endl; if(result){ cout<<"Yes"<<endl; } else cout<<"No"<<endl; } |
void drawLots() { //从标准输入读入 int numOfCard, sum; int k[MAX_N]; cout<<"输入numOfCard和sum"<<endl; cin>>numOfCard>>sum; cout<<"请输入这sum张卡片的数字"<<endl; for(int i=0; i<numOfCard; i++) { cin>>k[i]; } bool result = false; bool isBreakLoop = true; int _sum = 0; for(int a = 0; a < numOfCard && isBreakLoop; a ++) { for(int b = 0; b < numOfCard && isBreakLoop; b ++) { for(int c = 0; c < numOfCard && isBreakLoop; c++) { for(int d = 0; d < numOfCard && isBreakLoop; d ++) { _sum = k[a] + k[b] + k[c] + k[d]; if(_sum == sum) { result = true; isBreakLoop = false; } } } } } cout << "_sum:" << _sum << " " << "sum:" << sum << endl; if(result){ cout<<"Yes"<<endl; } else cout<<"No"<<endl; } |
蚂蚁爬行问题
最后更新于:2022-04-01 20:19:37
问题主题:Ants(POJ No.1852) |
问题描述: n只蚂蚁以每秒1cm的速度在长为Lcm的竹竿上爬行。当蚂蚁看到竿子的端点时就会落下来。由于竿子太细,两只蚂蚁相遇时,它们不能交错通过,只能各自反方向爬行。对于每只蚂蚁,我们只知道它离竿子最左端的距离为xi,但不知道它当前的朝向。请计算所有蚂蚁落下竿子的最短时间和最长时间。
限制条件: 1<=L<=106 1<=n<=106 0<=xi<=L |
样例: 输入 L=10 n=3 x={2,6,7} 输出 min=4{左、右、右} max=8{右、右、右} |
#include <iostream> #include <algorithm>
const int L = 10; const int n = 3; const int x[n] = {2,6,7}; int main() { int min, max; min = max = 0; int minX, maxX; for(int i=0; i<n; i++) { minX = x[i]<L-x[i]?x[i]:L-x[i]; min = minX>=min?minX:min; maxX = x[i]>(L-x[i])?x[i]:L-x[i]; max= maxX>max ? maxX : max; } cout<<min<<" "<<max<<endl; return 0; } |
package programdesign;
public class Ants { public static void solve(int l, int x[]) { if(0 >= x.length) return; int min , max; min = max = 0; int d1, d2; for(int i=0; i<x.length; i++) { if(x[i]<=l-x[i]) { d1 = x[i]; d2 = l-x[i]; } else { d1 = l-x[i]; d2 = x[i]; } if(min < d1) { min = d1; } if(max < d2) { max = d2; } } System.out.println(min + " " + max); } public static void main(String[] args) { int x[] = {2,6,7}; solve(10, x); }
}
|
几个比较大的在线提交系统(Online Judge)
最后更新于:2022-04-01 20:19:35
从简单的三角形开始
最后更新于:2022-04-01 20:19:33
问题主题:三角形 |
问题描述: 有n根棍子,棍子i的长度为ai,想要从中选出三根棍子组成周长尽可能长的三角形。请输出最大的周长,若无法组成三角形则输出0。 |
样例: 输入 n=5 a={2,3,4,5,10} 输出 12(选择3,4,5时)
输入 n=4 a={4,5,10,20} 输出 0(无法构成三角形) |
public void contructTriangle(int a[]) { // 对数组a进行排序 Arrays.sort(a); int n = a.length; int a1, a2, a3; for (int i = n - 1; i >= 0; i--) { a1 = a[i]; for (int j = i - 1; j >= 0; j--) { a2 = a[j]; for (int k = j - 1; k >= 0; k--) { a3 = a[k]; if (a2 + a3 > a1) { System.out.println("最大" + (a1 + a2 + a3) + "三角形为" + a1 + "," + a2 + "," + a3 ); return ; } } } } System.out.println("0, 不能构成三角形"); } |
#include <iostream> #include <algorithm> void constriangle(int* a, int n) { int ans = 0; //答案 int len, ma, result; //让i<j<k,这样棍子就不会被重复选了 for(int i=0; i<n; i++){ for(int j=i+1; j<n; j++) { for(int k=j+1; k<n; k++) { len = a[i] + a[j] + a[k]; ma = max(a[i], max(a[j], a[k])); int rest = len-ma; if(ma <rest) { ans = max(ans, len); } } } } cout<<ans<<endl; } int main() { int a[5] = {2, 3, 4, 5, 10}; constriangle(a, 5); return 0; } |
前言
最后更新于:2022-04-01 20:19:31