OJ first experience --- A + B precision control problems // EOF //

1.A + B control problems // EOF

#include <iostream>

using namespace std; // C ++ header files

{

int a,b;

while (cin >> a >> b) // corresponds to the C language while (scanf ( "% d% d", & a, & b)! = EOF)

{

cout << a + b << endl; // output and a + b, endl corresponds to the C language \ n

}

return 0;

}

Wherein the output can be written: cout << "sum =" << a + b << endl; (need to define in advance sum).

Note cin followed by ">>", cout followed by "<<."
-------------------------------------------------- -------------------------------------------------- ----------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------------

2. EOF judgment

 1.while((scanf"%d,%d",&m,&n)==2)

  {

  //...

  }

2.while((scanf"%d,%d",&m,&n)!=EOF)

  {

  //...

  }

3.while(cin>>m>>n)

   {

   //...

   }

 

 

(Scanf ( "% d% d", & n, & m), n + m) - This is a "comma" expression.

--------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------

3. The output control precision

In C ++, there is no format breaks, we can achieve this demand through the use of setprecision () function.

Want to use setprecision () function, you must include the header file #include <iomanip>. Used as follows:

cout << "a=" << setprecision(2) << a <<endl;

At this time, we will find, if a value of 0.20001, the output result is a = 0.2, 0 are omitted second later.

If we want it to automatically fill 0, 0 patch needs to be defined prior to cout. code show as below:

cout.setf(ios::fixed);
cout << "a=" <<fixed<< setprecision(2) << a <<endl; //输出a=0.20

In this way, we can get a 0.20. Of course, if you want to close off the fill 0, only need to be fixed cancel the setting operation.

cout.unsetf(ios::fixed);
cout << "a=" << setprecision(2) << a <<endl; //输出a=0.2

Our output will then change back to a = 0.2 a.

Guess you like

Origin www.cnblogs.com/Begin-Again/p/12616886.html