hdu 1266 Reverse Number

来源:互联网 发布:java架构设计 书籍 编辑:程序博客网 时间:2024/05/14 17:52

Reverse Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5196    Accepted Submission(s): 2421


Problem Description
Welcome to 2006'4 computer college programming contest!

Specially, I give my best regards to all freshmen! You are the future of HDU ACM! And now, I must tell you that ACM problems are always not so easy, but, except this one... Ha-Ha!

Give you an integer; your task is to output its reverse number. Here, reverse number is defined as follows:
1. The reverse number of a positive integer ending without 0 is general reverse, for example, reverse (12) = 21;
2. The reverse number of a negative integer is negative, for example, reverse (-12) = -21;
3. The reverse number of an integer ending with 0 is described as example, reverse (1200) = 2100.
 

Input
Input file contains multiple test cases. There is a positive integer n (n<100) in the first line, which means the number of test cases, and then n 32-bit integers follow.
 

Output
For each test case, you should output its reverse number, one case per line.
 

Sample Input
312-121200
 

Sample Output
21-212100
 

题目大意:给出一个数把这个数的各个位数翻转过来,如果后边有一串0,那么这串0原样输出
就像110200输出应该是201100,负号也不变
思路:定义两个字符数组,先判断第一个是不是负号,如果是直接把s1[0]的值给s2[0],再判断
s1后边有几个0,从后边开始查找,直到不为0,把s1的值倒序存入s2中,输出。 
2014,10,31;
#include<stdio.h>#include<string.h>int main(){int t,i,j,k,l;char s1[110],s2[110];scanf("%d",&t);getchar();while(t--){memset(s1,0,sizeof(s1));memset(s2,0,sizeof(s2));i=0;scanf("%s",s1);k=strlen(s1);if(s1[0]=='-'){s2[0]=s1[0];i++;}for(j=k-1;j>=0;j--)if(s1[j]!='0')break;l=j;for(;i<=j;i++,l--){s2[l]=s1[i];}for(i=0;i<=j;i++)printf("%c",s2[i]);for(i=j+1;i<k;i++)printf("%c",s1[i]);printf("\n");}return 0;}


0 0
原创粉丝点击