Benutzer-Werkzeuge

Webseiten-Werkzeuge


public:wettbewerbe:sum

Dies ist eine alte Version des Dokuments!


A PCRE internal error occured. This might be caused by a faulty plugin

====== Sum ====== Given a 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 ===== The first line contains a single integer n. The next n each contain a single integer representing the a_i ===== Output ===== Output the sum followed by a newline character. ===== Sample Input ===== <code> 3 1 2 42 </code> ===== Sample Output ===== <code> 45 </code> ===== Beispielprogramme ===== === 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); for(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. scanf("%d", array + i); } // Nun verwenden wir die eingelesenen Daten 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> === Quellcode in C++ === <code c++> #include <iostream> using namespace std; const int MAX_ARRAY_SIZE = 1000;· // Arrays global anlegen um Stapeluberlaeufe zu vermeiden. int array[MAX_ARRAY_SIZE]; int array_size; int main(){ // Mit cin kann man einzelne ints einlesen. cin >> array_size; 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]; } // Nun verwenden wir die eingelesenen Daten int sum = 0; for(int i=0; i<array_size; ++i){ sum += array[i]; } // Mit cout kann man ints und Zeilenumbrueche (endl) ausgeben. cout << sum << endl; return 0; } </code> === Quellcode in Java === <code java> import java.util.Scanner; class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int array_size = s.nextInt(); int[] array = new int[array_size]; for(int i=0; i<array_size; ++i){ array[i] = s.nextInt(); } int sum = 0; for(int i=0; i<array_size; ++i){ sum += array[i]; } System.out.println(sum); } } </code>

public/wettbewerbe/sum.1358758045.txt.gz · Zuletzt geändert: 2013-01-21 09:47 von daniel