题目
输入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请解答四个全等的直角三角形与中间的小正方形拼成一个大正方形,
- 2凹的部首是什么
- 3line powered是什么意思?
- 4用反证法证明,若a的3次方+b的3次方=2,求证a+b小于等于2
- 5举出生活中的是例来说明分子在永不停息地运动.
- 6如图,在△ABC中,AB=AC.M、N分别是AB、AC的中点,D、E为BC上的点,连接DN、EM.若AB=13cm,BC=10cm,DE=5cm,则图中阴影部分的面积为_cm2.
- 7用分数解答(用汉字解答:如二分之一) 1:小红家住6楼,她每次从一楼走到家中大概需要两分钟.小红平均走
- 8人教版四年级下册14课小英雄雨来给6个部分加小标题 急
- 9如图,BD平分∠ABC,BE分∠ABC分2:5两部分,∠DBE=21°,求∠ABC的度数.
- 10一道反比例函数和一次函数的题
热门考点
- 1Do you know that boy_____(穿)a green coat
- 2are my not these cousins.(连词成句)
- 3100滴水重5克,照这样催算1亿滴水重多少
- 4一个正方形平均分成七分怎么分
- 5为什么物体在光滑斜面上做功等于物体克服重力做功
- 6有一块直角三角形的绿地,量得两直角边长分别为6m,8m.现在要将绿地扩充成等腰三角形,且扩充部分是以8m为直角边的直角三角形,求扩充后等腰三角形绿地的周长.(图2,图3备用)
- 7用两根长度均为lcm的绳子分别围成一个正方形和一个圆.
- 8几种微粒具有相同的核电荷数,则可说明( ) A.可能属于同一种元素 B.一定是同一种元素 C.彼此之间一定是同位素 D.核外电子个数一定相等
- 916和72的最小公倍数是
- 10把这句话扩句:“大海里,波浪翻滚.”