Chinaunix首页 | 论坛 | 博客
  • 博客访问: 985466
  • 博文数量: 153
  • 博客积分: 4195
  • 博客等级: 上校
  • 技术积分: 2631
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-22 11:32
文章存档

2012年(7)

2010年(35)

2009年(111)

分类:

2009-11-16 11:28:15

写写代码真不错,把c语言的要点又感觉温习了一遍。
#include
struct bigandsmall
{
 int big;
 int small;
};
int biggertosmaller(int num,struct bigandsmall *this)
{
 int a[4] ;
 int i,j,tmp;
 if(num == 999)
 {
  this->big = 999;
  this->small = 999;
  return 0;
 }
 a[0] = num/1000;
 a[1] = num%1000/100;
 a[2] = num%100/10;
 a[3] = num%10;
 for(i = 0; i < 4; i++)
 for(j = i+1; j < 4; j++)
 {
  if(a[i] <= a[j])
  {
   tmp = a[i];
   a[i] = a[j];
   a[j] = tmp;
  }
 }
 this->big = a[0]*1000+a[1]*100+a[2]*10+a[3];
 this->small = a[3]*1000+a[2]*100+a[1]*10+a[0];
// printf("%d%d%d%d\n",a[0],a[1],a[2],a[3]);
 return 0;
   
}

int main()
{
 int num,bigger,smaller,result,count;
 struct bigandsmall this;
 while(scanf("%d",&num),num!=-1)
{
 count = 0;
 if(num < 1000 || num > 9999 || num %1111 == 0)
 {  
  printf("NO!!\n");
  continue; 
 }
 printf("N=%d\n",num);
 do
 {
 if(count) 
  num = bigger-smaller;
 count++;
 biggertosmaller(num,&this);
 bigger = this.big;
 smaller = this.small;
 result = bigger-smaller;
 printf("%d - %d = %d\n",bigger,smaller,result);
 }
 while(result != 6174 && result != 0);
 printf("OK!! %d timers\n",count);  
}
 return 0;
}
阅读(1124) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-11-17 12:19:28

这道题目的核心其实就是排序,可以用一下c语言自带的排序函数qsort: 如下(从小到大排序) #include #include #include #define LEN 8 int cmp1(const void *a, const void *b) { //printf("in the cmp\n"); char *aa = (char*)a; char *bb = (char*)b; return (*aa-*bb); } int main() { char aa[LEN] = {-8,5,83,2,-45,23,19,9}; int i; for(i = 0; i < LEN; i++) printf("%d ",aa[i]); printf("\n"); qsort(aa, strlen(aa), sizeof(char), cmp1); for(i = 0; i < LEN; i++) printf("%d ",aa[i]