变换步骤:对任意一个三位正整数(三位数字均相同的除外),将组成该整数的三个数字重新组合分别生成一个最大数和最小数,用最大数减去最小数得到一个新的三位数(不足三位时高位补0);再对新的三位数重复上述操作,最多重复7次。
编写程序,对从键盘输入的一个三位正整数(三位数字不全相同)实施以上变换过程(共7次),并将每步变换后得到的三位整数输出到屏幕.
12 个解决方案
#1
算法都给了,直接编就行了。
#2
刚学 死活写不出来
#3
闲着无聊,帮你写了,不知道是帮你还是害你,最好还是自己动手啊:)
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
int maxthree(int a,int b,int c)
{
return max(max(a,b),c);
}
int minthree(int a,int b,int c)
{
return min(min(a,b),c);
}
int getNewData(int x)
{
if( x >= 1000)
{
// TRACE("Please input a right integer");
}
int a,b,c;
//ge wei
a = x % 10;
//shi wei
b = (x % 100) /10;
//bai wei
c = x / 100;
int max,min;
max = maxthree(a,b,c);
min = minthree(a,b,c);
return (max - min)*100 + (min - max);
}
void main(void)
{
int x,y,time;
time = 0;
cin >> x;
while(x != 495)
{
time ++;
x = getNewData(x);
cout<<x<<endl;
}
cin>>x;
}
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
int maxthree(int a,int b,int c)
{
return max(max(a,b),c);
}
int minthree(int a,int b,int c)
{
return min(min(a,b),c);
}
int getNewData(int x)
{
if( x >= 1000)
{
// TRACE("Please input a right integer");
}
int a,b,c;
//ge wei
a = x % 10;
//shi wei
b = (x % 100) /10;
//bai wei
c = x / 100;
int max,min;
max = maxthree(a,b,c);
min = minthree(a,b,c);
return (max - min)*100 + (min - max);
}
void main(void)
{
int x,y,time;
time = 0;
cin >> x;
while(x != 495)
{
time ++;
x = getNewData(x);
cout<<x<<endl;
}
cin>>x;
}
#4
> return (max - min)*100 + (min - max);
(max - min)*100 + (min - max) == (max - min)*100 - (max - min) == (max - min) * 99
这是个什么结果?
(max - min)*100 + (min - max) == (max - min)*100 - (max - min) == (max - min) * 99
这是个什么结果?
#5
这就是最大数减去最小数得到的新3位数。最大数的百位必然是3个数字中的最大值,个位是3个数字中的最小值,而最小数正好相反。
#6
#include<stdio.h>
#define max(a,b) ((a)>(b) ? (a) : (b))
#define min(a,b) ((a)<(b) ? (a) : (b))
#define MAX(a,b,c) max(max((a),(b)), (c))
#define MIN(a,b,c) min(min((a),(b)), (c))
void function(int num)
{
int i = 0, a[3];
if(num == 495)
return;
while(num)
{
a[i] = num % 10;
num /= 10;
i++;
}
num = (MAX(a[0], a[1], a[2])-MIN(a[0], a[1], a[2]))*99;
printf("%d\n", num);
function(num);
}
int main()
{
int i;
scanf("%d", &i);
function(i);
return 0;
}
用递归算法
结贴,给分(我没分了,发个问题都不行了)
#7
补充说明一下:
num = (MAX(a[0], a[1], a[2])-MIN(a[0], a[1], a[2]))*99;
num 本来应该等于(MAX-MIN)*100 + (median- median)*10 + (MIN-MAX);
合并之后就等于num = (MAX(a[0], a[1], a[2])-MIN(a[0], a[1], a[2]))*99;
num = (MAX(a[0], a[1], a[2])-MIN(a[0], a[1], a[2]))*99;
num 本来应该等于(MAX-MIN)*100 + (median- median)*10 + (MIN-MAX);
合并之后就等于num = (MAX(a[0], a[1], a[2])-MIN(a[0], a[1], a[2]))*99;
#8
#include <stdio.h>
#include <stdlib.h>
int MAX(int d[])
{
return d[2] * 100 + d[1] * 10 + d[0];
}
int MIN(int d[])
{
return d[0] * 100 + d[1] * 10 + d[2];
}
int compare(const void *p1, const void *p2)
{
return *(int*)p1 - *(int*)p2;
}
void get_digit(int t, int d[])
{
d[0] = t / 100;
d[1] = t % 100 / 10;
d[2] = t % 10;
qsort((void*)d, 3, sizeof(int), compare);
}
int main(void)
{
int t, d[3];
int i;
printf("Input a number: ");
scanf("%d", &t);
for(i = 0; i < 7; ++i) {
printf("%d: ", i);
get_digit(t, d);
printf("%03d - %03d = %03d\n", MAX(d), MIN(d), t = MAX(d) - MIN(d));
if(t == 495)
break;
}
return 0;
}
Input a number: 009
0: 900 - 009 = 891
1: 981 - 189 = 792
2: 972 - 279 = 693
3: 963 - 369 = 594
4: 954 - 459 = 495
Press any key to continue
#9
路过
#10
好像真是这个样子啊,挺好玩啊....
Input:
568
Output:
step 1 :865 - 568 = 297
step 2 :972 - 279 = 693
step 3 :963 - 369 = 594
step 4 :954 - 459 = 495
#include <stdio.h>
char arr[3];
char Max()
{
char tmp;
return (tmp = (arr[0] > arr[1] ? arr[0] : arr[1])) > arr[2] ? tmp : arr[2];
}
char Min()
{
char tmp;
return (tmp = (arr[0] < arr[1] ? arr[0] : arr[1])) < arr[2] ? tmp : arr[2];
}
int doCalc()
{
char c = arr[0] + arr[1] + arr[2];
char a = Max();
char b = Min();
c -= a+b;
int num_max = (a-'0') * 100 + (c-'0') * 10 + b-'0';
int num_min = (b-'0') * 100 + (c-'0') * 10 + a-'0';
printf("%d - %d = %d\n",num_max, num_min, num_max - num_min);
return num_max - num_min;
}
int main()
{
scanf("%s",arr);
int sum = 0,v;
int count = 0;
while(1)
{
printf("step %d :",++count);
int v = doCalc();
arr[0] = v / 100 + '0';
arr[1] = v / 10 % 10 + '0';
arr[2] = v % 10 + '0';
if(v == 495)
break;
}
return 0;
}
Input:
568
Output:
step 1 :865 - 568 = 297
step 2 :972 - 279 = 693
step 3 :963 - 369 = 594
step 4 :954 - 459 = 495
#11
学过
#12
#include <stdio.h>
#include <stdlib.h>
#define MAX(a, b )(((a)>(b))?(a):(b))
#define MIN( a, b) (((a)<(b))?(a):(b))
int threeMax(int a ,int b,int c)
{
return MAX(MAX(a,b),c);
}
int threeMin(int a ,int b,int c)
{
return MIN(MIN(a,b),c);
}
int main()
{
int num;
int i = 0;
scanf("%d",&num);
int hundred,ten,one;
hundred = num/100;
ten = (num%100)/10;
one = num %10;
num = 100*hundred +10*ten + one;
while( i <7)
{
if(num != 495)
{
hundred = num/100;
ten = (num%100)/10;
one = num %10;
// num = 100*hundred +10*ten + one;
num = (threeMax(hundred,ten,one)-threeMin(hundred,ten,one))*100+(threeMin(hundred,ten,one)-threeMax(hundred,ten,one));
printf("the %d count is %d\n",i,num);
i++;
}
else
{
printf("Number is 495!\n");
break;
}
}
getchar();
system("pause");
}
#1
算法都给了,直接编就行了。
#2
刚学 死活写不出来
#3
闲着无聊,帮你写了,不知道是帮你还是害你,最好还是自己动手啊:)
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
int maxthree(int a,int b,int c)
{
return max(max(a,b),c);
}
int minthree(int a,int b,int c)
{
return min(min(a,b),c);
}
int getNewData(int x)
{
if( x >= 1000)
{
// TRACE("Please input a right integer");
}
int a,b,c;
//ge wei
a = x % 10;
//shi wei
b = (x % 100) /10;
//bai wei
c = x / 100;
int max,min;
max = maxthree(a,b,c);
min = minthree(a,b,c);
return (max - min)*100 + (min - max);
}
void main(void)
{
int x,y,time;
time = 0;
cin >> x;
while(x != 495)
{
time ++;
x = getNewData(x);
cout<<x<<endl;
}
cin>>x;
}
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
int maxthree(int a,int b,int c)
{
return max(max(a,b),c);
}
int minthree(int a,int b,int c)
{
return min(min(a,b),c);
}
int getNewData(int x)
{
if( x >= 1000)
{
// TRACE("Please input a right integer");
}
int a,b,c;
//ge wei
a = x % 10;
//shi wei
b = (x % 100) /10;
//bai wei
c = x / 100;
int max,min;
max = maxthree(a,b,c);
min = minthree(a,b,c);
return (max - min)*100 + (min - max);
}
void main(void)
{
int x,y,time;
time = 0;
cin >> x;
while(x != 495)
{
time ++;
x = getNewData(x);
cout<<x<<endl;
}
cin>>x;
}
#4
> return (max - min)*100 + (min - max);
(max - min)*100 + (min - max) == (max - min)*100 - (max - min) == (max - min) * 99
这是个什么结果?
(max - min)*100 + (min - max) == (max - min)*100 - (max - min) == (max - min) * 99
这是个什么结果?
#5
这就是最大数减去最小数得到的新3位数。最大数的百位必然是3个数字中的最大值,个位是3个数字中的最小值,而最小数正好相反。
#6
#include<stdio.h>
#define max(a,b) ((a)>(b) ? (a) : (b))
#define min(a,b) ((a)<(b) ? (a) : (b))
#define MAX(a,b,c) max(max((a),(b)), (c))
#define MIN(a,b,c) min(min((a),(b)), (c))
void function(int num)
{
int i = 0, a[3];
if(num == 495)
return;
while(num)
{
a[i] = num % 10;
num /= 10;
i++;
}
num = (MAX(a[0], a[1], a[2])-MIN(a[0], a[1], a[2]))*99;
printf("%d\n", num);
function(num);
}
int main()
{
int i;
scanf("%d", &i);
function(i);
return 0;
}
用递归算法
结贴,给分(我没分了,发个问题都不行了)
#7
补充说明一下:
num = (MAX(a[0], a[1], a[2])-MIN(a[0], a[1], a[2]))*99;
num 本来应该等于(MAX-MIN)*100 + (median- median)*10 + (MIN-MAX);
合并之后就等于num = (MAX(a[0], a[1], a[2])-MIN(a[0], a[1], a[2]))*99;
num = (MAX(a[0], a[1], a[2])-MIN(a[0], a[1], a[2]))*99;
num 本来应该等于(MAX-MIN)*100 + (median- median)*10 + (MIN-MAX);
合并之后就等于num = (MAX(a[0], a[1], a[2])-MIN(a[0], a[1], a[2]))*99;
#8
#include <stdio.h>
#include <stdlib.h>
int MAX(int d[])
{
return d[2] * 100 + d[1] * 10 + d[0];
}
int MIN(int d[])
{
return d[0] * 100 + d[1] * 10 + d[2];
}
int compare(const void *p1, const void *p2)
{
return *(int*)p1 - *(int*)p2;
}
void get_digit(int t, int d[])
{
d[0] = t / 100;
d[1] = t % 100 / 10;
d[2] = t % 10;
qsort((void*)d, 3, sizeof(int), compare);
}
int main(void)
{
int t, d[3];
int i;
printf("Input a number: ");
scanf("%d", &t);
for(i = 0; i < 7; ++i) {
printf("%d: ", i);
get_digit(t, d);
printf("%03d - %03d = %03d\n", MAX(d), MIN(d), t = MAX(d) - MIN(d));
if(t == 495)
break;
}
return 0;
}
Input a number: 009
0: 900 - 009 = 891
1: 981 - 189 = 792
2: 972 - 279 = 693
3: 963 - 369 = 594
4: 954 - 459 = 495
Press any key to continue
#9
路过
#10
好像真是这个样子啊,挺好玩啊....
Input:
568
Output:
step 1 :865 - 568 = 297
step 2 :972 - 279 = 693
step 3 :963 - 369 = 594
step 4 :954 - 459 = 495
#include <stdio.h>
char arr[3];
char Max()
{
char tmp;
return (tmp = (arr[0] > arr[1] ? arr[0] : arr[1])) > arr[2] ? tmp : arr[2];
}
char Min()
{
char tmp;
return (tmp = (arr[0] < arr[1] ? arr[0] : arr[1])) < arr[2] ? tmp : arr[2];
}
int doCalc()
{
char c = arr[0] + arr[1] + arr[2];
char a = Max();
char b = Min();
c -= a+b;
int num_max = (a-'0') * 100 + (c-'0') * 10 + b-'0';
int num_min = (b-'0') * 100 + (c-'0') * 10 + a-'0';
printf("%d - %d = %d\n",num_max, num_min, num_max - num_min);
return num_max - num_min;
}
int main()
{
scanf("%s",arr);
int sum = 0,v;
int count = 0;
while(1)
{
printf("step %d :",++count);
int v = doCalc();
arr[0] = v / 100 + '0';
arr[1] = v / 10 % 10 + '0';
arr[2] = v % 10 + '0';
if(v == 495)
break;
}
return 0;
}
Input:
568
Output:
step 1 :865 - 568 = 297
step 2 :972 - 279 = 693
step 3 :963 - 369 = 594
step 4 :954 - 459 = 495
#11
学过
#12
#include <stdio.h>
#include <stdlib.h>
#define MAX(a, b )(((a)>(b))?(a):(b))
#define MIN( a, b) (((a)<(b))?(a):(b))
int threeMax(int a ,int b,int c)
{
return MAX(MAX(a,b),c);
}
int threeMin(int a ,int b,int c)
{
return MIN(MIN(a,b),c);
}
int main()
{
int num;
int i = 0;
scanf("%d",&num);
int hundred,ten,one;
hundred = num/100;
ten = (num%100)/10;
one = num %10;
num = 100*hundred +10*ten + one;
while( i <7)
{
if(num != 495)
{
hundred = num/100;
ten = (num%100)/10;
one = num %10;
// num = 100*hundred +10*ten + one;
num = (threeMax(hundred,ten,one)-threeMin(hundred,ten,one))*100+(threeMin(hundred,ten,one)-threeMax(hundred,ten,one));
printf("the %d count is %d\n",i,num);
i++;
}
else
{
printf("Number is 495!\n");
break;
}
}
getchar();
system("pause");
}