小明和朋友玩跳格子游戏,有 n 个连续格子组成的圆圈,每个格子有不同的分数,小朋友可以选择以任意格子起跳,但是不能跳连续的格子,不能回头跳,也不能超过一圈;
给定一个代表每个格子得分的非负整数数组,计算能够得到的最高分数。
给定一个数例,第一个格子和最后一个格子首尾相连,如: 2 3 2
输出能够得到的最高分,如: 3
2 3 2
3
只能跳3这个格子,因为第一个格子和第三个格子首尾相连
1 2 3 1
4
1 + 3 = 4
#include <stdio.h>
int max(int a, int b) {
return a > b ? a : b;
}
int rob(int nums[], int start, int end) {
int pre2 = 0, pre1 = 0;
for (int i = start; i <= end; i++) {
int cur = max(pre1, pre2 + nums[i]);
pre2 = pre1;
pre1 = cur;
}
return pre1;
}
int main() {
int nums[] = {1, 2, 3, 1}; // Example input
int size = sizeof(nums) / sizeof(nums[0]);
if (size == 1) {
printf("%d\n", nums[0]);
} else {
printf("%d\n", max(rob(nums, 0, size - 2), rob(nums, 1, size - 1)));
}
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int rob(vector<int>& nums, int start, int end) {
int pre2 = 0, pre1 = 0;
for (int i = start; i <= end; i++) {
int cur = max(pre1, pre2 + nums[i]);
pre2 = pre1;
pre1 = cur;
}
return pre1;
}
int main() {
string line;
getline(cin, line);
vector<int> nums;
istringstream iss(line);
int num;
while (iss >> num) {
nums.push_back(num);
}
if (nums.size() == 1) {
cout << nums[0] << endl;
} else {
cout << max(rob(nums, 0, nums.size() - 2), rob(nums, 1, nums.size() - 1)) << endl;
}
return 0;
}
package com.amoscloud.nowcoder.refactor.t0251_0260;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @Author: Amos
* @E-mail: amos@amoscloud.com
* @Date: 2023/7/22
* @Time: 23:04
* @Description:
*/
public class Main0251 {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
String[] input = scanner.nextLine().split(" ");
int[] nums = new int[input.length];
for (int i = 0; i < input.length; i++) {
nums[i] = Integer.parseInt(input[i]);
}
System.out.println(solution(nums));
}
}
public static int solution(int[] nums) {
if (nums.length == 1) return nums[0];
return Math.max(rob(nums, 0, nums.length - 2), rob(nums, 1, nums.length - 1));
}
private static int rob(int[] nums, int start, int end) {
int pre2 = 0, pre1 = 0;
for (int i = start; i <= end; i++) {
int cur = Math.max(pre1, pre2 + nums[i]);
pre2 = pre1;
pre1 = cur;
}
return pre1;
}
}
def rob(nums, start, end):
pre2, pre1 = 0, 0
for i in range(start, end + 1):
cur = max(pre1, pre2 + nums[i])
pre2 = pre1
pre1 = cur
return pre1
nums = list(map(int, input().split()))
if len(nums) == 1:
print(nums[0])
else:
print(max(rob(nums, 0, len(nums) - 2), rob(nums, 1, len(nums) - 1)))
let readline = require('readline');
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (line) {
let nums = line.split(' ').map(x => +x);
if (nums.length === 1) {
console.log(nums[0]);
} else {
console.log(Math.max(rob(nums, 0, nums.length - 2), rob(nums, 1, nums.length - 1)));
}
rl.close();
}).on('close', function () {
process.exit(0);
});
function rob(nums, start, end) {
let pre2 = 0, pre1 = 0;
for (let i = start; i <= end; i++) {
let cur = Math.max(pre1, pre2 + nums[i]);
pre2 = pre1;
pre1 = cur;
}
return pre1;
}
package main
import (
"fmt"
"strings"
"strconv"
)
func rob(nums []int, start int, end int) int {
pre2, pre1 := 0, 0
for i := start; i <= end; i++ {
cur := max(pre1, pre2 + nums[i])
pre2 = pre1
pre1 = cur
}
return pre1
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
func main() {
var line string
fmt.Scanln(&line)
items := strings.Split(line, " ")
nums := make([]int, len(items))
for i, item := range items {
nums[i], _ = strconv.Atoi(item)
}
if len(nums) == 1 {
fmt.Println(nums[0])
} else {
fmt.Println(max(rob(nums, 0, len(nums)-2), rob(nums, 1, len(nums)-1)))
}
}