信息学奥赛一本通---第三章---程序的控制结构--- 1049-1058 (第二节)题解

124 阅读2分钟

注:此为c++代码

    虽然我的代码无注释,但如想复制,必须完全搞懂代码所对应的题!

Dev c++这东西点进来的都有吧,详情见我的另一篇文章。

正式进入题解部分:          (本人小白,大佬们请勿吐槽)

1049:

#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
    cin>>n;
    if((n==1)||(n==3)||(n==5)) cout<<"NO"<<endl;
    else cout<<"YES"<<endl;
    return 0;
}

1050:

#include<bits/stdc++,h>
using namespace std;
int s;
double bike,walk;
int main()
{
    cin>>s;
    bike=s/3.0+50;
    walk=s/1.2;
    if(bike==walk) cout<<"All"<<endl;
    else if(bike>walk) cout<<"Walk"<<endl;
    else cout<<"Bike"<<endl;
    return 0;
}

1051:

#include<bits/stdc++.h>
using namespace std;
double x,y,t;
int main()
{
    cin>>x;
    t=int(x)/5.0;
    switch(int (t))
    {
    case 0:
        y=(-x)+2.5;
        break;
    case 1:
        y=2-1.5*(x-3)*(x-3);
        break;
    case 2:
    case 3:
        y=x/2-1.5;
        break;
    }
    cout<<setiosflags(ios::fixed)<<setprecision(3)<<y<<endl;
    return 0;
}

1052:

#include<bits/stdc++.h>
using namespace std;
int m,money;
char ch
int main()
{
    cin>>m>>ch;
    if(m>1000) money=8+ceil((m-1000)/500.0)*4;
    else money=8;
    if(ch=='y') money=money+5;
    cout<<money<<endl;
    return 0;
}

1053:

#include<bits/stdc++.h>
using namespace std;
int a,b,c,max;
int main()
{
    cin>>a>>b>>c;
    if(a>b) max=a;
    else max=b;
    if(c>max) max=c;
    cout<<max<<endl;
    return 0;
}

1054:

#include<bits/stdc++.h>
using namespace std;
int a,b,c;
int main()
{
    cin>>a>>b>>c;
    if((a+b>c)&&(a+c>b)&&(b+c>a)) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
    return 0;
}

1055:

#include<bits/stdc++.h>
using namespace std;
int a;
int main()
{
    cin>>a;
    if(a%4==0)
    {
        if(a%100==0)
        {
            if(a%400) cout<<"N"<<endl;
            else cout<<"Y"<<endl;
        }
        else cout<<"Y"<<endl;
    }
    else cout<<"N"<<endl;
    return 0;
}

1056:

#include<bits/stdc++.h>
using namespace std;
double x,y;
int main()
{
    cin>>x>>y;
    if( (x>=-1&&x<=1) && (y>=-1&&y<=1) ) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
    return 0;
}

1057:

#include<bits/stdc++.h>
using namespace std;
int a,b;
char ch;
int main()
{
    cin>>a>>b>>ch;
    if((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/'))
    {
        switch (ch)
        {
            case '+':
                cout<<a+b<<endl;
                break;
            case '-':
                cout<<a-b<<endl;
                break;
            case '*':
                cout<<a*b<<endl;
                break;
            case '/':
            {
                if(b==0) cout<<"Divided by zero!"<<endl;
                else cout<<a/b<<endl;
            }
        }
    }
    else cout<<"Invalid operator!"<<endl;
    return 0;
}

1058:

#include<bits/stdc++.h>
#define precision_1 1e-12
#define precision_2 1e-6
using namespace std;
int main()
{
    double a,b,c,x1,x2,delta;
    cin>>a>>b>>c;
    delta=b*b-4*a*c;
    if(delta<0&&fabs(delta)>precision_1) printf("No answer!\n");
    else if(fabs(delta)<precision_1)
    {
        x1=-b/(2*a);
        if(fabs(x1)<precision_2) printf("x1=x2=%.5lf\n",0);
        else printf("x1=x2=%.5lf\n",x1);
    }
    else
    {
        x1=(-b+sqrt(delta))/(2*a);
        x2=(-b-sqrt(delta))/(2*a);
        if(fabs(x1)<precision_2) x1=fabs(x1);
        if(fabs(x2)<precision_2) x2=fabs(x2);
        if(x1<x2) printf("x1=%.5lf;x2=%.5lf",x1,x2);
        else printf("x1=%.5lf;x2=%.5lf",x2,x1);
    }
    return 0;
}

个人码风,不喜勿喷

明天发:

信息学奥赛一本通---第四章--- 循环结构的程序设计--- 1059-1084 (第二节)题解

阅读世界,共赴山海

423全民读书节,邀你共读