rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » C/C++

编程起步 AC-1002

  今天本来1002挺顺手的,大概思路都写出来了,由于是很恨很菜鸟,平时不怎么练习编程,先后摸索多重嵌套格式,保留一位小数,循环输入下不能Break,绝对值的表示等。

  可是最后老是WA,无奈下找到群里的高手帮我看看,群里还真是高手如云一看就发现问题了,还借鉴了别人的思路,发现我的代码写的很没有水平,还不善于用函数,最重要的是太马虎了题中一个条件根本没有审对,求最大我却取平均,还有就是读入多个变量竟然少写了一个%d,再就是注意OJ里面main函数都是int型最后return 0,

 以后还真得多注意细节,细节决定成败啊,这么一个小题都不能成功AC,太惭愧了,都怪我以前不认真练习编程,以后好好加油~~

  附上代码留纪念 感谢那位 清风 耐心指教~~

  #include "stdio.h"

float max_3(int a,int b,int c)//求最值代码  float max=(float)a;
 if(max>b)
  max=(float)b;
 if(max>c)
  max=(float)c;
 return max; int main() //你的是void,改为int     int n,t,g1,g2,g3,gl;
     while(scanf("%d%d%d%d%d%d",&n,&t,&g1,&g2,&g3,&gl)!=EOF)//6个%d,你好像写了5个      float d=0;
     if(g1-g2>=-t&&g1-g2<=t)       d=(float)(g1+g2)/2;
      printf("%.1f\n",d);      }
     else if(g1-g3>=-t&&g1-g3<=t)       if(g2-g3>=-t&&g2-g3<=t)        d=max_3(g1,g2,g3);//这里题目要求是最大值,你的是平均值
       printf("%.1f\n",d);       }
      else        d=(float)(g1+g3)/2;
       printf("%.1f\n",d);       }      }
     else if(g2-g3>=-t&&g2-g3<=t)         d=(float)(g2+g3)/2;
        printf("%.1f\n",d);      }      else       d=(float)gl;
        printf("%.1f\n",d);     }
    return 0;
}

那位清风的代码很有水平 也借鉴&纪念下吧

#include <iostream>
#include <stdio.h>
#include <iomanip>
using namespace std;

bool tolerance(int a,int b,int t)  int remain;
 if(a>=b)
  remain=a-b;
 else
  remain=b-a;
 if(remain<=t)
  return true;
 return false;
}

int main()  bool s1,s2;
 int max;
 float P,T,G1,G2,G3,GJ;
 while(cin>>P>>T>>G1>>G2>>G3>>GJ)   if(tolerance(G1,G2,T))    cout<<setprecision(1)<<setiosflags(ios::fixed | ios::showpoint)<<(G1+G2)/2.0<<endl;
   continue;   s1=tolerance(G1,G3,T);
  s2=tolerance(G2,G3,T);
  if(s1&&s2)    max=G1;
   if(G2>max)
    max=G2;
   if(G3>max)
    max=G3;
   cout<<setprecision(1)<<setiosflags(ios::fixed | ios::showpoint)<<max<<endl;
   continue;   if(!s1&&!s2)    cout<<setprecision(1)<<setiosflags(ios::fixed | ios::showpoint)<<GJ<<endl;
   continue;   else    if(s1)
    cout<<setprecision(1)<<setiosflags(ios::fixed | ios::showpoint)<<(G1+G3)/2.0<<endl;
   else
    cout<<setprecision(1)<<setiosflags(ios::fixed | ios::showpoint)<<(G2+G3)/2.0<<endl;  }
 return 0;
}

顶一下
(0)
踩一下
(0)