[DTOJ] 2703: the remainder and Quotient of two numbers, and the remainder of dtoj2703
DTOJ 2703: remainder of two numbers and Business Solution report
- 2017.11.10 First Edition--CoolOriginal, referencing the Chinese version of C ++ Primer Plus (6th)
Question information:Description
Give You a and B and ask for their remainder and non-integer quotient. Retain two decimal places.
Output
Remainder and operator
Sample Input
5 3
Sample output
2 1.67
Prompt
1 <= a, B <= 10000
Ideas:
Using operators to calculate the remainder and quotient, the modulo operation is mentioned in the previous article.
Note:
The behavior of the Division operator (/) depends on the type of the operand. If both operands are integers, C ++ performs integer division. This means that the fractional part of the result will be discarded so that the final result is an integer. If one or two of the operands is a floating point value, the fractional part is retained and the result is a floating point number.
-- The preceding excerpt is fromC ++ Primer Plus (6th) Chinese Version
This means that before executing the fractional division, you must ensure that one of the operands is a floating point value. Keep two decimal places
My code (C ++ ):
1 //DTOJ 2703 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 int a,b; 8 cin>>a>>b; 9 int c=a%b;10 double d=double(a)/b;11 printf("%d %.2f",c,d);12 return 0;13 }
Analysis:
Row 7: int a, B. Because the modulo operation is performed here, it must be two integers. So we define two integer int variables.
Row 10: double d = double (a)/B. As mentioned above, at least one of the operands for executing non-integer division operations is a floating point value, so double () is used here () convert a to double-precision floating-point double to obtain a non-integer quotient.
Row 11th: printf ("% d %. 2f ", c, d); in this example, printf is used to print the values of c and d to the screen (output). Because two decimal places need to be retained, it is added to the position of d. 2f
This work is licensed using the knowledge sharing signature-non-commercial use-share the 4.0 international license agreement in the same way.
-- Qoreng's Funny match w