这个题目挺简单,我是分情况讨论的。
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n,m,i;
cin>>n>>m;
if(m>2*n){
for(i=1;i<=2*n;){
if(m>1){
printf("%d %d ",i+2*n,i);
m--;
i++;
}
if(m<=2*n)break;
if(m>1){
printf("%d %d ",i+2*n,i);
m--;
i++;
}
if(m<=2*n)break;
}
for(;i<=2*n;i++)
printf("%d ",i);
printf("\n");
}
else{
for(i=1;i<m;i++)
printf("%d ",i);
printf("%d\n",i);
}
return 0;
}
看了下排行的大神的代码,真的是感受到了思维的差距,明明很简单就可以解决,我缺写了那么多,加油啊!
# include <cstdio>
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n * 2; ++i)
{
if (i + n * 2 <= m)
printf("%d ", i + n * 2);
if (i <= m)
printf("%d ", i);
}
}