hdu acm 1982 简单字符串处理

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1982

题目大意:

输入一行字符串,此字符串除了数字就是‘#’和‘-’,要你依据数字转换为大写字母。

我的思路:

我想利用if语句对字符串的单个字符进行判断,但是后面发现不行,因为要是字符里连着两个数的话,大于9,这个时候,要是逐个读取,就只会输出‘A’~‘I';

后面,我又想了想可以利用这个式子:t=t*10+str[i]-'0' 来处理:

程序代码如下:

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 char str[10005],zimubiao[30]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
 5 int main()
 6 {
 7     int n;
 8     int len=0,i;
 9     int t;
10     scanf("%d",&n);
11     getchar();
12     while(n--)
13     {
14         t=0;
15         gets(str);
16         len=strlen(str);
17         for(i=0;i<len;i++)
18         {
19             if(str[i]>='0' && str[i]<='9')
20                 t=t*10+str[i]-'0';
21             else
22             {
23                 if(t>0)
24                 {
25                     printf("%c",zimubiao[t-1]);
26                     t=0;
27                 }
28                 if(str[i]=='#') printf(" ");
29             }
30         }
31         if(t>0) printf("%c",zimubiao[t-1]);
32         if(n>0) printf("\n");
33     }
34     printf("\n");
35     return 0;
36 }

还请各位大牛多多指教。

2012-07-17

posted on 2012-07-17 15:27  l流沙  阅读(174)  评论(0)    收藏  举报

导航