codeforces---1040B- Shashlik Cooking(JAVA)

Shashlik Cooking

Description

在这里插入图片描述

Input

The first line contains two integers nn and kk (1≤n≤10001≤n≤1000, 0≤k≤10000≤k≤1000) — the number of skewers and the number of skewers from each side that are turned in one step.

Output

The first line should contain integer l— the minimum number of actions needed by Miroslav to turn over all nn skewers. After than print ll integers from 11 to nn denoting the number of the skewer that is to be turned over at the corresponding step.

Sample

在这里插入图片描述

Note

在这里插入图片描述

题意解释

大概就翻转烤肉,输入两个数n,k。n代表一共有多少串,标号是从1-n,k代表翻转范围,例如k=1时,我翻第2串,那么1、2、3都要翻,左右对称的。k=2时,要翻第2串,那么1、2、3、4都得翻。就是中心点p,和p-k以及p+k范围内的都要翻翻翻。
问怎样才能翻动最少的次数,使得全部的烤串都由初始的正面变成反面。
输出的第一行是次数,第二行是翻转索引。

Java代码实现

package algorithm_study;

import java.util.Scanner;
public class Shashlik_Cooking {

	static int count=0;
	static StringBuffer num = new StringBuffer();
	static int n,k;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		k = sc.nextInt();
		int index;
		if(k==0){
			System.out.println(n);
			for(int i=1; i<=n; i++){
				System.out.print(i +" ");
			}
		}
		else if(n<=2*k+1){
			System.out.println(1);
			System.out.print(n/2+1);
		}else if((n%(2*k+1))==0){
			index = k+1;
			printResult(index);
		} else {
			if (n % (2 * k + 1) < k + 1) {
				index = 1;
				printResult(index);
			} else {
				index = k+1;
				printResult(index);
			}
		}
	}
	
	public static void printResult(int index){
		for(;index<=n;index+=2*k+1){
			count++;
			num.append(index+" ");
		}
		System.out.println(count);
		System.out.println(num);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值