最大花费金额
标题:最大花费金额 | 时间限制:1秒 | 内存限制:32768K | 语言限制:不限
双十一众多商品进行打折销售,小明想购买自己心仪的一些物品,但由于受购买资金限制,所以他决定从众多心仪商品中购买三件,而且想尽可能的花完资金,现在请你设计一个程序帮助小明计算尽可能花费的最大资金数额。
import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); List<Integer> totalNumList = Arrays.stream(in.nextLine().split(",")) .map(Integer::parseInt) .sorted() .collect(Collectors.toList()); int r = Integer.parseInt(in.nextLine()); in.close(); int maxMoney = -1; for (int i = 0; i < totalNumList.size(); i++) { for (int j = 0; j < totalNumList.size()-1; j++) { for (int k = 0; k < totalNumList.size(); k++) { if(i != j && j != k && i != k){ int sum = totalNumList.get(i) + totalNumList.get(j) + totalNumList.get(k); if (sum <= r && sum > maxMoney){ maxMoney = sum; } } } } } System.out.println(maxMoney); } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll p = 998244353; int a[110]; int main() { string s; cin >> s; int now = 0, op = 0; for(int i = 0; i < s.size(); i++) { if(s[i] == ',') { a[++op] = now; now = 0; } else now = now * 10 + s[i] - '0'; } a[++op] = now; int n; cin >> n; int ans = -1; for(int i = 1; i <= op; i++) { for(int j = i + 1; j <= op; j++) { for(int k = j + 1; k <= op; k++) { if(a[i] + a[j] + a[k] <= n) ans = max(ans, a[i] + a[j] + a[k]); } } } cout << ans << endl; }//manfen
#include<bits/stdc++.h> using namespace std; int Buy(vector<int>& nums, int target) { int n = nums.size(); if(n<3) return -1; sort(nums.begin(),nums.end()); int left = 0, right = n-1, sum = 0; while(left+1<right) { while(left+1<right&&nums[left]+nums[left+1]+nums[right]>target) right--; for(int i=left+1;i<right;i++) { if(nums[i]+nums[left]+nums[right]<=target) sum = max(sum,nums[i]+nums[left]+nums[right]); } left++; } if(sum>0) return sum; else return -1; } int main(){ string input; int target; getline(cin,input); cin>>target; vector<int> nums; int num = 0; for(int i=0;i<=input.size();i++) { if(input[i]==','||i==input.size()) { nums.push_back(num); num = 0; } else num = num*10 + (input[i]-'0'); } cout<<Buy(nums,target)<<endl; return 0; }
package main import ( "bufio" "fmt" "os" "strconv" "strings" ) func main() { input := bufio.NewScanner(os.Stdin) input.Scan() n:= strings.Split(input.Text(), ",") input.Scan() totals:=input.Text() items:=make([]int,0,0) for idx:=range n { price,_:=strconv.Atoi(n[idx]) items = append(items,price) } dif := -1 total, _ :=strconv.Atoi(totals) for i:=0;i<len(items);i++ { for j:=0;j<len(items);j++ { for k:=0;k<len(items);k++ { if i!=j && j!=k && i!= k{ tmp := items[i] + items[j] + items[k] //fmt.Print(tmp," ") if tmp > dif && tmp <= total { dif = tmp } } } } } fmt.Println(dif) }