国庆大礼包题解

国庆大礼包题解

(ps:bits/stdc++.h是万能头文件,这个头文件里面包括了其他全部头文件,yyds部分编译环境可能不支持)

A-比大小

输入两个数,比较两个数的大小,输出较大的数。

#include <bits/stdc++.h>

using namespace std;

int main() {
    int a,b;
    scanf("%d%d",&a,&b);
    a=a>b?a:b;
    //if (a <= b) a=b; 与上一行等价
    printf("%d",a);
    return 0;
}

B-算面积

输入矩形的长和宽,输出矩形的周长和面积。

#include <bits/stdc++.h>

using namespace std;

int main() {
    int a,b;
    int l,s;
    scanf("%d%d",&a,&b);
    l=(a+b)*2;
    s=a*b;
    printf("%d %d",l,s);
    return 0;
}

C-求函数

输入一个x,输出x * x + x * 2 +5。

#include <bits/stdc++.h>

using namespace std;

int main() {
    int x;
    int ans;
    scanf("%d",&x);
    ans=x*x+x*2+5;
    printf("%d",ans);
    return 0;
}

D-判正负

输入两个数a,b,a+b < 0输出negative;

0 <= a+b <= 3输出YES

4 <= a+b输出NO

#include <bits/stdc++.h>

using namespace std;

int main() {
    int a,b;
    scanf("%d %d",&a,&b);
    a=a+b;
    if (a < 0) printf("negative
");
    else if (a >= 4) printf("NO
");
    else printf("YES
");
    return 0;
}

E-装苹果

如果a是b的倍数,输出a/b;

否则需要多一个篮子,输出a/b+1;

#include <bits/stdc++.h>

using namespace std;

int main() {
    int a,b;
    int ans=0;
    scanf("%d %d",&a,&b);
    ans=a/b;
    if (a%b) ans++;//如果a不是b的倍数
    printf("%d",ans);
    return 0;
}

F-数人数

容斥原理,参加了语文小组或者数学小组的总共a+b-c人

都没参加的就是n-(a+b-c)

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n,a,b,c;
    int ans=0;
    scanf("%d %d %d %d",&n,&a,&b,&c);
    ans=n-(a+b-c);
    printf("%d",ans);
    return 0;
}

G-关灯泡

1的因数 1 灯亮

2的因数 1,2

3的因数 1,3

4的因数 1,2,4 灯亮

6的因数 1,2,3,6

.......

观察可以发现,i的因数为奇数的时候灯亮

就是i为完全平方数的时候

题目就变成求n内完全平方数的个数,就是sqrt(n)

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    scanf("%d",&n);
    n=sqrt(n);
    printf("%d
",n);
    return 0;
}

H-约分数

递归gcd函数求最大公约数

#include <bits/stdc++.h>

using namespace std;

int gcd(int a,int b) {
    if (b == 0) return a;
    else return gcd(b,a%b);
}


int main() {
    int a,b,c;
    scanf("%d %d",&a,&b);
    c=gcd(a,b);
    printf("%d %d
",a/c,b/c);
    return 0;
}

I-判相交

为什么要挂这道题

分为三种情况:

1.三个点都在圆内部,不相交。

2.三个点都在圆外部,判断三个线段是否与圆相交。

3.剩下的情况都相交。

点到圆心的距离和半径比较,判断是否在圆的内部。

第二种情况,对于每一条线段,先判断直线和圆是否相交,再判断以两个点为端点的线段是否和圆相交。

#include <bits/stdc++.h>

using namespace std;

struct Point {//用来储存点结构
    double x,y;
};

double dis(Point a,Point b) {//计算两点间距离
    double d;
    d=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    return d;
}

int fun(Point p1,Point p2,Point o,double r) {//判断线段和圆是否相交
    double a,b,c,dist1,dist2,angle1,angle2; // ax + by + c = 0;
    if (p1.x == p2.x) {//特殊情况判断,分母不能为零
        a=1;b=0;c=-p1.x;
    }
    else if (p1.y == p2.y) {//特殊情况判断,分母不能为零
        a=0;b=1;c=-p1.y;
    }
    else {
        a=p1.y-p2.y;
        b=p2.x-p1.x;
        c=p1.x*p2.y-p1.y*p2.x;
    }
    dist1=a*o.x+b*o.y+c;
    dist1*=dist1;
    dist2=(a*a+b*b)*r*r;
    //点到直线距离大于半径r
    if (dist1 > dist2) return 0;
    //判断以两个点为端点的线段是否和圆相交
    angle1=(o.x-p1.x)*(p2.x-p1.x)+(o.y-p1.y)*(p2.y-p1.y);
    angle2=(o.x-p2.x)*(p1.x-p2.x)+(o.y-p2.y)*(p1.y-p2.y);
    if (angle1 > 0 && angle2 > 0) return 1;
    return 0;
}

int main() {
    int t;
    scanf("%d",&t);
    while (t--) {
        Point o,p[5];
        double r;
        scanf("%lf%lf%lf",&o.x,&o.y,&r);
        for (int i=1; i <= 3; ++i) {
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        if (dis(p[1], o) < r && dis(p[2], o) < r && dis(p[3], o) < r) printf("No
");//三点都在圆内
        else if (dis(p[1], o) > r && dis(p[2], o) > r && dis(p[3], o) > r) {//三点都在圆外
            int flag=0;
            for (int i=1; i <= 3; ++i) {
                flag=fun(p[i],p[i%3+1],o,r);
                if (flag == 1) break;
            }
            if (flag) printf("Yes
");
            else printf("No
");
        }
        else printf("Yes
");//其他情况全相交
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Cyber-lxh/p/13795553.html