C++_USACO_从文件读出两数,相加输出到另一个文件
#include<iostream> #include<fstream> #include<string> using namespace std; int main(){ ofstream ofs("text.out"); ifstream ifs("text.in"); int a,b; ifs>>a>>b; ofs<<a+b<<endl; return 0; }
上面是C++。文件格式可以是.txt,也可以使用任意的后缀,采用.in后缀生成IN文件,采用.out后缀生成OUT文件。可以用记事本、Notepad++等打开
下面是C。
/* ID: your_id_here LANG: C TASK: test */ #include <stdio.h> main () { FILE *fin = fopen ("test.in", "r"); FILE *fout = fopen ("test.out", "w"); int a, b; fscanf (fin, "%d %d", &a, &b); /* the two input integers */ fprintf (fout, "%d\n", a+b); exit (0); }
下面是JAVA。
/* ID: your_id_here LANG: JAVA TASK: test */ import java.io.*; import java.util.*; class test { public static void main (String [] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster BufferedReader f = new BufferedReader(new FileReader("test.in")); // input file name goes above PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.out"))); // Use StringTokenizer vs. readLine/split -- lots faster StringTokenizer st = new StringTokenizer(f.readLine()); // Get line, break into tokens int i1 = Integer.parseInt(st.nextToken()); // first integer int i2 = Integer.parseInt(st.nextToken()); // second integer out.println(i1+i2); // output result out.close(); // close the output file System.exit(0); // don't omit this! } }