Introduction to Algorithmic Competition learning note 2.4.4 input and output in C + +

Source: Internet
Author: User

2.4.3 64-bit integer
Input output long Long in addition to Cin,cout, the printf and scanf statements can also be used, but the corresponding placeholder missing is the peace table associated with the compiler: In Linux, GCC is very agreeable to the use of%lld; in Windows, MinGW gcc and VC6 all need to use%I64D, but VS2008 is%lld.
2.4.4 input and output in C + +
The problem is the classic "a+b" problem: Enter several pairs of integers and output each pair.
The 1th method:

#include <cstdio>//the function is close to the stdio.h in C, but some are different .using namespacestd;intMain () {intA, B;  while(SCANF ("%d%d", &a, &b) = =2) printf ("%d\n", A +b); return 0;}

To use a C language header file in a C + + program, remove the extension. h and precede it with the lowercase c. For example, the new name of stdio.h in C + + is Cstdio. In addition, the first line in//begins with the C + + specific "single-line comment," which can be mixed with the traditional comments in C (/* and */).
It is necessary to note that C + + has a common header file that is used in the language. If you prefer, you can continue to use stdio.h and omit the using namespace STD statement. In fact, many, but not all, C programs can be compiled without modification by the C + + compiler.
The 2nd method:

#include <iostream>usingnamespace  std; int Main () {    int  A, b;      while (Cin >> a >> b)         " \ n " ;     return 0 ;}

The header file iostream contains definitions for the input and output streams.
The 2nd method can also be modified into a file input and output stream. Of course, you can also redirect files with Freopen, but the more orthodox approach is as follows:

#include <fstream>using namespacestd;ifstream Fin ("aplusb.in"); Ofstream Fout ("Aplusb.out");intMain () {intA, B;  while(Fin >> a >>b) Fout<< A + b <<"\ n"; return 0;}

If you want to use CIN and cout again, simply remove the statement for Fin and fout and add two lines to it:

#define fin cin#define Fout cout

Compare efficiency (using clock () and clocks_per_sec)

Introduction to Algorithmic Competition learning note 2.4.4 input and output in C + +

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.