码迷,mamicode.com
首页 > 其他好文 > 详细

(HDU)1266 -- Reverse Number(反向数)

时间:2016-12-05 19:17:07      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:cstring   链接   strlen   bre   包括   amp   pac   return   color   

题目链接:http://bak.vjudge.net/problem/HDU-1266

这题要注意前导0和后导0了,用字符串处理找出需要倒序的位置,这题读入字符串忘了getchar(),调试了半天。

另外要注意的是字符串数组下标是从0开始的,最后有一个回车符。而strlen计算字符串长度遇到回车符停止,长度不包括回车符。

举例 :我往s字符串读入hello。

s[0]=h   s[1]=e   s[2]=l   s[3=l]   s[4]=o   s[5]=\0   strlen(s)返回的是5 。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <string>
 7 #include <cstdlib>
 8 
 9 using namespace std;
10 
11 int main()
12 {
13     int t,len,i;
14     char s[1000];
15     scanf("%d",&t);
16     while(t--)
17     {
18         getchar();
19         int flag=0,last,first;
20         scanf("%s",s);
21         len=strlen(s);
22         char test=s[len-1];
23         last=len-1;
24         while(test==0)
25         {
26             last--;
27             test=s[last];
28         }
29         if(s[0]==-) flag=(1);
30         first=0;
31         for(i=0;i<len-1;i++)
32             if(s[i]!=0&&s[i]!=-)
33             {
34                 first=i;
35                 break;
36             }
37         if(flag) printf("-");
38         for(i=last;i>=first;i--)
39             printf("%c",s[i]);
40         for(i=last+1;i<=len-1;i++)
41             printf("0");
42         printf("\n");
43     }
44     return 0;
45 }

 

(HDU)1266 -- Reverse Number(反向数)

标签:cstring   链接   strlen   bre   包括   amp   pac   return   color   

原文地址:http://www.cnblogs.com/ACDoge/p/6134858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!