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);
}
}