"Algorithmic Competition Primer Classic" 1.5.6 On-machine practice

Source: Internet
Author: User
Tags float number

Exercise 1-1 average (average)

1#include <stdio.h>2 intMain ()3 {4     inta,b,c;5scanf"%d%d%d",&a,&b,&c);6printf"%0.3LF", (A+B+C)/3.0);7     return 0;8}

The focus is on format output, type conversion ...

But in

printf ("%0.3lf", (A+B+C)/3), when input 1 1 2 is output 0, this I really do not understand, int type is not to get 1 and then double ...

Exercise 1-2 temperature (temperature)
1#include <stdio.h>2 intMain ()3 {4     floatf,c;5scanf"%f",&f);6printf"%0.3f",5* (f +)/9.0);7     return 0;8}

This in

scanf ("%f",&f); Change%f to%d, input 32 also error, result output:-17.778. Normal is not supposed to be 0 ... Although there is an int and float conversion, but it does not feel ah ...

I searched the web http://tieba.baidu.com/p/88194315
float a=3; printf ("a=%d\n", a) results a=0 ah? Ask for advice because you define a as a Folat type, but your output is int type. The output computer automatically converts the value of the float type to int, which is to assign the latter 16 bits of the float to the int number. In this question, the 16 bits of the float number are all zeros. So the output is 0. Or a forced type conversion. Or with the%f type output??

Feel the sense of that.

Exercise 1-3 continuous and (sum)

1#include <stdio.h>2 intMain ()3 {4     intN;5scanf"%d",&n);6printf"%d", N (n+1)/2);7     return 0;8}

Exercise 1-4 sine and cosine (sincos)

1#include <stdio.h>2#include <math.h>3 intMain ()4 {5     Const floatPi=4.0*atan (1.0);6     intN;7scanf"%d",&n);8printf"%LF%LF", Sin (n*pi/ the), cos (n*pi/ the) );9     return 0;Ten}

The key is math ...

Exercise 1-5 distance (distance)

1#include <stdio.h>2#include <math.h>3 intMain ()4 {5     floatx1, x2, y1, y2;6     floats;7scanf"%f%f%f%f",&x1,&y1,&x2,&y2);8S=sqrt ((x1-x2) * (X1-X2) + (y1-y2) * (y1-y2));9printf"%f", s);Ten     return 0; One}

"Algorithmic Competition Primer Classic" 1.5.6 On-machine practice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.