Short Code. Calculate the distance between 2 points

Source: Internet
Author: User
Problem:
Input two-point coordinates (x1, Y1), (X2, Y2), calculate and output the distance between two points.

There are multiple groups of input data. Each group occupies one row and consists of four real numbers, representing X1, Y1, X2, and Y2 respectively. Data is separated by spaces.

For each group of input data, a row is output, and the result is retained with two decimal places.
For example, enter:

0   0   0   1
0   1   1   0

Output:

1.00
1.41

The question is simple and even boring.

# Include < Math. h>
Int main ()
{
Float A, B, c, d;
While (Scanf ( " % F " , & A, & B , & C , & D ) = 4)
{
Printf ( " %. 2f \ n " , SQRT (( A -C ) * ( A -C ) + (B -D ) * (B -D )));
}
Return 0;
}

Well, the first one looks pretty short. Well, for the compiler, use the C compiler, GCC, or VC (Save the. c file ). In this way, we can take advantage of some C 'proxy', at least to shortenCodeIt is a benefit.

# Include < Math. h>
Main ()
{
Float A, B, c, d;
While (Scanf ( " % F " , & A, & B , & C , & D ) = 4)
Printf ( " %. 2f \ n " , SQRT (( A -C ) * ( A -C ) + (B -D ) * (B -D )));
}

The default function return type is int. What is the return value? I want to know how much it is. It can be short. Remove unnecessary spaces.

# Include < Math. h>
Main () { Float A, B, c, d ; While (Scanf ( " % F " , & A, & B , & C , & D ) = 4 ) Printf ( " %. 2f \ n " , SQRT (( A -C ) * ( A -C ) + (B -D ) * (B -D )));}

125
Unfortunately, you have to keep math. h; otherwise, SQRT is undefined.C LanguageI think it is a function that returns an int, so the link fails to be linked to the function we want.
Continue to shorten, the square of the difference is very eye-catching, so many parentheses. The return value of scanf is almost the same as 3 and 4. Instead of requiring 4 input, you can get 5. Also changed

# Include <math. h>
Main () {float a, B, c, d; while (scanf ("% F", & A, & B, & C, & D)> 3) A-= C, B-= D, printf ("%. 2f \ n ", SQRT (A * A + B * B ));}

118
The length of while and for is the same. Although for is less than that of while, for statements require two semicolons. It can be regarded as flattening, but it is not actually. Note that when there are multiple sentences in the loop body, you can save a comma --

# Include <math. h>
Main () {float a, B, c, d; For (; scanf ("% F", & A, & B, & C, & D)> 3; printf ("%. 2f \ n ", SQRT (A * A + B * B) A-= C, B-= D ;}

117
The file that contains is really annoying and must be wrapped in a line break. It would be nice to remove it. Since I only want SQRT, why not declare it myself.

Double SQRT (); main () {float a, B, c, d; For (; scanf ("% F", & A, & B, & C, & D)> 3; printf ("%. 2f \ n ", SQRT (A * A + B * B) A-= C, B-= D ;}

114
Well, it seems a little shorter. Can double and float be used in general? Maybe not. Do you want to try it?

Float SQRT (); main () {float a, B, c, d; For (; scanf ("% F", & A, & B, & C, & D)> 3; printf ("%. 2f \ n ", SQRT (A * A + B * B) A-= C, B-= D ;}

113
The output result is correct. Strange... decompilation. Oh. As a parameter, float is also converted to a double Type Pressure stack. This may not be safe. God knows if all machines are pressed double --. But fortunately, we can make it together here, so we can make an estimation of X86.
Ah, I'm so stupid. Why are there two float in the code! Why not merge them?

Main () {float SQRT (), a, B, c, d; For (; scanf ("% F", & A, & B, & C, & D)> 3; printf ("%. 2f \ n ", SQRT (A * A + B * B) A-= C, B-= D ;}

107
The farmer came up with a method with the "-1 binary" prompt ". Oh! By the way, when scanf returns EOF when the input ends, it is-1. The binary value of-1 is full 1, that is, the anticode is 0. Well, thanks to Jack farmer, I want to shorten the byte to 3 ~

Main () {float SQRT (), a, B, c, d; (;~ Scanf ("% F", & A, & B, & C, & D); printf ("%. 2f \ n ", SQRT (A * A + B * B) A-= C, B-= D ;}

106
End...
If you have a more BT idea to shorten the code, please don't forget to tell me ~

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.