由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问一道C++编程题
相关主题
what's the output汗,不问算法
代码求助问一道题
分享A公司面经Python 里有类似 Sprintf 的函数么?
问个IQ 题伪O(1) space的O(n)时间重新排列a1a2a3...b1b2b3...算法
请教一个bloomberg题目问下LeetCode上的题目:count and say
float的格式化打印C的argc问题
这题有好办法吗?攒人品 报BB面经
A simple interview questionFacebook phone screen
相关话题的讨论汇总
话题: long话题: string话题: double话题: via话题: do
进入JobHunting版参与讨论
1 (共1页)
l********y
发帖数: 1327
1
输入一个long double,格式化后输出。例如: 234567614.98 -> 234,567,
614.98
-给出test case
方法是,
Char a[200];
Sprintf(a,”%Lf”,d);
但是得到的c string数组后面总有0000的结尾,比如输入2345.98得到2345.980000,有
什么办法可以把这些后缀的0去掉呢?
l********y
发帖数: 1327
2
主要是不清楚sprintf总是会得到后缀是0000的c string吗?如果每次都是0000结尾那
倒反而好了,如果不是那就不明白如何判断了
h**6
发帖数: 4160
3
如果没有指定精度,默认保留六位小数。
l********y
发帖数: 1327
4
用了sprintf(a,".2Lf",d),结果出来的c string数组是1234.98D\01这样的,不知道为
什么已经限制精度为2了,还是出来几个字符非要补齐6位?
s*****e
发帖数: 16824
5
试一下限制位数总长,比如6.2Lf.

【在 l********y 的大作中提到】
: 用了sprintf(a,".2Lf",d),结果出来的c string数组是1234.98D\01这样的,不知道为
: 什么已经限制精度为2了,还是出来几个字符非要补齐6位?

l********y
发帖数: 1327
6
好像不行,而且如何事先知道long double转化为c string以后的长度?
p******z
发帖数: 38
7
有方法限制吗?
如果没有的话,先转成字符串再操作?对字符串的操作方法很多的
d*********g
发帖数: 59
8
如果转换成字符串,那转换成的字符串里岂不是不知道有多少个0000
t****t
发帖数: 6806
9
you are totally missing the point -- the question asked you to add "," as
the thousands' grouping character. printf() family is not guaranteed to do
that (i.e. implementation-defined), and if it does, it also depends on
locale and some special flag ("%'f" for SUSv2, for example). so you are not
supposed to use printf() family (i.e. whoever asked you the question do not
expect you to do that). you are supposed to do it manually.
as for the trailing zero, you need to give more information -- probably
depends on whether the input is send in via a string (e.g. from cin) or via
a long double variable. if it is sent in via string, simply don't involve a
long double, process the string and add "," wherever it should be. if it is
sent in via long double, you'll need to know the output precision (given or
you may assume a default value), otherwise long double could be quite long
that you don't know whether a trailing number is really there or just error.

【在 l********y 的大作中提到】
: 输入一个long double,格式化后输出。例如: 234567614.98 -> 234,567,
: 614.98
: -给出test case
: 方法是,
: Char a[200];
: Sprintf(a,”%Lf”,d);
: 但是得到的c string数组后面总有0000的结尾,比如输入2345.98得到2345.980000,有
: 什么办法可以把这些后缀的0去掉呢?

1 (共1页)
进入JobHunting版参与讨论
相关主题
Facebook phone screen请教一个bloomberg题目
c++ 问题float的格式化打印
问两个题这题有好办法吗?
请教一道c/c++题 (转载)A simple interview question
what's the output汗,不问算法
代码求助问一道题
分享A公司面经Python 里有类似 Sprintf 的函数么?
问个IQ 题伪O(1) space的O(n)时间重新排列a1a2a3...b1b2b3...算法
相关话题的讨论汇总
话题: long话题: string话题: double话题: via话题: do