题目描述
最近,Dr. Kong 新设计一个机器人Bill。这台机器人很聪明,会做许多事情。惟独对自然数的理解与人类不一样,它是从右往左读数。比如,它看到123时,会理解成321。让它比较23与15哪一个大,它说15大。原因是它的大脑会以为是32与51在进行比较。再比如让它比较29与30,它说29大。
给定Bill两个自然数A和B,让它将 [A,B] 区间中的所有数按从小到大排序出来。你会认为它如何排序?
输入
输入一组数据A B
两个正整数A B 表示待排序元素的区间范围。
要求 1<=A<=B<=200000 B-A<=50。
输出
输出一行,为所有排好序的元素,元素之间有一个空格
样例输入
22 39
样例输出
30 31 22 32 23 33 24 34 25 35 26 36 27 37 28 38 29 39
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int A,B; //1<=A<=B<=200000 B-A<=50。
cin>>A>>B; //输入两个数 A和B。
cout<<endl;
int i,j,k=0,t,m,h,d,s;
int a[6],b[51],c[51];
h=A;
for(i=0;i<=B-A;i++) //将A,B之间的数用数组c[]储存。
{
c[i]=h;
h++;
// cout<<c[i]<<" ";
}
for(int i=A;i<=B;i++) //将A B之间的数都倒序,储存在数组b[]中。
{
int io=i;
j=0;
s=0;
while(1)
{
if(io/10==0)
{
a[j]=io;
m=t=j;
break;
}
else
{
a[j]=io%10;
io=io/10;
j++;
}
}
for(j=0;j<=m;j++)
{
s+=a[j]*pow(10,t);
t--;
}
b[k]=s;
k++;
}
t=B-A;
while(t>=0) //冒泡排序。
{
for(j=0;j<t;j++)
{
if(b[j]>b[j+1]) //使数组b[]中的数从小到大排序。
{
m=c[j]; //将c[]中数一一对应调换。
c[j]=c[j+1];
c[j+1]=m;
d=b[j];
b[j]=b[j+1];
b[j+1]=d;
}
}
t--;
}
/* //b[]中输出的结果。
for(j=0;j<=B-A;j++)
cout<<b[j]<<" ";
cout<<endl;
*/
for(j=0;j<=B-A;j++) //最终结果。
cout<<c[j]<<" ";
cout<<endl;
return 0;
}
N组数据代码。仅供参考。
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int N;
int i,j,k=0,t,m,h,d,s,f;
cin>>N;
cout<<endl;
int A[5],B[5]; //A[]和B[]中一一对应,如A[1]对应B[1]。
h=0;
for(int x=0;x<N;x++)
cin>>A[x]>>B[x]; // //1<=A[x]<=B[x]<=200000 B[x]-A[x]<=50。
for(int x=0;x<N;x++)
{
int a[6],b[51],c[51];
h=A[x];
for(i=0;i<=B[x]-A[x];i++) //将A[x],B[x]之间的数用数组c[]储存。
{
c[i]=h;
h++;
// cout<<c[i]<<" ";
}
for(int i=A[x];i<=B[x];i++) //将A[x] B[x]之间的数都倒序,储存在数组b[]中。
{
int io=i;
j=0;
s=0;
while(1)
{
if(io/10==0)
{
a[j]=io;
m=t=j;
break;
}
else
{
a[j]=io%10;
io=io/10;
j++;
}
}
for(j=0;j<=m;j++)
{
s+=a[j]*pow(10,t);
t--;
}
b[k]=s;
k++;
}
t=B[x]-A[x];
while(t>=0) //冒泡排序。
{
for(j=0;j<t;j++)
{
if(b[j]>b[j+1]) //使数组b[]中的数从小到大排序。
{
m=c[j]; //将c[]中数一一对应调换。
c[j]=c[j+1];
c[j+1]=m;
d=b[j];
b[j]=b[j+1];
b[j+1]=d;
}
}
t--;
}
/* //b[]中输出的结果。
for(j=0;j<=B-A;j++)
cout<<b[j]<<" ";
cout<<endl;
*/
for(j=0;j<=B[x]-A[x];j++) //最终结果。
cout<<c[j]<<" ";
cout<<endl;
}
return 0;
}