这是个老题目了,主要考察的是能不能考虑到所有的情况,还有就是怎么判断是否溢出。
情况大致分为如下四种:(下面我使用@代替空格,x代表除数字之外的所有字符)
1. "@@[+-]3232343@@"
2. "@@[+-]333x54"
3. "@@[+-]4343"
4. "2147483648"或者"-2147483649"(也就是溢出的情况)
下面代码中给出了两个判断是否溢出的方法。...
分类:
其他好文 时间:
2014-06-08 15:47:49
阅读次数:
270
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca...
分类:
其他好文 时间:
2014-06-07 12:21:30
阅读次数:
284
#include using namespace std;int main(){#if 1
int num = 12345; char str[25];//不要写成char*,因为没有分配空间 itoa(num, str, 10);//10进制字符串
printf("num ...
分类:
其他好文 时间:
2014-06-07 03:32:09
阅读次数:
215
问题
输入:一个表示数字的字符串,需要考虑不同的输入形式。
输出:对应的整数
特殊输入形式:
1.输入开始几个字符为空格
2.考虑正负号
3.数字字符不属于[0,9]时,输出当前结果
4.字符串代表的数字大于INT_MAX或者小于INT_MIN时输出INT_MAX或者INT_MIN。
class Solution {
// out of range...
分类:
其他好文 时间:
2014-06-03 06:30:14
阅读次数:
314
Implementatoito convert a string to an
integer.Hint:Carefully consider all possible input cases. If you want a
challenge, please do not see below and ...
分类:
其他好文 时间:
2014-06-02 08:51:43
阅读次数:
233
1 import "strconv" 2 3 func IsLeapYear(y string)
bool { //y == 2000, 2004 4 //判断是否为闰年 5 year, _ := strconv.Atoi(y) 6 if year%4 ==
0 && y...
分类:
其他好文 时间:
2014-05-26 15:12:52
阅读次数:
230
char*itoa(intvalue,char*string,intradix);int value
被转换的整数,char *string 转换后储存的字符数组,int radix 转换进制数,如2,8,10,16 进制等头文件:
atof(将字符串转换成浮点型数)atoi(将字符串转换成整型数)...
分类:
其他好文 时间:
2014-05-24 03:40:06
阅读次数:
232
怎样把数字转为字符串(与atoi相反)?有itoa函数吗?
用sprintf就可以了:
sprintf(string, "%d", number);同理,也可以同sprintf把long型或浮点型转换成字符串(使用%ld或%f),也就是说,可以把sprintf看成是atol或者atof的
反函数。
怎样在日期上加n天?怎样取得两个日期的时间间隔?
第一个问题,mktime接受没有...
分类:
编程语言 时间:
2014-05-18 06:20:21
阅读次数:
356
非常考虑思维全面性的一道题,考验是否能够考虑本问题的方方面面。题目:将一个string转换为int。实现函数atoi()的功能。先应该明确atoi()有哪些特殊功能:(正常的正负数情况我就不列了)inputoutput”+1“1”
+ 1“0(error了)” 1“1(前头只有空格是合法的...
分类:
其他好文 时间:
2014-05-16 01:08:33
阅读次数:
245