Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung Nächste Überarbeitung Beide Seiten der Revision | ||
public:wettbewerbe:sum [2011-01-28 13:53] simon |
public:wettbewerbe:sum [2011-01-28 14:02] simon |
||
---|---|---|---|
Zeile 28: | Zeile 28: | ||
45 | 45 | ||
</code> | </code> | ||
+ | ===== Beispielprogramme ===== | ||
- | ===== Java Code ===== | + | === Quellcode in C === |
- | + | <code c> | |
- | **Compiler-Aufruf:** ''javac Main.java''\\ | + | #include <stdio.h> |
- | **Ausführung:** ''java Main'' | + | |
- | <code> | + | #define MAX_ARRAY_SIZE 1000 |
- | import java.util.Scanner; | + | |
- | + | int array[MAX_ARRAY_SIZE]; | |
- | class Main { | + | int array_size; |
- | public static void main(String[]args){ | + | |
- | Scanner s = new Scanner(System.in); | + | int main(){ |
- | + | int i,sum; | |
- | int[] num = new int[1000]; | + | // Mit scanf kann man einzelne ints einlesen. |
+ | scanf("%d", &array_size); | ||
- | + | for(i=0; i<array_size; ++i){ | |
- | for(int i = 0; s.hasNextInt(); ++i) | + | // Liesst das naechste int ein (und ueberspringt dabei |
- | num[i] = s.nextInt(); | + | // alle Leerzeichen, Tabs und Zeilenumbrueche). Das int |
- | + | // wir an die i-te Stelle des Arrays gespeichert. | |
- | int sum = 0; | + | scanf("%d", array + i); |
- | for(int i=0; i<num.length; ++i) | + | } |
- | sum += num[i]; | + | |
- | + | // Nun verwenden wir die eingelesenen Daten | |
- | System.out.println(sum); | + | sum = 0; |
- | } | + | for(i=0; i<array_size; ++i){ |
+ | sum += array[i]; | ||
+ | } | ||
+ | |||
+ | // Mit printf kann man ints und Zeilenumbrueche ausgeben. | ||
+ | printf("%d\n", sum); | ||
+ | |||
+ | return 0; | ||
} | } | ||
</code> | </code> | ||
- | ===== C++ Code ===== | + | === Quellcode in C++ === |
- | **Compiler-Aufruf:** ''g++ -O2 -Wall yourfile.cpp''\\ | + | <code c++> |
- | **Ausführung:** ''./a.out'' | + | |
- | + | ||
- | <code> | + | |
#include <iostream> | #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 -lm -O2 -Wall yourfile.cpp''\\ | + | === Quellcode in Java === |
- | **Ausführung:** ''./a.out'' | + | <code java> |
- | <code> | + | import java.util.Scanner; |
- | #include <stdio.h> | + | |
- | // Ein Array ist nicht wirklich notwendig. Es geht nur | + | class Main { |
- | // darum die Syntax zu zeigen. | + | public static void main(String[] args) { |
- | int num_count; | + | Scanner s = new Scanner(System.in); |
- | int num[1000]; | + | |
+ | int array_size = s.nextInt(); | ||
+ | int[] array = new int[array_size]; | ||
+ | for(int i=0; i<array_size; ++i){ | ||
+ | array[i] = s.nextInt(); | ||
+ | } | ||
- | int main(){ | + | int sum = 0; |
- | int i, sum; | + | for(int i=0; i<array_size; ++i){ |
- | + | sum += array[i]; | |
- | num_count = 0; | + | } |
- | while(scanf("%d", &num[num_count]) > 0) | + | |
- | ++num_count; | + | |
- | sum = 0; | + | System.out.println(sum); |
- | for(i = 0; i<num_count; ++i) | + | } |
- | sum += num[i]; | + | |
- | + | ||
- | printf("%d\n", sum); | + | |
- | return 0; | + | |
} | } | ||
- | </code> | ||
- | |||
- | |||
- | |||
+ | </code> |