简体   繁体   中英

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

Logic: Sort lanterns in non-decreasing order. Then we need to find maximal distance between two neighbour lanterns, let it be 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]). The answer will be 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,

printf("%.10f\\n",rez/2.); (and removing f), I get Accepted. What does this . mean and why does it give a WA when I use an extra variable f ?

Meaning of . in both statement is as follow-

Let us take an example int a=4; float c; int a=4; float c; If we write c=a/3; then printf("%f",c); will give 1.000000 (precise to six decimal place) , if you write printf("%.10f",c); then it will give 1.0000000000 (precise to ten decimal place) , that means writing . in printf will give significance digit precise to number written after . 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 . So we can achieve precise answer upto reqiured significant digit doing this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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