给出两个不大于100的正整数a和b,要你输出a+b的值。
下面将展示出几种不同编程语言下本题的实现代码:
C语言源代码
#include <stdio.h> #include <stdlib.h> int main () { int a, b,sum; while (scanf ("%d %d", &a, &b)) { if (a == 0 && b == 0) { break; } // ---- if ! (a == 0 && b == 0), the program will not quit. sum = a + b; printf ("%d\n", sum); } return 0; }
C++语言源代码
#include <iostream> using namespace std; int main () { int a, b; while (cin >> a >> b) { if (a == 0 && b == 0) { break; } // ---- if ! (a == 0 && b == 0), the program will not quit. int sum = a + b; cout << sum << endl; } return 0; }
Java语言源代码
import java.util.*; public class Main { public static void main (String[] args) { Scanner scanner = new Scanner(System.in); int a, b; a = scanner.nextInt(); b = scanner.nextInt(); while (!(a == 0 && b == 0)) { int sum; sum = a + b; System.out.println (sum); a = scanner.nextInt(); b = scanner.nextInt(); } } }
Python 2 语言源代码
while True: a,b = map(int, raw_input().split()) if a + b == 0: exit(0) print a + b
Python 3 语言源代码
while True: a,b = map(int, input().split()) if a + b == 0: exit(0) print (a + b)