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

题目链接: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 }
时间: 2024-12-20 10:44:46

(HDU)1266 -- Reverse Number(反向数)的相关文章

HDU 1266 Reverse Number(字符串逆转 水题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1266 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 proble

杭电 HDU 1266 Reverse Number

Reverse Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5763    Accepted Submission(s): 2643 Problem Description Welcome to 2006'4 computer college programming contest! Specially, I give

hdu 3709 Balanced Number(平衡数)--数位dp

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 9036    Accepted Submission(s): 4294 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

HDU 1018 Big Number 斯特林数近似n!

题目链接:点击打开链接 斯特林数:点击打开链接 题意是计算n! 的位数 即ans = log10(n!) = log10(sqrt(2πn)) + n*log10(n/e) #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include <

HDU 4937 Lucky Number 搜索

题意: 给你一个数,求在多少种不同的进制下这个数每一位都是3.4.5.6中的一个. 思路: 搜索.枚举这个数在任意进制下的表示,判断是否合法.当数字只有3.4.5.6时,必定有无穷种. 因为数字太大,所以直接枚举必定会超时. 下面有两种剪枝的方法: 1. 先枚举最后一位的情况. 假设数字n在base进制下表示为 a[n]...a[0],即 n = a[0] + a[1]*base^1 + ... + a[n]*base^n. 则 n - a[0] = a[1]*base^1 + ... + a[

hdu 4937 Lucky Number ( 进制转换+枚举 )

题意: 有一个数n,问有多少个进制x(基数)使得n转换为x进制后的数字中只有3.4.5.6四个数. 算法: 对于只有一位数的情况,显然3.4.5.6都应该输出-1. 如果有2位数,假设这2位中高位为a,低位为b,进制为base,则 n = a * base + b,解一元一次方程即可. 如果有3位数,假设这3为从高到低分别为a.b.c,进制为base,则 n = a * base * base + b * base + c,即一元二次方程即可. 如果位数>= 4,可以暴力枚举进制数.base>

HUST 1343 Reverse Number(哈理工 亚洲区选拔赛前练习赛)

G - Reverse Number Time Limit:1000MS    Memory Limit:131072KB    64bit IO Format:%lld & %llu SubmitStatusPracticeHUST 1347 Description Given a non-negative integer sequence A with length N, you can exchange two adjacent numbers each time. After K exc

枚举 + 进制转换 --- hdu 4937 Lucky Number

Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 294    Accepted Submission(s): 49 Problem Description “Ladies and Gentlemen, It’s show time! ” “A thief is a creative artist who ta

hust 1347 - Reverse Number

题目描述 Given a non-negative integer sequence A with length N, you can exchange two adjacent numbers each time. After K exchanging operations, what’s the minimum reverse number the sequence can achieve? The reverse number of a sequence is the number of