题目:
题目描述
先输入一个正整数n
接下来输入n个整数,你需要统计出这n个整数中有多少个正数
样例输入
5
1 -2 3 0 4
样例输出
3
为什么我做出来是“2”呢?
代码如下:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int b,s;
int a;
scanf("%d",&a);
scanf("%d",&b);
if(b>0)
s++;
while(b!=0)
{
scanf("%d",&b);
if(b>0)
s++;
}
printf("%d",s);
return 0;
}
哪位能帮我一下,感谢
题主代码修改如下,供参考:
#include<stdio.h>
//#include<bits/stdc++.h>
//using namespace std;
int main()
{
int b,s;
int a;
scanf("%d",&a);
//scanf("%d",&b); 修改
//if(b>0) 修改
//s++; 修改
s = 0; //修改
while(a--)//(b!=0) 修改
{
scanf("%d",&b);
if(b>0)
s++;
}
printf("%d",s);
return 0;
}
s变量没有初始化为0
另外while(b!=0)这个结束条件是你自己想象的吧?题目中根本没有说输入0表示结束啊。明明a表示一共有多少个整数啊,但你输入了a连用的没有用