Computer Programming Contest Preparation

ToolBox - Source for: 106/10669/judged.c



/home/toolbox/public_html/solutions/106/10669/judged.c
    1 /*
    2  @JUDGE_ID: 47124AK  10669  C
    3  for practice class
    4  date: 2004-07-13
    5  problem: 10669
    6 */
    7 
    8 #include <stdio.h>
    9 #include <stdlib.h>
   10 #include <string.h>
   11 
   12 #define DIGITS 1000
   13 
   14 char input[DIGITS];
   15 char output[DIGITS];
   16 
   17 void half(char *num)
   18 {
   19     char temp[DIGITS];
   20     int i;
   21     int carry = 0;
   22 
   23     for (i = 0; '\0' != num[i]; i++)
   24         {
   25             temp[i] = (num[i] - '0' + carry) / 2 + '0';
   26             carry = ((num[i] - '0') % 2) * 10;
   27         }
   28     temp[i] = '\0';
   29 
   30     for (i = 0; '0' == temp[i]; i++);
   31 
   32     strcpy(num, &temp[i]);
   33 }
   34 
   35 void triple(char *num)
   36 {
   37     char buff[DIGITS];
   38     int i, len;
   39     int carry = 0;
   40     int jsg;
   41 
   42     len = strlen(num);
   43     strcpy(buff, num);
   44     for (i = 0; i < len; i++)
   45         {
   46             jsg = (buff[i] - '0')*3 + carry;
   47             num[i] = (jsg%10 + '0');
   48             carry = jsg/10;
   49         }
   50     if(0 < carry)
   51         {
   52             num[i++] = carry + '0';
   53         }
   54     num[i] = '\0';
   55 }
   56 
   57 void process()
   58 {
   59     int odd;
   60     int i;
   61     char tmp[3] = " ";
   62 
   63     printf("{");
   64     while (strcmp(input, "") != 0)
   65         {
   66             odd = (input[strlen(input)-1] - '0') % 2;
   67 
   68             if (odd)
   69                 {
   70                     printf("%s", tmp);
   71                     for (i = strlen(output) - 1; 0 <= i; i--)
   72                         printf("%c", output[i]);
   73                     strcpy(tmp, ", ");
   74                 }
   75 
   76             half(input);
   77             triple(output);
   78         }
   79     printf(" }\n");
   80 }
   81 
   82 void sub1(char *num)
   83 {
   84     char tmp[DIGITS];
   85     int i;
   86     int len;
   87 
   88     len=strlen(num);
   89     for (i=len-1; i >= 0 && '0' == num[i]; i--)
   90         {
   91             num[i]='9';
   92         }
   93     num[i]=num[i]-1;
   94 }
   95 
   96 int main()
   97 {
   98     scanf("%s ", input);
   99 
  100     while (strcmp(input, "0") != 0)
  101         {
  102             sub1(input);
  103             strcpy(output, "1");
  104             process();
  105 
  106             scanf("%s ", input);
  107         }
  108 
  109     return EXIT_SUCCESS;
  110 }
  111 
  112