鱼C论坛

 找回密码
 立即注册
查看: 693|回复: 5

[已解决]求助!问一个c++课后作业题

[复制链接]
发表于 2020-6-28 06:15:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Write a program that reads in ten whole numbers and that outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the ten numbers just once each and the user can enter them in any order. Your program should not ask the user to enter the positive numbers and the negative numbers separately.
编写一个程序,读取十个整数,并输出所有大于零的数字的总和,小于零的所有数字的总和(将是负数或零)以及所有数字的总和, 正,负或零。 用户仅输入一次十个数字,并且用户可以按任何顺序输入它们。 您的程序不应要求用户分别输入正数和负数。

我要怎么编写系统 让系统自动区分哪些是正数, 哪些是负数呢。如果我为每一个数字定义number1, number2.... number 10, 我就需要每个分辨是正是负, 求大家指点10个数自动区分的方法!
最佳答案
2020-6-28 07:46:03
  1. #include<iostream>
  2. using namespace std;


  3. int main() {
  4.     long long pos = 0, neg = 0, sum = 0, temp;

  5.     for (unsigned char count = 10; count; --count) {
  6.         cin >> temp;
  7.         sum += temp;

  8.         if (temp > 0) {
  9.             pos += temp;
  10.         }
  11.         else {
  12.             neg += temp;
  13.         }
  14.     }

  15.     cout << pos << ' ' << neg << ' ' << sum << endl;
  16.     return 0;
  17. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-28 07:05:30 | 显示全部楼层
if(number > 0)
判断正数

if(number < 0)
判断负数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-28 07:46:03 | 显示全部楼层    本楼为最佳答案   
  1. #include<iostream>
  2. using namespace std;


  3. int main() {
  4.     long long pos = 0, neg = 0, sum = 0, temp;

  5.     for (unsigned char count = 10; count; --count) {
  6.         cin >> temp;
  7.         sum += temp;

  8.         if (temp > 0) {
  9.             pos += temp;
  10.         }
  11.         else {
  12.             neg += temp;
  13.         }
  14.     }

  15.     cout << pos << ' ' << neg << ' ' << sum << endl;
  16.     return 0;
  17. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-28 10:34:39 | 显示全部楼层

太厉害了 我哭了 刚开始学c++。我觉得好多还没学到  想请问下第一行怎么解释呢? long long 代表什么? 为什么 pos neg sum 要为0?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-28 10:41:02 | 显示全部楼层
EherChen 发表于 2020-6-28 10:34
太厉害了 我哭了 刚开始学c++。我觉得好多还没学到  想请问下第一行怎么解释呢? long long  ...

long long,长整数
为什么要为0?平常求和都是从 0 开始累加的吧。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-28 11:04:38 | 显示全部楼层
永恒的蓝色梦想 发表于 2020-6-28 10:41
long long,长整数
为什么要为0?平常求和都是从 0 开始累加的吧。

原来是这样 学习了!!!感谢!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-3 04:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表