K - Problem K. Expression in Memories HDU - 6342

 

 Sample Input

5
?????
0+0+0
?+*??
?0+?0
?0+0?

Sample Output

11111
0+0+0
IMPOSSIBLE
10+10
IMPOSSIBLE

判断字符串是否符合格式
分为数字、运算符、问号考虑,注意首尾位置特殊情况
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3  
 4 int main()
 5 {
 6     int t;
 7     char s[1005];
 8     cin >> t;
 9     for(int T = 0;T<t;T++)
10     {
11         cin >> s;
12         for(int i = 0;i < strlen(s);i++)
13         {
14             if(s[i] == '?')
15             {
16                 if(!isdigit(s[i-2]) && s[i-1] == '0' )
17                     s[i] = '+';
18                 else
19                     s[i] = '1';
20             }
21         }
22         int flag = 0;
23         for(int i = 0; i < strlen(s); i++)
24         {
25             if(!isdigit(s[i]))
26             {
27                 if( !isdigit(s[i+1]) )
28                     flag = 1;
29             }
30             else if( s[i] == '0' )
31             {
32                 if( !isdigit(s[i-1]) && isdigit(s[i+1]) )
33                     flag = 1;
34             }
35         }
36         if(!isdigit(s[0]) || !isdigit(s[strlen(s)-1]))
37             flag = 1;
38         if(flag)
39         cout << "IMPOSSIBLE\n";
40         else 
41             cout << s << endl;
42     }
43     return 0;
44 }

 

posted @ 2019-08-05 10:06  YukiRinLL  阅读(276)  评论(0编辑  收藏  举报