题目
输入n个整数,求输入正数之和,负数之和,并统计相应正数和负数的个数,以输入0表示输入结束.
提问时间:2020-11-13
答案
C版本:
#include
int main(){
int numPos = 0,sumPos = 0,numNeg = 0,sumNeg = 0,in;
printf("Please key in the integers,key in 0 to stop: ");
do{
scanf("%d",&in);
if(in > 0){
numPos++;
sumPos += in;
}
else if(in < 0){
numNeg++;
sumNeg += in;
}
}while(in != 0);
printf("There are %d positive integers,whose sum is %d. ",numPos,sumPos);
printf("There are %d negative integers,whose sum is %d. ",numNeg,sumNeg);
return 0;
}
borland C版本5.5编译通过.
Java版本:
import java.util.Scanner;
public class test{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int numPos = 0,sumPos = 0,numNeg = 0,sumNeg = 0,in;
System.out.println("Please key in the integers,0 to stop");
do{
in = sc.nextInt();
if(in > 0){
numPos++;
sumPos += in;
}
else if(in < 0){
numNeg++;
sumNeg += in;
}
}while(in != 0);
System.out.println("There are " + numPos + " positive integers,whose sum is " + sumPos);
System.out.println("There are " + numNeg + " negative integers,whose sum is " + sumNeg);
}
}
JGrasp 版本1.8.6_10编译通过.
#include
int main(){
int numPos = 0,sumPos = 0,numNeg = 0,sumNeg = 0,in;
printf("Please key in the integers,key in 0 to stop: ");
do{
scanf("%d",&in);
if(in > 0){
numPos++;
sumPos += in;
}
else if(in < 0){
numNeg++;
sumNeg += in;
}
}while(in != 0);
printf("There are %d positive integers,whose sum is %d. ",numPos,sumPos);
printf("There are %d negative integers,whose sum is %d. ",numNeg,sumNeg);
return 0;
}
borland C版本5.5编译通过.
Java版本:
import java.util.Scanner;
public class test{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int numPos = 0,sumPos = 0,numNeg = 0,sumNeg = 0,in;
System.out.println("Please key in the integers,0 to stop");
do{
in = sc.nextInt();
if(in > 0){
numPos++;
sumPos += in;
}
else if(in < 0){
numNeg++;
sumNeg += in;
}
}while(in != 0);
System.out.println("There are " + numPos + " positive integers,whose sum is " + sumPos);
System.out.println("There are " + numNeg + " negative integers,whose sum is " + sumNeg);
}
}
JGrasp 版本1.8.6_10编译通过.
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
最新试题
热门考点
- 1【牢牢的剪纸】姥姥对我期待什么呢?请认真品读后写下来.
- 21.(1/2-1)*(1/3-1)*(1/4-1)*……*(1/20-1)= 2 若-4x(10的平方)=ax(10的n次方),求(a+n)的值
- 3用修改符号修改下面语段的错误
- 4they say,"we saw her in the street" 改为间接引语
- 5屏风隔断的作用有哪些
- 6木兰诗运用了哪些修辞手法
- 71=5 2=10 3=20 4=25 5=?
- 8can you recognize the purse __ on the bookshelf
- 9是 I want to be a teacher who loves his students 还是 I want to be a teacher who love his students
- 10.已知2Mg(s) O2(g)==2MgO(s)的ΔH0=-1203KJmol-1,则MgO== Mg O2的