1 2 3 4 5 6 7 8 9
9 8 7 6 5,4 3 2 1
5 6 7,8 9,1 2,3 4
7 6,5,9,8,2,1,4,3
6,7,5,9,8,2,1,4,3
多次倒序
Input
9 4
Output
8
折中枚举,两种情况,右边的,左边的……
View Code
#include<stdio.h>
int main()
{
int n,x,y;
scanf("%d%d",&n,&y);
int add=0;
while(n>=2)
{
x=n+1-y;
if(x>(n+1)/2)
{
y=x-(n+1)/2;
add+=(n+1)/2;
n=n/2;
}
else
{
y=x;
n=(n+1)/2;
}
}
printf("%d\n",add+y);
return 0;
}