PTA B1012 数字分类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fuckingone/article/details/82426306

1 .前两天做题发现if和If,if和else,代码执行效率不一样。又百度了一下else if 。也能减少运行时间。但是注意逻辑。

if和else if  。

if (条件一) {执行方法一}
else if(条件二){执行方法二} //这个只有在条件一不满足时才会判断条件二, 若条件一满足,则这句else if直接跳过了


if 和if 。
if (条件一) {执行方法一}
if(条件二){执行方法二} //这个不管条件一满不满足,条件二都会再判断一次,也就是说这句if 一直会执行

2.开始时有答案过不去,题意没理解好。标注在下面代码中。

3.开始时用字符串存数据,再循环比较。这样又浪费内存了。直接用temp比较可以。

#include<cstdio>
using namespace std;
int main(){
  int n,temp;
  int count[5]={0};
  int ans[5]={0};
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d",&temp);
    if(temp%5==0){          //不能写成temp%5==0&&temp%2==0,下面的else if会受影响
      if(temp%2==0){
        ans[0]+=temp; 
        count[0]++;          //count计算的是又是偶数又是5倍数
      }
    }
    else if(temp%5==1){
      count[1]++;
      if(count[1]%2==1){
        ans[1]+=temp;
      }
      else{
        ans[1]-=temp;
      }
      
    }
    else if(temp%5==2){
      count[2]++;
    }
    else if(temp%5==3){
      count[3]++;
      ans[3]+=temp;
    }
    else{
      count[4]++;
      if(temp>ans[4]){
        ans[4]=temp;
      }
    }
  }
  if(count[0]==0)printf("N ");
  else printf("%d ",ans[0]);
  if(count[1]==0)printf("N ");
  else printf("%d ",ans[1]);
  if(count[2]==0)printf("N ");
  else printf("%d ",count[2]);
  if(count[3]==0)printf("N ");
  else printf("%.1f ",(double)ans[3]/count[3]);
  if(count[4]==0)printf("N");
  else printf("%d",ans[4]);
}

猜你喜欢

转载自blog.csdn.net/fuckingone/article/details/82426306