KS1. 获得最多的奖金
描述
输入描述
第一行包含一个正整数n,表示有多少个红包。输出描述
小明可以拿到的总奖金示例1
输入:
5 1 3 1 1 4
输出:
5
说明:
[1,3,1] [ ] [1,4] ,其中第一组奖金和是5,等于第三组奖金和。所以小明可以拿到5越南盾示例2
输入:
5 1 3 2 1 4
输出:
4
说明:
[1,3] [2,1] [4],小明可以拿到4越南盾示例3
输入:
3 4 1 2
输出:
0
说明:
[ ] [4, 1, 2] [ ] ,小明没办法,为了保证第一组第三组相等,只能都分成空的。所以小明只能拿到0越南盾。Rust 解法, 执行用时: 13ms, 内存消耗: 3376KB, 提交时间: 2022-05-10
use std::io; fn main() { let mut input = String::new(); io::stdin().read_line(&mut input); input.clear(); io::stdin().read_line(&mut input); let nums = input.trim().split_ascii_whitespace().map(|num| num.parse::<i32>().unwrap()).collect::<Vec<i32>>(); let (mut l, mut r) = (0, nums.len() - 1); let (mut suml, mut sumr) = (nums[l] as i64, nums[r] as i64); let mut res = 0i64; while l < r { if suml == sumr { res = suml; l += 1; r -= 1; suml += nums[l] as i64; sumr += nums[r] as i64; } else if suml > sumr { r -= 1; sumr += nums[r] as i64; } else { l += 1; suml += nums[l] as i64; } } println!("{}", res); }
Go 解法, 执行用时: 16ms, 内存消耗: 10648KB, 提交时间: 2020-12-28
package main import ( "bufio" "fmt" "os" "strconv" "strings" ) func main() { reader := bufio.NewReader(os.Stdin) // 读一行,去掉最后的换行后按照空格分割 str, _ := reader.ReadString('\n') str = str[:len(str)-1] strs := strings.Split(str, " ") // 将strs中的字符串转为整数后传入 n, _ := strconv.Atoi(strs[0]) arr := make([]int, n) str, _ = reader.ReadString('\n') str = str[:len(str)-1] strs = strings.Split(str, " ") for i := 0; i < n; i++ { arr[i], _ = strconv.Atoi(strs[i]) } fmt.Println(maxBoun(arr, n)) } func maxBoun(arr []int, n int) int { if n == 1 { return 0 } else if n == 2 { if arr[0] == arr[1] { return arr[0] } else { return 0 } } res := 0 l, r, curl, curr := 0, n-1, 0, 0 for l <= r { if curl == curr { res = curl curl += arr[l] curr += arr[r] l++ r-- } else if curl < curr { curl += arr[l] l++ } else { curr += arr[r] r-- } } if curl == curr { res = curl } return res }
C++ 解法, 执行用时: 19ms, 内存消耗: 2040KB, 提交时间: 2020-10-29
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin>>n; vector<int> num; int tmp; for(int i=0;i<n;i++){ cin>>tmp; num.push_back(tmp); } int i=0,j=n-1; long long int sum1=0,sum3=0,maxnum=0; while(i<=j){ if(sum1 < sum3){ sum1 += num[i]; i++; }else if (sum1 > sum3){ sum3 += num[j]; j--; } if(sum1==sum3){ maxnum = sum1; if(i<j){ sum1 += num[i]; i++; } } //cout<<i<<" "<<j<<" "<<sum1<<" "<<sum3<<endl; } cout<<maxnum; return 0; }
C++ 解法, 执行用时: 20ms, 内存消耗: 3308KB, 提交时间: 2020-08-13
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin>>n; vector<int> num; int tmp; for(int i=0;i<n;i++){ cin>>tmp; num.push_back(tmp); } int i=0,j=n-1; long long int sum1=0,sum3=0,maxnum=0; while(i<=j){ if(sum1 < sum3){ sum1 += num[i]; i++; }else if (sum1 > sum3){ sum3 += num[j]; j--; } if(sum1==sum3){ maxnum = sum1; if(i<j){ sum1 += num[i]; i++; } } //cout<<i<<" "<<j<<" "<<sum1<<" "<<sum3<<endl; } cout<<maxnum; return 0; }
C++ 解法, 执行用时: 20ms, 内存消耗: 3392KB, 提交时间: 2020-10-29
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin>>n; vector<int> num; int tmp; for(int i=0;i<n;i++){ cin>>tmp; num.push_back(tmp); } int i=0,j=n-1; long long int sum1=0,sum3=0,maxnum=0; while(i<=j){ if(sum1 < sum3){ sum1 += num[i]; i++; }else if (sum1 > sum3){ sum3 += num[j]; j--; } if(sum1==sum3){ maxnum = sum1; if(i<j){ sum1 += num[i]; i++; } } //cout<<i<<" "<<j<<" "<<sum1<<" "<<sum3<<endl; } cout<<maxnum; return 0; }