本题要求编写程序,计算4个整数的和与平均值。题目保证输入与输出均在整型范围内。
输入格式:
输入在一行中给出4个整数,其间以空格分隔。
输出格式:
在一行中按照格式“Sum = 和; Average = 平均值”顺序输出和与平均值,其中平均值精确到小数点后1位。
输入样例:
1 2 3 4
输出样例:
Sum = 10; Average = 2.5
C语言代码实现:
1 #include <stdio.h> 2 3 int main() 4 { 5 int a1,a2,a3,a4,sum; 6 scanf("%d %d %d %d", &a1, &a2, &a3, &a4); 7 sum = a1 + a2 + a3 + a4; 8 printf("Sum = %d;", sum); 9 printf(" Average = %0.1f\n", sum/4.0); 10 return 0; 11 }
Java代码实现:
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 int a, b, c, d, sum; 6 Scanner s1 = new Scanner(System.in); 7 a = s1.nextInt(); 8 b = s1.nextInt(); 9 c = s1.nextInt(); 10 d = s1.nextInt(); 11 sum = a + b + c + d; 12 System.out.println("Sum = " + sum + "; Average = " + sum / 4.0); 13 } 14 }