给出一个不大于1 000 000的数a。每次操作,你可以将当前的数中的两个非零位交换位置,并都减去1,得到一个新的数。比如155,我们将百位和个位进行操作,就得到了450.请你的程序输出经过任意次上述的操作,a最大能变成什么数。最好运行的速度不超过1秒。请用c++或者c#写
因为是初学者,想请教下高手如何来写这个程序,谢谢!!
偶是菜鸟,不知道怎么做,只弄了个3位数,把个位跟百位交换并都减1的东东,惭愧的很.
////////////////////////
#include "math.h"
void action(int x)
{
int first_num,second_num,third_num,answer;
first_num=x/100;
second_num=(x-first_num*100)/10;
third_num=x-first_num*100-second_num*10;
/* printf("\n%d,%d,%d",first_num,second_num,third_num); */
answer=(third_num-1)*100+second_num*10+(first_num-1);
printf("\n%d",answer);
}
main()
{
int a;
printf("\nplese input one number:");
scanf("%d",&a);
if(a>=100&&a<=999)
{
printf("\nThe number you enter is:%d",a);
action(a);
}
else
{
printf("\nThe number you enter is wrong!");
}
}
////////////////////////////
改了一下,可以进行3位数的操作,找出三位中最大的数跟最高位交换,并分别减一,菜鸟,刚学C,大家见笑了.
///////////////////////////////
#include "math.h"
int max(int x,int y,int z)
{
int answer;
if(y>x)
{
answer=(y-1)*100+(x-1)*10+z;
}
else if(z>x)
{
answer=(z-1)*100+y*10+(x-1);
}
else answer=x*100+y*10+z;
return answer;
}
void action(int x)
{
int first_num,second_num,third_num,answer_num;
first_num=x/100;
second_num=(x-first_num*100)/10;
third_num=x-first_num*100-second_num*10;
answer_num=max(first_num,second_num,third_num);
if(answer_num>x)
{
x=answer_num;
first_num=x/100;
second_num=(x-first_num*100)/10;
third_num=x-first_num*100-second_num*10;
answer_num=max(first_num,second_num,third_num);
}
│ printf("\n%d",x);
│
│
│}
│main()
│{
│ int a;
│ printf("\nplese input one number:");
│ scanf("%d",&a);
│ if(a>=100&&a<=999)
│ {
│ printf("\nThe number you enter is:%d",a);
│ action(a);
│ }
│ else
│ {
│ printf("\nThe number you enter is wrong!");
│ }
}