Contest2037 - CSU Monthly 2013 Oct (Problem J: Scholarship)

http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2037&pid=9

【题解】:

  这题卡了一下,卡在负数的情况,负数输出 0

  这题主要找到一个个相邻重复的位置,然后加1上去,看是否进位,直到满足条件为止

【code】:

 

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<stdlib.h>
  5 using namespace std;
  6 
  7 
  8 void inttostr(int n,char *str)
  9 {
 10     int cnt=0;
 11     while(n)
 12     {
 13         str[cnt]=n%10+'0';
 14         n/=10;
 15         cnt++;
 16     }
 17     str[cnt]='\0';
 18    // cout<<str<<endl;
 19     int i;
 20     for(i=0;i<cnt/2;i++)
 21     {
 22         swap(str[i],str[cnt-i-1]);
 23     }
 24 }
 25 
 26 int CheckRight(char *str)
 27 {
 28     int len = strlen(str);
 29     int i;
 30     for(i=1;i<len;i++)
 31     {
 32         if(str[i]==str[i-1]&&str[i]!='0')
 33         {
 34             return i;
 35         }
 36     }
 37     return -1;
 38 }
 39 
 40 int main()
 41 {
 42     int n;
 43     scanf("%d",&n);
 44     while(n--)
 45     {
 46         int m;
 47         scanf("%d",&m);
 48          if(m<0)
 49         {
 50             printf("%d\n",0);
 51             continue;
 52         }
 53         if(m<10)
 54         {
 55             printf("%d\n",m+1);
 56             continue;
 57         }
 58         char str[20];
 59         inttostr(m,str);
 60         if(CheckRight(str)==-1)
 61         {
 62             m++;
 63             inttostr(m,str);
 64         }
 65         while(1)
 66         {
 67             int id = CheckRight(str);
 68             if(id==-1)
 69             {
 70                break;
 71             }
 72             else
 73             {
 74                 int len = strlen(str);
 75                 int i,j;
 76                 str[id]++;
 77                 for(i=id+1;i<len;i++)
 78                 {
 79                     str[i]='0';
 80                 }
 81                 for(i=id;i>=0;i--)
 82                 {
 83                     if(str[i]>'9')
 84                     {
 85                         str[i]='0';
 86                         if(i>0)
 87                             str[i-1]++;
 88                         else
 89                         {
 90                             for(j=len+1;j>0;j--)
 91                             {
 92                                 str[j]=str[j-1];
 93 
 94                             }
 95                             str[j] = '1';
 96                         }
 97                     }
 98                 }
 99             }
100         }
101         printf("%s\n",str);
102     }
103     return 0;
104 }

 

posted @ 2013-10-02 17:20  crazy_apple  阅读(247)  评论(0编辑  收藏  举报