你这是蓝桥杯(C本科组)的试题,之前参赛前做的练习题有它,直接你代码吧:
- #include <stdio.h>
- struct
- {
- int num;
- int next;
- int node;
- }N[100000];
- void init()
- {
- int i;
- for(i=0; i<100000;i++)
- {
- N[i].num=i;
- N[i].next=-1;
- N[i].node=-1;
- }
- }
- int maxof(int m)
- {
- int i, t = 0;
- int d[10] = {0,0,0,0,0,0,0,0,0,0};
- while (m > 0)
- {
- d[m%10]++;
- m/=10;
- }
- for (i = 9; i >= 0; i --)
- while (d[i] > 0)
- {
- t*= 10;
- t += i;
- d[i] --;
- }
- return t;
- }
- int minof(int m)
- {
- int i, t = 0;
- int d[10] = {0,0,0,0,0,0,0,0,0,0};
- while (m > 0)
- {
- d[m%10]++;
- m/=10;
- }
- for (i = 0; i <= 9; i ++)
- while (d[i] > 0)
- {
- t*= 10;
- t += i;
- d[i]--;
- }
- return t;
- }
- int trans(int m)
- {
- return (maxof(m)-minof(m));
- }
- int main()
- {
- int i,t,k,j,count=0;
- init();
- for(i = 0;i<100000; i ++)
- if(N[i].next== -1)
- {
- k = i;
- do
- {
- N[k].next = trans(k); //下一个位置
- N[k].node = i; //首先由i开始的链
- k = trans(k);
- }while (N[k].next == -1); //遇到已经处理的节点则结束
-
-
- if (N[k].node== i) //是首先由i起始的链才是首次发现的圈
- {
- t = k;
- printf("%d: [%d", count++, t);
- while (trans(k) != t)
- {
- k = trans(k);
- printf(", %d", k);
- N[k].node = 1;
- }
- printf("]\n");
- }
- }
- }
复制代码
|