NOI题解(1.4编程基础之逻辑表达式与条件分支)

NOI题解(1.4编程基础之逻辑表达式与条件分支)_第1张图片


01:判断数正负

#include "iostream"
#include "math.h"
#include "iomanip"
/*
*/
using namespace std;
int main()
{
    long N;
    cin>>N;
    if(N>0)
    {
        cout<<"positive"<

02:输出绝对值

#include "iostream"
#include "math.h"
#include "iomanip"
/*
*/
using namespace std;
int main()
{
    float N;
    cin>>N;
    if(N>=0)
    {
        cout<

03:奇偶数判断

#include "iostream"
#include "math.h"
#include "iomanip"
/*
*/
using namespace std;
int main()
{
    unsigned int N;
    cin>>N;
    if(N%2==0)
    {
        cout<<"even";
    }else{
        cout<<"odd";
    }
    return 0;
}

04:奇偶ASCII值判断

#include "iostream"
#include "math.h"
#include "iomanip"
/*
*/
using namespace std;
int main()
{
    char N;
    N=getchar();//不能cin,否则没有考虑到空格
    if((N+0)%2==0)
    {
        cout<<"NO";
    }else{
        cout<<"YES";
    }
    return 0;
}

05:整数大小比较

#include "iostream"
#include "math.h"
#include "iomanip"
/*
 对于无符号数,根据占用的位数可以直接计算:
 unsigned short 16位 0~2的16次方-1(即65535)
 unsigned int 16位 0~2的16次方-1(即65535)
 unsigned long 32位 0~2的32次方-1(即4294967295)
 
 对于有符号数,由于0也占用一个位置,导致负数的边界值与正数的边界值不一样:
 short 16位 - 2的(16-1)次方~2的(16-1)次方-1(即-32768~32767)
 int 16位 - 2的(16-1)次方~2的(16-1)次方-1(即-32768~32767)
 long 32位 - 2的(32-1)次方~2的(32-1)次方-1(即-2147483648~2147483647)
 */
using namespace std;
int main()
{
    unsigned long x;
    long y;
    cin>>x>>y;
    long long N=(long long)x-y;
    if (N>0) {
        cout<<">";
    }else if(N==0)
        cout<<"=";
    else
        cout<<"<";
    return 0;
}

06:判断是否为两位数

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    unsigned short x;
    cin>>x;
    if (x>=10&&x<=99) {
        cout<<"1";
    }
    else
        cout<<"0";
    return 0;
}

07:收集瓶盖赢大奖


#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    if(a>=10||b>=20)
        cout<<"1";
    else
        cout<<"0";
    return 0;
}

08:判断一个数能否同时被3和5整除

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    long num;
    cin>>num;
    if(num%3==0&&num%5==0)
        cout<<"YES";
    else
        cout<<"NO";
    return 0;
}

09:判断能否被3,5,7整除

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    long num;
    cin>>num;
    int count=0;
    if(num%3==0)
    {
        cout<<"3";
        count++;
    }
    if(num%5==0)
    {
        if(count)
            cout<<" ";
        cout<<"5";
        count++;
    }
    if(num%7==0)
    {
        if(count)
            cout<<" ";
        cout<<"7";
        count++;
    }
    if(count==0)
        cout<<"n";
    return 0;
}

10:有一门课不及格的学生

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    int math,english;
    cin>>math>>english;
    int count=0;
    if(math<60)
        count++;
    if(english<60)
        count++;
    if(count==1)
        cout<<"1";
    else
        cout<<"0";
    return 0;
}

11:晶晶赴约会

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    int N;
    cin>>N;
    switch (N) {
        case 1:
            cout<<"NO";
            break;
        case 3:
            cout<<"NO";
            break;
        case 5:
            cout<<"NO";
            break;
        default:
            cout<<"YES";
    }
    return 0;
}

12:骑车与走路

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    float N;
    cin>>N;
//    cout<<23+25+N/3.0;
    if(23+27+N/3.0>N/1.2)
        cout<<"Walk";
    else if(23+27+N/3.0

13:分段函数

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    float x,y;
    cin>>x;
    if(x>=0&&x<5)
        y=-x+2.5;
    else if(x>=5&&x<10)
        y=2-1.5*(x-3)*(x-3);
    else if(x>=10&&x<20)
        y=x/2-1.5;
    cout<

14:计算邮资

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    int weight;
    char c;
    int charge;
    cin>>weight>>c;
    if(weight<=1000)
        charge=8;
    else{
        //ceil()向上取整,头文件math.h
        charge=8+ceil(((double)weight-1000)/500)*4;
    }
    if(c=='y')
        charge+=5;
    cout<

15:最大数输出

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    int num[3];
    cin>>num[0]>>num[1]>>num[2];
    int max=num[0];
    for(int i=0;i<3;i++)
    {
        if(max

16:三角形判断

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    if((a+b)>c&&(a+c)>b&&(b+c)>a)
        cout<<"yes";
    else
        cout<<"no";
}

17:判断闰年

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    int num;
    cin>>num;
    if(num%4!=0||((num%100==0)&&(num%400!=0))||(num%3200==0))
    {
        cout<<"N";
    }else{
        cout<<"Y";
    }
}

18:点和正方形的关系

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
int main()
{
    int x,y;
    cin>>x>>y;
    if(x>=-1&&x<=1&&y>=-1&&y<=1)
        cout<<"yes";
    else
        cout<<"no";
    return 0;
}


19:简单计算器

#include "iostream"
using namespace std;
/*
 RuntimeError常见出错的原因可能有以下几种:
 
 1、数组开得太小了,导致访问到了不该访问的内存区域
 
 2、发生除零错误
 
 3、大数组定义在函数内,导致程序栈区耗尽
 
 4、指针用错了,导致访问到不该访问的内存区域
 
 5、还有可能是程序抛出了未接收的异常
 */
int main()
{
    int x,y;
    char c;
    cin>>x>>y>>c;
    if(c=='/'&&y==0)
    {
        cout<<"Divided by zero!";
        return 0;//return 1NOI会runtime error,但是实际上我觉得没有问题
    }
    if(c!='/'&&c!='+'&&c!='-'&&c!='*')
    {
        cout<<"Invalid operator!";
        return 0;
    }
    switch (c) {
        case '+':
            cout<

20:求一元二次方程的根

#include "iostream"
#include "math.h"
#include "iomanip"
using namespace std;
/*
 sqrt函数有三种形式
 
 double sqrt(double x);
 float sqrtf(float x);
 long double sqrtl(long double x);
 
 三种形式的区别只是参数和返回值的精度不同,float精度最低,double较高,long double精度最高
 */
int main()
{
    float a,b,c;
    cin>>a>>b>>c;
    /*cout<4*a*c)
        cout<


21:苹果和虫子2

#include "iostream"
#include "math.h"
using namespace std;
int main()
{
    int n,x,y;
    cin>>n>>x>>y;
    if(y>=0)
    {
        if(y<=n*x)//考虑虫子待的时间很长的情况
            cout<


你可能感兴趣的:(OJ,NOI题解)