題目網址
題目
1.一個數先奇數從小到大再偶數從小到大(1–n),問第k個數是什么?
2.舉例發現規律:
第k個數與n的奇偶無關,使用中間數mid=(a+1)/2;進行判斷,k是奇數還是偶數
奇數:count=b*2-1;
偶數:count=(b-mid)*2;
3.因為數很大,使用scanf("%lld %lld",&a,&b);
代碼
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{long long int a=0,b=0,count=0,mid=0;scanf("%lld %lld",&a,&b);mid=(a+1)/2;if(b<=mid)count=b*2-1;elsecount=(b-mid)*2;printf("%lld",count);system("pause");getchar();return 0;}