A+B: Difference between revisions

From Rosetta Code
Content added Content deleted
(Add Factor)
m (Alphabetize)
Line 26: Line 26:


= Solution =
= Solution =
=={{header|Pascal}}==
<lang pascal>
var
a, b: integer;
begin
readln(a, b);
writeln(a + b);
end.
</lang>
Same with input from file <tt>input.txt</tt> and output from file <tt>output.txt</tt>.
<lang pascal>
var
a, b: integer;
begin
reset(input, 'input.txt');
rewrite(output, 'output.txt');
readln(a, b);
writeln(a + b);
close(input);
close(output);
end.
</lang>


=={{header|C}}==
=={{header|C}}==
Line 102: Line 80:
return 0;
return 0;
}
}
</lang>

=={{header|C_sharp|C#}}==
<lang csharp>
using System.IO;

class plus {
public static void Main(string[] args) {
int a,b;
{
StreamReader reader = new StreamReader("plus.in");
a = int.Parse(reader.ReadLine());
b = int.Parse(reader.ReadLine());
StreamWriter writer = new StreamWriter("plus.out");
writer.WriteLine(a+b);
writer.Close();
}
}
}
</lang>

=={{header|Factor}}==
<lang factor>: a+b ( -- )
readln " " split1
[ string>number ] bi@ +
number>string print ;</lang>

( scratchpad ) a+b
2 2
4

=={{header|Haskell}}==
<lang text>
import Control.Monad

main = liftM2 (+) readLn readLn >>= print
</lang>
</lang>


=={{header|Java}}==
=={{header|Java}}==
<lang java>
<lang java>import java.util.*;
import java.util.*;


public class Sum2 {
public class Sum2 {
Line 113: Line 126:
System.out.println(in.nextInt() + in.nextInt()); // Standard output
System.out.println(in.nextInt() + in.nextInt()); // Standard output
}
}
}</lang>
}
</lang>
Object of [[class]] Scanner works slow enough, because of that contestants prefer to avoid its use. Often, longer solution works faster and easily scales to problems.
Object of [[class]] Scanner works slow enough, because of that contestants prefer to avoid its use. Often, longer solution works faster and easily scales to problems.


<lang java>
<lang java>import java.io.*;
import java.io.*;
import java.util.*;
import java.util.*;


Line 168: Line 179:
}</lang>
}</lang>


=={{header|C_sharp|C#}}==
=={{header|Pascal}}==
<lang csharp>
<lang pascal>
var
using System.IO;
a, b: integer;

begin
class plus {
readln(a, b);
public static void Main(string[] args) {
writeln(a + b);
int a,b;
end.
{
</lang>
StreamReader reader = new StreamReader("plus.in");
Same with input from file <tt>input.txt</tt> and output from file <tt>output.txt</tt>.
a = int.Parse(reader.ReadLine());
<lang pascal>
b = int.Parse(reader.ReadLine());
var
StreamWriter writer = new StreamWriter("plus.out");
a, b: integer;
writer.WriteLine(a+b);
begin
writer.Close();
reset(input, 'input.txt');
}
rewrite(output, 'output.txt');
}
readln(a, b);
}
writeln(a + b);
close(input);
close(output);
end.
</lang>
</lang>

=={{header|Factor}}==
<lang factor>: a+b ( -- )
readln " " split1
[ string>number ] bi@ +
number>string print ;</lang>

( scratchpad ) a+b
2 2
4


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Line 219: Line 224:
r = fin.readline().split()
r = fin.readline().split()
fout.write(str(int(r[0]) + int(r[1])))
fout.write(str(int(r[0]) + int(r[1])))
</lang>

=={{header|Haskell}}==
<lang text>
import Control.Monad

main = liftM2 (+) readLn readLn >>= print
</lang>
</lang>



Revision as of 19:29, 8 April 2010

Task
A+B
You are encouraged to solve this task according to the task description, using any language you may know.

A+B - in programming contests, classic problem, which is given so contestants can gain familiarity with online judging system being used.

A+B is one of few problems on contests, which traditionally lacks fabula.

Problem statement

Given 2 integer numbers, A and B. One needs to find their sum.

Input data

In input stream, two integer numbers are written, separated by space.

Output data

In output, should be one integer be written: sum of A and B.

Example

Input Output
2 2 4
3 2 5

Solution

C

<lang c> // Standard input-output streams

  1. include <stdio.h>

main() {

  int a, b;
  scanf("%d%d", &a, &b);
  printf("%d\n", a + b);

} </lang> <lang c> // Input file: input.txt // Output file: output.txt

  1. include <stdio.h>

int main() {

  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
  int a, b;
  scanf("%d%d", &a, &b);
  printf("%d\n", a + b);
  return 0;

} </lang>

C++

<lang cpp> // Standard input-output streams

  1. include <iostream>

using namespace std; int main() {

  int a, b;
  cin >> a >> b;
  cout << a + b << endl;
  return 0;

} </lang> <lang cpp> // Input file: input.txt // Output file: output.txt

  1. include <fstream>

using namespace std; int main() {

  ifstream in("input.txt");
  ofstream out("output.txt");
  int a, b;
  in >> a >> b;
  out << a + b << endl;
  return 0;

} </lang>

C_sharp

<lang csharp> using System.IO;

class plus { public static void Main(string[] args) { int a,b; { StreamReader reader = new StreamReader("plus.in"); a = int.Parse(reader.ReadLine()); b = int.Parse(reader.ReadLine()); StreamWriter writer = new StreamWriter("plus.out"); writer.WriteLine(a+b); writer.Close(); } } } </lang>

Factor

<lang factor>: a+b ( -- )

   readln " " split1
   [ string>number ] bi@ +
   number>string print ;</lang>
( scratchpad ) a+b
2 2
4

Haskell

<lang text> import Control.Monad

main = liftM2 (+) readLn readLn >>= print </lang>

Java

<lang java>import java.util.*;

public class Sum2 {

  public static void main(String[] args) {
     Scanner in = new Scanner(System.in); // Standard input
     System.out.println(in.nextInt() + in.nextInt()); // Standard output
  }

}</lang> Object of class Scanner works slow enough, because of that contestants prefer to avoid its use. Often, longer solution works faster and easily scales to problems.

<lang java>import java.io.*; import java.util.*;

public class SumDif {

  StreamTokenizer in;
  PrintWriter out;
  public static void main(String[] args) throws IOException {
     new SumDif().run();
  }
  private int nextInt() throws IOException {
     in.nextToken();
     return (int)in.nval;
  }
  public void run() throws IOException {
     in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); // Standard input
     out = new PrintWriter(new OutputStreamWriter(System.out)); // Standard output
     solve();
     out.flush();
  }
  private void solve() throws IOException {
     out.println(nextInt() + nextInt());
  }

} </lang>

<lang java>import java.io.*;

public class AplusB { public static void main(String[] args) { try { StreamTokenizer in = new StreamTokenizer(new FileReader("input.txt")); in.nextToken(); int a = (int) in.nval; in.nextToken(); int b = (int) in.nval; FileWriter outFile = new FileWriter("output.txt"); outFile.write(Integer.toString(a + b)); outFile.close(); } catch (IOException e) { System.out.println("IO error"); } } }</lang>

Pascal

<lang pascal> var

  a, b: integer;

begin

  readln(a, b);
  writeln(a + b);

end. </lang> Same with input from file input.txt and output from file output.txt. <lang pascal> var

  a, b: integer;

begin

  reset(input, 'input.txt');
  rewrite(output, 'output.txt');
  readln(a, b);
  writeln(a + b);
  close(input);
  close(output);

end. </lang>

PicoLisp

<lang PicoLisp>(+ (read) (read)) 3 4 -> 7</lang>

Scheme

<lang scheme> (write (+ (read) (read))) </lang>

Python

Console

<lang python> r = raw_input().split() print int(r[0]) + int(r[1]) </lang>

File

<lang python> fin = open("input.txt", "r") fout = open("output.txt","w") r = fin.readline().split() fout.write(str(int(r[0]) + int(r[1]))) </lang>

Ruby

<lang ruby> puts gets.split(/\s+/).map{|x| x.to_i}.inject{|sum, x| sum + x} </lang>