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 ====== Your task is to write a program that sums up integers. Every integer is in the range [-1000,1000]. There are at most 1000 integers. ===== Input ===== The input consists of one line containing all integers seperated by a single space. ===== Output ===== The output contains only the sum. ===== Sample Input ===== <code> 1 52 1000 -9 323 -1000 -325 </code> ===== Sample Output ===== <code> 42 </code> ===== Java Code ===== **Compiler-Aufruf:** ''javac YourClass.java''\\ **Ausführung:** ''java $MAINCLASS'' <code> import java.util.Scanner; class Main { public static void main(String[]args){ Scanner s = new Scanner(System.in); int[] num = new int[1000]; for(int i = 0; s.hasNextInt(); ++i) num[i] = s.nextInt(); int sum = 0; for(int i=0; i<num.length; ++i) sum += num[i]; System.out.println(sum); } } </code> ===== C++ Code ===== **Compiler-Aufruf:** ''g++ -O2 -Wall -static -o program yourfile.cpp'' <code> #include <iostream> using namespace std; // Ein Array ist nicht wirklich notwendig. Es geht nur // darum die Syntax zu zeigen. int num_count; int num[1000]; int main(){ num_count = 0; while(cin>>num[num_count]) ++num_count; int sum = 0; for(int i=0; i<num_count; ++i) sum += num[i]; cout<<sum<<endl; return 0; } </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 // darum die Syntax zu zeigen. int num_count; int num[1000]; int main(){ int i, sum; num_count = 0; while(scanf("%d", &num[num_count]) > 0) ++num_count; sum = 0; for(i = 0; i<num_count; ++i) sum += num[i]; printf("%d\n", sum); return 0; } </code>

public/wettbewerbe/sum.1265719464.txt.gz · Zuletzt geändert: 2010-02-09 13:44 von ben