acwing 712 正数 C++循环得到输入的以及获取数组长度

712. 正数

输入 66 个数字,它们要么是正数,要么是负数。

请你统计并输出正数的个数。

输入格式

六个数字,每个占一行。

输出格式

输出格式为 x positive numbers,其中 xx 为正数的个数。

数据范围

输入数字的绝对值不超过 100100。

输入样例:

 7
 -5
 6
 -3.4
 4.6
 12

输出样例:

4 positive numbers

 

代码

 #include <iostream>
 using namespace std;
 ​
 int main()
 {
     double a[6];
     int cnt = 0;
     int n = sizeof(a) / sizeof(a[0]);
     for (int i = 0; i < n; i++) {
         cin >> a[i];
         if (a[i] > 0) {
             cnt++;
         }
     }
     cout << cnt << " positive numbers" << endl;
     return 0;
 }

收获点

  • c++获取数组长度 ​ ​a = sizeof(a) / sizeof(a[0]);

  • 可以定义一个数组,然后在循环里通过​ cin >> a[i]获取

Guess you like

Origin blog.csdn.net/qq_41688840/article/details/117698988