简体   繁体   English

什么是“。” 意味着输出?

[英]What does a '.' means in output?

So, I was solving a question which basically has the following code: 因此,我正在解决一个基本上具有以下代码的问题:

int rez,arr[10000],n,l,diff;
float f;
int main (void)
{
    cin>>n>>l;
    for (int i = 0; i < n; i++ )
    {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    rez = 2*max(arr[0],l-arr[n-1]);
    for ( int i = 0; i < n-1; i++ )
    {
        rez = max(rez, arr[i+1]-arr[i]);
    }
    f = rez/2.0;
    printf("%.10f\n",f);
    return 0;
}

The above is the code for the following problem: 上面是以下问题的代码:

http://codeforces.com/problemset/problem/492/B http://codeforces.com/problemset/problem/492/B

Logic: Sort lanterns in non-decreasing order. 逻辑:按非降序对灯笼排序。 Then we need to find maximal distance between two neighbour lanterns, let it be maxdist. 然后我们需要找到两个相邻灯笼之间的最大距离,使其为maxdist。 Also we need to consider street bounds and count distances from outside lanterns to street bounds, it will be (a[0] - 0) and (l - a[n - 1]). 另外,我们还需要考虑街道边界并计算从外部灯笼到街道边界的距离,即(a [0]-0)和(l-a [n-1])。 The answer will be max(maxdist / 2, max(a[0] - 0, l - a[n - 1])) 答案将是max(maxdist / 2,max(a [0]-0,l-a [n-1]))

Now, the above code gives a WA (Wrong answer) but on modifying the last statement to, 现在,以上代码给出了WA(错误答案),但在修改最后一条语句时,

printf("%.10f\\n",rez/2.); (and removing f), I get Accepted. (并删除f),我被接受了。 What does this . 这是什么. mean and why does it give a WA when I use an extra variable f ? 是什么意思,为什么当我使用额外的变量f时会给出WA?

Meaning of . 的含义. in both statement is as follow- 在这两个陈述中如下:

Let us take an example int a=4; float c; 让我们举一个例子int a=4; float c; int a=4; float c; If we write c=a/3; 如果我们写c=a/3; then printf("%f",c); 然后printf("%f",c); will give 1.000000 (precise to six decimal place) , if you write printf("%.10f",c); 如果您写printf("%.10f",c);将给出1.000000 (精确到小数点后六位printf("%.10f",c); then it will give 1.0000000000 (precise to ten decimal place) , that means writing . 那么它将给出1.0000000000 (精确到小数点后十位),即表示. in printf will give significance digit precise to number written after . printf中的有效数字将精确地写在之后的数字上. And if we write c=a/3.0 this means we tell compiler to divide int by float and here implict conversion(or type promotion) takes place and complier treat int a as float and give output 1.333333 . 而且,如果我们编写c=a/3.0这意味着我们告诉编译器将int by float并在此处进行隐式转换(或类型提升),然后Complier将int a as float并给出输出1.333333 So we can achieve precise answer upto reqiured significant digit doing this. 这样我们就可以达到精确的答案,直到要求的有效位数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM