Benutzer-Werkzeuge

Webseiten-Werkzeuge


public:wettbewerbe:sum

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen gezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
Letzte Überarbeitung Beide Seiten der Revision
public:wettbewerbe:sum [2010-02-09 13:16]
moritz
public:wettbewerbe:sum [2013-01-21 09:47]
daniel [Output]
Zeile 1: Zeile 1:
 ====== Sum ====== ====== Sum ======
  
-Your task is to write program that sums up integers. Every integer ​is in the range [-1000,​1000]. There are at most 1000 integers.+Given sequence of n (0 < n < 1001) integer ​numbers a_1 to a_n with 0 < a_i <100 
 +compute ​the sum of all the a_i. 
  
 ===== Input ===== ===== Input =====
  
- +The first line contains ​a single ​integer nThe next n each contain a single integer 
-The input consists of one line containing all integers seperated by a single ​space.+representing the a_i
  
 ===== Output ===== ===== Output =====
  
-The output contains only the sum. +Output ​the sum followed by a newline character.
 ===== Sample Input ===== ===== Sample Input =====
  
 <​code>​ <​code>​
-52 1000 -9 323 -1000 -325+
 +1 
 +
 +42
 </​code>​ </​code>​
  
Zeile 21: Zeile 25:
  
 <​code>​ <​code>​
-42+45
 </​code>​ </​code>​
 +===== Beispielprogramme =====
  
-===== Java Code =====+=== Quellcode in C === 
 +<code c> 
 +#include <​stdio.h>​ 
 +  
 +#define MAX_ARRAY_SIZE 1000 
 +  
 +int array[MAX_ARRAY_SIZE];​ 
 +int array_size;​ 
 +  
 +int main(){ 
 +    int i,sum; 
 +    // Mit scanf kann man einzelne ints einlesen. 
 +    scanf("​%d",​ &​array_size);​
  
-**Compiler-Aufruf:​** ''​javac YourClass.java''​\\ +    for(i=0; i<array_size++i){ 
-**Ausführung:​** ''​java -Xmx300m -Xms64m $MAINCLASS''​ +        // Liesst das naechste int ein (und ueberspringt dabei 
-<code> +        // alle Leerzeichen,​ Tabs und Zeilenumbrueche)Das int 
-import java.io.*; +        // wir an die i-te Stelle des Arrays gespeichert
-import java.math.*;​ +        scanf("​%d",​ array + i); 
-import java.text.*;​ +    } 
-import java.util.*;​ +  
- +    // Nun verwenden wir die eingelesenen Daten 
-public class Main { +    sum = 0; 
- +    for(i=0; i<array_size; ++i){ 
-static public void main(String[]args){ +        sum += array[i]; 
- String line = readLine(); +    
- String[] num_as_str = line.split("​\\s+"​);​ +  
- +    // Mit printf kann man ints und Zeilenumbrueche ausgeben
- // Ein Array ist nicht wirklich notwendigEs geht nur +    ​printf("​%d\n",​ sum); 
- // darum die Syntax zu zeigen+  
- int[] num = new int[num_as_str.length];​ +    return ​0;
- for(int i=0; i<​num.length;​ ++i+
- num[i] = Integer.parseInt(num_as_str[i]); +
- +
- int sum = 0; +
- for(int i=0; i<num.length; ++i) +
- sum += num[i]; +
- +
- writeLine(Integer.toString(sum));​ +
-  +
- System.exit(0);​ +
- +
- +
- /* Ab hier: Hilfsfunktionen */ +
- +
- static BufferedReader readLineHelper = new BufferedReader(new InputStreamReader(System.in)); +
-  +
- public static String readLine() +
- String s = null+
- try { +
- s = readLineHelper.readLine();​ +
- if (s != null) +
- s = s.trim (); +
- } catch (IOException ioe) { +
- s = null; +
- } +
- return ​s; +
-+
- +
- public static void writeLine (String s) { +
- System.out.println(s);​ +
- }+
 } }
 </​code>​ </​code>​
  
-===== C++ Code ===== +=== Quellcode in C++ === 
-**Compiler-Aufruf:​** ''​g++ -O2 -Wall -static -o program yourfile.cpp''​+<code c++
 +#include <​iostream>​
  
-<​code>​ 
-#include <​iostream>​ 
 using namespace std; using namespace std;
  
-// Ein Array ist nicht wirklich notwendig. Es geht nur +const int MAX_ARRAY_SIZE = 1000;· 
-// darum die Syntax ​zu zeigen+ 
-int num_count+// Arrays global anlegen um Stapeluberlaeufe ​zu vermeiden
-int num[1000];+int array[MAX_ARRAY_SIZE]
 +int array_size;
  
 int main(){ int main(){
- num_count ​= 0; +    // Mit cin kann man einzelne ints einlesen. 
- while(cin>>​num[num_count]+    cin >> array_size;​ 
- ++num_count;+    for(int i=0; i<​array_size;​ ++i){ 
 +        // Liesst das naechste int ein (und ueberspringt dabei 
 +        // alle Leerzeichen,​ Tabs und Zeilenumbrueche). Das int 
 +        // wir an die i-te Stelle des Arrays gespeichert. 
 +        ​cin >> ​array[i]; 
 +    }
  
- int sum = 0; +    // Nun verwenden wir die eingelesenen Daten 
- for(int i=0; i<num_count; ++i) +    ​int sum = 0; 
- sum += num[i];+    for(int i=0; i<array_size; ++i){ 
 +        sum += array[i]; 
 +    }
  
- cout<<​sum<<​endl;​ +    // Mit cout kann man ints und Zeilenumbrueche (endl) ausgeben. 
- return 0;+    ​cout << sum << endl; 
 + 
 +    ​return 0;
 } }
 +
 </​code>​ </​code>​
  
-===== C code ===== 
-**Compiler-Aufruf:​** ''​gcc -O2 -Wall -static -o program yourfile.c''​ 
-<​code>​ 
-#include <​stdio.h>​ 
  
-// Ein Array ist nicht wirklich notwendig. Es geht nur +=== Quellcode in Java === 
-// darum die Syntax zu zeigen. +<code java> 
-int num_count;​ +import java.util.Scanner;
-int num[1000];+
  
-int main(){ +class Main { 
- int i, sum  ​ +    public static void main(String[] args) { 
-  +        ​Scanner s = new Scanner(System.in)
- num_count ​0+         
- while(scanf("​%d",​ &num[num_count]) > 0)  +        int array_size ​s.nextInt()
- ++num_count;+        int[] array = new int[array_size];​ 
 +        ​for(int i=0; i<​array_size; ​++i){ 
 +            array[i] = s.nextInt(); 
 +        }
  
- sum = 0; +        int sum = 0; 
- for(i = 0; i<num_count; ++i) +        for(int i=0; i<array_size; ++i){ 
- sum += num[i];+            sum += array[i]; 
 +        }
  
- printf("​%d\n", ​sum); +        System.out.println(sum); 
- return 0;+    }
 } }
 +
 </​code>​ </​code>​
- 
- 
- 
- 
public/wettbewerbe/sum.txt · Zuletzt geändert: 2013-01-21 09:48 von daniel