首先输入要输入的整数个数n,然后输入n个整数。输出为n个整数中负数的个数,和所有正整数的平均值,结果保留一位小数。
0即不是正整数,也不是负数,不计入计算。如果没有正数,则平均值为0。
数据范围: ,输入的整数都满足
首先输入一个正整数n,
然后输入n个整数。
输出负数的个数,和所有正整数的平均值。
11 1 2 3 4 5 6 7 8 9 0 -1
1 5.0
3 0 0 0
0 0.0
#include <stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { int num, count1 = 0, count2 = 0; double sum = 0; while (n--) { scanf("%d", &num); if (num < 0) { count1++; } if (num > 0) { sum += num; count2++; } } printf("%d %.1f\n", count1, sum == 0 ? 0 : sum / count2); } return 0; }
#include <stdio.h> int main() { int n,i; int result=0; int countzheng=0; int fushu=0; int num; scanf("%d", &n); for(i=0;i<n;i++){ scanf("%d", &num); if(num<0){ fushu++; }else if(num>0){ countzheng++; result+=num; } } if(countzheng==0){ printf("%d %.1f", fushu, 0.0); }else{ printf("%d %.1f", fushu, (double)result/countzheng); } return 0; }
#include <stdio.h> int main() { int inputCnt; int sum = 0; int zCnt = 0; int oCnt = 0; scanf("%d", &inputCnt); while(inputCnt--){ int temp; scanf("%d", &temp); if(0 == temp){ continue; } if(temp < 0){ oCnt++; }else{ zCnt++; sum += temp; } } printf("%d ", oCnt); if(0 == zCnt){ zCnt = 1; } printf("%.1f\n",(float)sum/zCnt); return 0; }
#include<stdio.h> int main() { int n = 0; int i = 0; int count = 0; int add = 0; double sum = 0; double AVG = 0; scanf("%d", &n); int arr[2000] = { 0 }; for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } for (i = 0; i <= n; i++) { if (arr[i] < 0) count += 1; else if(arr[i]==0); else { add++; sum += arr[i]; AVG = sum / add; } } printf("%d %.1lf", count, AVG); return 0; }
#include <stdio.h> int main() { int n = 0; int i = 0; int arr[2001]={0}; int negative_num = 0;//记录-1的个数 double sum = 0;//正整数的总和 int num = 0;//记录正整数的个数 int zero_num = 0;//记录零的个数 double aver_num = 0;//正整数的平均值 scanf("%d",&n); for(i = 0;i<n;i++) { scanf("%d ",&arr[i]); if(arr[i]>0) { sum+=arr[i]; num++; } else if(arr[i]<0) negative_num++; else zero_num++; } if(zero_num==n) aver_num = 0; else aver_num=sum/num; printf("%d %.1lf",negative_num,aver_num); return 0; }
#include<stdio.h> #include<stdlib.h> int main() { int n = 0; while (scanf("%d", &n) != EOF) { int i = 0; int sum = 0; float ava = 0.0f; int cnt1 = 0;//记录负数 int cnt2 = 0;//记录除0后的数 int cnt3 = 0;//记录0的个数 int* arr = (int*)malloc(n * sizeof(int)); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } for (i = 0; i < n; i++) { if (arr[i] < 0) { cnt1++; } else if (arr[i] > 0) { sum += arr[i]; cnt2++; } else { cnt3++; } } if (cnt3 == n) { printf("%d 0.0", cnt1); } else { ava = (double)sum / cnt2; printf("%d %.1f", cnt1, ava); } free(arr); arr = NULL; } return 0; }
#include <stdio.h> int main() { //输入 int n=0; scanf("%d\n",&n); int arr1[2000]={0}; int i=0; for(i=0;i<n;i++) { scanf("%d\n",&arr1[i]); } //查找 //第一次判断 int *p=&arr1[0]; int count1=0; //标志1 计负数的个数 double count2=0; //标志2 计正数的个数 int count3=0; //标志3 计0的个数 int sum=0; int arr2[]={0}; for(i=0;i<n;i++) { if(*(p+i)<0) { count1++; } //去掉负数 else if(*(p+i)>0) { count2++; sum+=*(p+i); } } //计算 double eve=0; eve=sum/count2; //输出 if(count2==0) { printf("%d 0.0",count1); } else { printf("%d %.1lf",count1,eve); } }
#include<stdio.h> int main() { int n = 0; int temp = 0; int count1 = 0;//累计正数个数 int count2 = 0;//累计负数个数 float sum = 0;//计算正数和的平均值 scanf("%d", &n);//读取输入整数的个数 while (n > 0) { scanf("%d", &temp); if (temp > 0) { sum += temp; count1++; } else if (temp < 0) { count2++; } n--; } printf("%d ", count2); if (count1 == 0) { printf("0.0\n"); } else { printf("%.1f\n", sum / count1); } return 0; }
#include <stdio.h> #define N 2000 int main() { int n,i,j=0,temp,negative=0,positive[N]; float num=0; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&temp); if(temp>0) positive[j++]=temp; else if(temp<0) negative++; } for(i=0;i<j;i++) num+=positive[i]; if(j>0) num/=j; printf("%d %-6.1f\n",negative,num); return 0; }
#include<stdio.h> int main(){ int n,m; while(~scanf("%d",&n)){ int negative=0,positive=0; float sum=0; for(int i=0;i<n;i++){ scanf("%d",&m); if(m<0) negative++; if(m>0){ positive++; sum += m; } } printf("%d ",negative); if(positive==0) printf("0.0\n"); else printf("%.1f\n",sum/positive); } }