Simple floating point Division simulation-hdu-4493-Tutor

Source: Internet
Author: User

Question link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4493

Question meaning:

Calculate the average salary for the 12 months after the decimal point, and output the two decimal places closest to the second decimal point.

Solution:

If this question is first divided by 12, there will be a loss of precision (for example, 12.4449999999999, and the result is 12.45, it should be 12.44). Therefore, divide the question by 12 first, the third digit after the decimal point. If it is greater than or equal to 5, the second digit is carried. Otherwise, it is discarded.

Use a [0] To represent 12 months of income and an integer. A [1] indicates the first decimal place, and a [2] indicates the second decimal place. Then, simulate the high-precision division and obtain the third decimal number after dividing by 12.

Code:

# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <set> # include <stack> # include <list> # include <queue> # define EPS 1e-6 # define INF 0x1f1f1f1f # define PI ACOs (-1.0) # define ll _ int64 # define lson L, M, (RT <1) # define rson m + 1, R, (RT <1) | 1 # pragma comment (linker, "/Stack: 10241000000,1024000000") using namespace STD; // freopen ("data. in "," r ", stdin); // freopen (" data. out "," W ", stdout); int ans [4], a [3]; int main () {int t; scanf (" % d ", & T ); while (t --) {double sum = 0, TT; For (INT I = 1; I <= 12; I ++) scanf ("% lf", & TT ), sum = sum + tt; // printf ("% lf \ n", sum); A [0] = (INT) sum; // integer part A [1] = (INT) (sum * 10) % 10; // first decimal part A [2] = (INT) (sum * 100) % 10; // second decimal part // printf ("% d \ n", a [0], a [1], a [2]); int LA = 0; For (INT I = 0; I <= 2; I ++) // find the third digit after the decimal point {ans [I] = (a [I] + La * 10)/12; La = (a [I] + La * 10) % 12;} ans [3] = La * 10/12; If (ANS [3]> = 5) // carry {ans [2] ++; if (ANS [2]> = 10) // carry {ans [2] = 0; ans [1] ++; If (ANS [1]> = 10) {ans [1] = 0; ans [0] ++ ;}} printf ("$ % d", ANS [0]); If (ANS [1]) // remove the tail 0 {printf (". % d ", ANS [1]); If (ANS [2]) printf (" % d ", ANS [2]);} else if (ANS [2]) // note. 0 printf (". % d ", ANS [1], ANS [2]); putchar ('\ n');} return 0 ;}/ * optional */


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.