• 算法竞赛入门【码蹄集新手村600题】(MT1551-1600)


    算法竞赛入门【码蹄集新手村600题】(MT1551-1600)



    前言

    在这里插入图片描述

    为什么突然想学算法了?

    > 用较为“官方”的语言讲,是因为算法对计算机科学的所有分支都非常重要。 在绝大多数的计算机科学分支领域中,要想完成任何实质性的工作,理解算法的基础知识并掌握与算法密切相关的数据结构知识是必不可少的。
    > 但从实际而言,是因为当下快到了考研和找工作的年纪(ಥ_ಥ),无论走哪一条路,都不免需要一些相对丰富的算法知识,是故,便产生了一个暑假速成算法的计划,可能对于像我这种算法竞赛小白而言,几乎很难,但我仍然还是想尝试一下,毕竟,梦想还是要有的,万一实现了呢?~( ̄▽ ̄~)~

    在这里插入图片描述


    为什么选择码蹄集作为刷题软件?

    码蹄集,是在全国高等学校计算机教学与产业实践资源建设专家委员会(TIPCC) 指导下建设的,其依托全国各大名校计算机系和清华大学出版社等单位的强大资源,旨在为计算机学习爱好者提供全面和权威的计算机习题。
    在这里插入图片描述


    目录

    1. MT1551 找字符串

    (1)题目描述
    有一个字符数组存有多个字符串(一个紧接一个) : i\0love\0my\0mother\0

    编写函数找出每一个字符串,并返回到指针p所指向的指针数组中。

    格式

    输入格式: 无
    .
    输出格式: 多行:每一行为所给字符中以\0结尾的字符串。

    样例1

    输入格式: 无
    .
    输出格式:
    i
    love
    my
    mother

    (2)参考代码

    #include 
    using namespace std;
    int find(char s[],int len, char *p[]){
    char* pos=s;
    int cnt=0;
    for (int i=0; i<len; i++){
    	if (s[i]=='\0' && s[i-1]!='\0') {
    		p[cnt]=pos;
    		cnt++;
    		pos=s+i+1;
    	}
    }
    return cnt;
    }
    int main(){
    	char s[] = "1\0love\0my\0mother\0";
    	int len = sizeof(s);
    	char *p[100];
    	int cnt=find(s, len, p);
    	for (int i=0;i<cnt; i++){
    		printf("%s\n",p[i]);
    }
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    2. MT1552 指针数组

    (1)题目描述
    一个指针数组指向10个字符串常量,用选择排序法对指针数组按字符串排序。

    格式

    输入格式: 十行:每一行都是一个字符串。
    .
    输出格式:十行:每一行是一个字符串。它们按照首字母从小到大的顺序排列(首字母相同取下一个比较)。

    样例1

    输入格式:
    h
    i
    j
    g
    f
    e
    d
    a
    b
    c
    .
    输出格式:
    a
    b
    c
    d
    e
    f
    g
    h
    i
    j

    (2)参考代码

    #include
    using namespace std;
    int main() {
        char s[10][100];
        for (int i = 0; i < 10; i++)
    	    cin.getline(s[i],100);
        char *sorted [10];
        for (int i = 0; i < 10; i++) sorted[i] = s[i];
        for (int i = 0; i < 10; i++) { 
            int pos = i;
    	    for (int j = i; j < 10; j++)
    		    if (strcmp(sorted[pos], sorted[j]) > 0) pos = j;
    	char *t = sorted [pos];
    	sorted [pos] = sorted[i];
    	sorted[i] = t;
    }
    for (int i=0; i<10;i++)
    	printf("%s\n", sorted[i]);
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    3. MT1553 数组遍历

    (1)题目描述
    编写函数void Traverse(void*p,int n,void(*visit)(void *ep)),遍历p所指的数组的每个元素,通过调用函数void visit(void *ep);输出元素。那么设计不同的visit,使之能够实现char、double、int等类型的输出,则调用Traverse函数就可以支持多种类型的数组遍历。其中:

    int a[]={1,2,3,4,5,6,7,8,9};
    double b[]={1.,2.,3.,4.,5.,6.,7.,8.,9.};
    char s[]=" abcdefghi”;

    格式

    输入格式: 无
    .
    输出格式: 三行:一个数组占一行。且相邻元素以空格隔开。b数组要求保留小数点后6位。

    样例1

    输入格式: 无
    .
    输出格式:
    1 2 3 4 5 6 7 8 9
    1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000
    a b c d e f g h i

    (2)参考代码

    #include 
    using namespace std;
    
    void visitint(void *p){
    	printf("%d",*((int *) p));
    }
    
    void visitchar(void *p){
    	printf("%c ", *((char *) p));
    }
    
    void visitdouble(void *p) {
    	printf("%lf", * ((double *) p)) ;
    }
    
    void Traverse(void *p, int n, void(*visit) (void *ep)) {
    	for(int i=0;i<n;i++){
            visit((char*) p + i * (visit == visitchar ? sizeof(char) : (visit ==visitint?sizeof(int):sizeof(double))));
        }
    }
    
    int main() {
    int a[]={1,2,3,4,5,6,7,8,9} ;
    Traverse(a, 9, visitint);
    printf("\n");
    double b[]={1.,2.,3.,4.,5.,6.,7.,8.,9.};
    Traverse(b, 9, visitdouble);
    printf("\n");
    char s[]="abcdefghi";
    Traverse(s, 9, visitchar);
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    4. MT1554 函数指针

    (1)题目描述
    输入两个整数,定义加法函数和减法函数,用指向函数的指针分别指向他们,通过指针调用函数求两数的和与差。

    格式

    输入格式: 输入整型,空格分隔
    .
    输出格式: 输出整型,空格分隔

    样例1

    输入格式: 3 4
    .
    输出格式: 7 -1

    (2)参考代码

    #include 
    int add(int a, int b) {
        return a+b;
    }
    int minus(int a, int b) {
        return a-b;
    }
    
    int main(){
        int a, b;
        scanf ("%d%d", &a, &b) ;
        int (*p) (int a, int b);
        p=add;
        printf("%d ",p(a, b));
        p=minus;
        printf("%d ",p(a, b));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    5. MT1555 返回指针

    (1)题目描述
    编写函数查找一维数组中的某个元素(仅查找第一次出现的即可),并返回该元素的指针(下标,从0开始),主调函数输出该元素的指针,如果没找到,则输出-1。

    格式

    输入格式: 第一行输入数组长度N(<100),第二行输入数组元素,整型,空格分隔,第三行输入要查找的元素M。
    .
    输出格式:输出整型

    样例1

    输入格式:
    8
    1 2 3 4 5 67 89 3
    3
    .
    输出格式: 2

    (2)参考代码

    #include 
    using namespace std;
    
    int getPointer(int a[], int n, int key){
    	for (int i= 0; i < n; i++)
    		if (key == a[i])
    			return i;
    	return -1;
    }
    int main() {
        int a [100], n;
        cin >> n;
        for (int i = 0; i < n; i++)
        cin >> a[i];
        int key;
        cin >> key;
        printf("%d", getPointer(a, n, key)) ;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    6. MT1556 约瑟夫环1

    (1)题目描述
    约瑟夫问题是个著名的问题:N个人围成一圈,第一个人从1开始报数,报M的将被杀掉,下一个人接着从1开始报。如此反复,最后剩下一个,求最后的胜利者。

    格式

    输入格式: 两个整数,N,M
    .
    输出格式: 最后胜利的人的序号

    样例1

    输入格式: 7 3
    .
    输出格式: 4

    (2)参考代码

    #include 
    using namespace std;
    
    typedef struct Node{
    int value;
    Node *next;
    } NODE, *PNODE,*NodeList;
    
    PNODE createNode (int value, PNODE next){
    	PNODE node = (PNODE) malloc(sizeof (NODE)) ;
    	node->value = value;
    	node->next = next;
    	return node;
    }
    int main(){
    	int n, m;
    	scanf("%d %d'", &n, &m) ;
    	NodeList head, last;
    
    	for(int i=0; i<n; ++i) {
    		PNODE node = createNode(i+1, NULL) ;
    		if(i == 0){
    		head = node;
    		last = node;}
     		else{
    			last->next = node;
    			last = node;
    		}
    		if(i==n-1)
    			last->next=head;
    		}
    		
    	int i=1;
    	while(head->next!=head){
    		if(i==m){
    			PNODE p=head;
    			last->next=head->next;
                free(p);
                i=1;
                head=last->next;
    		}
    		else{
    			last=head;
    			head=head->next;
    			i++;
    		}
    	}
    	printf("%d",head->value);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    7. MT1557 约瑟夫环2

    (1)题目描述
    约瑟夫问题是个著名的问题:N个人围成一圈,第一个人从1开始报数,报M的将被杀掉,下一个人接着从1开始报。如此反复,最后剩下一个,求按顺序退出的人的序号序列。

    格式

    输入格式: 两个整数,N,M
    .
    输出格式: 按顺序退出的人的序号序列。

    样例1

    输入格式: 7 3
    .
    输出格式: 3 6 2 7 5 1 4

    (2)参考代码

    #include 
    using namespace std;
    
    typedef struct Node{
    	int value;
    	Node *next;
    } NODE, *PNODE,*NodeList;
    
    PNODE createNode (int value, PNODE next){
    	PNODE node = (PNODE) malloc(sizeof (NODE)) ;
    	node->value = value;
    	node->next = next;
    	return node;
    }
    int main(){
    	int n, m;
    	scanf("%d %d'", &n, &m) ;
    	NodeList head, last;
    
    	for(int i=0; i<n; ++i) {
    		PNODE node = createNode(i+1, NULL) ;
    		if(i == 0){
    		head = node;
    		last = node;}
     		else{
    			last->next = node;
    			last = node;
    		}
    		if(i==n-1)
    			last->next=head;
    		}
    		
    	int i=1;
    	while(head->next!=head){
    		if(i==m){
    			PNODE p=head;
    			last->next=head->next;
    			printf("%d ",head->value);
    			free(p);
    			i = 1;
    			head = last->next;
    		}
    		else{
    			last=head;
    			head=head->next;
    			i++;
    		}
    	}
    	printf("%d ",head->value);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    8. MT1558 新生命

    (1)题目描述
    狼群新生了一只尊贵的艾尔法狼,请设计一个结构体,管理它的信息,信息包括名字,年龄,性别。

    输入艾尔法狼宝宝的信息,然后再输出他的信息。

    格式

    输入格式: 输入名字性别为字符型,年龄整型
    .
    输出格式: 输出名字性别为字符型,年龄整型

    样例1

    输入格式: King 0 M
    .
    输出格式: King 0 M

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct Wolf{
        string name;
        int age;
        string sex;
    }w[1005];
    
    int main( )
    {
        cin>>w[0].name>>w[0].age>>w[0].sex;
        cout<<w[0].name<<" "<<w[0].age<<" "<<w[0].sex<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    9. MT1559 幼儿园

    (1)题目描述
    幼儿园开学了,请帮老师设计一个结构体,管理宝宝们的信息,信息包括姓名,年龄,性别。

    输入5个宝宝的信息,然后再输出他们的信息。

    格式

    输入格式: 输入分5行,姓名性别为字符型,年龄整型
    .
    输出格式: 输出1行

    样例1

    输入格式:
    Mike 2 M
    Molly 3 F
    Tom 1 M
    Tony 1 M
    Nana 2 F
    .
    输出格式: Mike 2 M Molly 3 F Tom 1 M Tony 1 M Nana 2 F

    (2)参考代码

    #include 
    using namespace std;
    struct BABY {
    char name[100];
    int age;
    char sex;
    };
    
    int main()
    {
    	BABY a[5];
    	for(int i=0;i<5;i++)
    		scanf("%s%d %c", a[i].name, &a[i].age, &a[i].sex);
    	for (int i=0;i<5;i++)
    		printf("%s %d %c ", a[i].name, a[i].age, a[i].sex);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    10. MT1560 龙族

    (1)题目描述
    你在设计一个游戏,你负责龙族,请设计一个结构体,处理龙族生物的信息,信息包括品种,颜色,攻击力。

    输入3个龙族生物的信息,然后再输出龙族生物的信息。

    格式

    输入格式: 输入分3行,品种,颜色为字符型,攻击力整型
    .
    输出格式: 输出分3行

    样例1

    输入格式:
    Fire black 800
    Ice white 666
    Thunder blue 900
    .
    输出格式:
    Fire black 800
    Ice white 666
    Thunder blue 900

    (2)参考代码

    #include 
    using namespace std;
    
    struct DRAGON {
    	char race [20];
    	char color[20];
    	int power;
    };
    int main()
    {
    	struct DRAGON a[3];
    	int i;
    	for (i=0; i<3; i++)
    		scanf("%s%s%d", a[i].race, a[i].color, &a[i].power) ;
    	for(i=0; i<3; i++) {
    		printf("%s %s %d", a[i].race, a[i].color, a[i].power);
    		printf("\n");
    }
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    11. MT1561 排兵布阵

    (1)题目描述
    你正在排兵布阵,请设计一个结构体,管理你的士兵,士兵信息包括兵种,武器,攻击力。

    输入2种士兵的信息,然后再按照攻击力从大到小输出他们的信息。

    格式

    输入格式: 输入分2行,兵种,武器为字符型,攻击力整型
    .
    输出格式: 输出分2行

    样例1

    输入格式:
    Archer bow 20
    Knight spear 500
    .
    输出格式:
    Knight spear 500
    Archer bow 20

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct shibin{
        char name[1005],tool[1005];
        int force;
    }shi[3];
    
    int main( )
    {
        for(int i=0;i<2;i++){
            cin>>shi[i].name>>shi[i].tool>>shi[i].force;
        }
    
        if(shi[0].force>=shi[1].force){
            cout<<shi[0].name<<" "<<shi[0].tool<<" "<<shi[0].force<<endl;
            cout<<shi[1].name<<" "<<shi[1].tool<<" "<<shi[1].force;
        }else{
            cout<<shi[1].name<<" "<<shi[1].tool<<" "<<shi[1].force<<endl;
            cout<<shi[0].name<<" "<<shi[0].tool<<" "<<shi[0].force;
        } 
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    12. MT1562 谁是先锋

    (1)题目描述
    攻城战要开始了,女王依依手里有4个黑骑士,女王依依要找出最强大的黑骑士作为先锋。

    请设计一个结构体,管理他们的信息,信息包括姓名,攻击力。

    输入他们信息,然后再输出先锋的信息。

    格式

    输入格式:输入分4行,姓名为字符型,攻击力整型
    .
    输出格式: 输出分4行

    样例1

    输入格式:
    Mike 200
    Molly 355
    Tom 199
    Nana 290
    .
    输出格式: Molly 355

    备注:

    各黑骑士攻击力唯一,且均为正数。

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct shibin{
        char name[1005];
        int force;
    }shi[5];
    
    int main( )
    {
        for(int i=0;i<4;i++){
            cin>>shi[i].name>>shi[i].force;
        }
        int maxx=shi[0].force,index=0;
        for(int i=1;i<4;i++){
            if(shi[i].force>maxx){
                maxx=shi[i].force;
                index = i;
            }
        }
        cout<<shi[index].name<<" "<<shi[index].force<<endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    13. MT1563 谁是胆小鬼

    (1)题目描述
    攻城战结束了,女王依依清点俘虏,发现跑掉了1个胆小的穴居人,女王依依要找出是谁跑掉了。斥候调查发现逃走的是一个攻击力最弱小的穴居人。

    请设计一个结构体,管理穴居人俘虏的信息,信息包括姓名,攻击力。

    输入4个俘虏的信息,然后再输出逃走的俘虏的信息。

    格式

    输入格式: 输入分4行,姓名为字符型,攻击力整型
    .
    输出格式: 输出分4行

    样例1

    输入格式:
    Mike 20
    Molly 13
    Tom 11
    Nana 29
    .
    输出格式: Tom 11

    备注:

    各穴居人攻击力唯一,且均为正数。

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct shibin{
        char name[1005];
        int force;
    }shi[5];
    
    int main( )
    {
        for(int i=0;i<4;i++){
            cin>>shi[i].name>>shi[i].force;
        }
        int minn=shi[0].force,index=0;
        for(int i=1;i<4;i++){
            if(shi[i].force<minn){
                minn=shi[i].force;
                index = i;
            }
        }
        cout<<shi[index].name<<" "<<shi[index].force<<endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    14. MT1564 编程好难

    (1)题目描述
    小码哥买了3本编程书,他想先学最简单的,请帮他把最薄的书挑出来。请设计一个结构体,管理书籍的信息,信息包括种类,书名(不含空格),页数。输入书籍的信息,然后再输出最薄的书信息。

    格式

    输入格式: 按行输入种类,书名为字符型,价格整型
    .
    输出格式: 按行输出种类,书名为字符型,价格整型

    样例1

    输入格式:
    c cprimer 500
    java hijava 300
    c cbeginner 230
    .
    输出格式: c cbeginner 230

    备注:

    书的页数唯一,且均为正数。

    (2)参考代码

    #include
    struct Book {
        char typical[10];
        char book[50];
        int page;
    };
    struct Book bookneed[100];
    int main()
    {
        int i,j;
        for(i=1;i<=3;i++){
            scanf("%s",&bookneed[i].typical);
            scanf("%s",&bookneed[i].book);
            scanf("%d",&bookneed[i].page);
        }
        for(i=1;i<=3;i++){
            for(j=1;j<=3-i;j++) {
                if (bookneed[j + 1].page < bookneed[j].page) {
                    bookneed[0] = bookneed[j+1];
                    bookneed[j + 1] = bookneed[j];
                    bookneed[j] = bookneed[0];
                }
            }
        }
        printf("%s %s %d",bookneed[1].typical,bookneed[1].book,bookneed[1].page);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    15. MT1565 长者

    (1)题目描述
    输出结构体数组中年龄最大者的数据,请设计一个结构体,信息包括名字,年龄。输入5个人信息,然后再输出年龄最大的人的信息。

    格式

    输入格式: 输入名字性别为字符型,年龄整型
    .
    输出格式: 输出名字性别为字符型,年龄整型

    样例1

    输入格式:
    Mike 25
    Molly 37
    Tom 16
    Tony 15
    Nana 59
    .
    输出格式: Nana 59

    备注:

    各人年龄唯一,且均为正数。

    (2)参考代码

    #include
    struct AgeMan{
        char name[50];
        int age;
    };
    struct AgeMan Minage[10];
    int main()
    {
        int n,j,i;
        for(n=1;n<=5;n++) scanf("%s %d",&Minage[n].name,&Minage[n].age);
        for(i=0; i<5; i++){
            for(j=0; j<5-i; j++){
                if(Minage[j+1].age>Minage[j].age){
                    Minage[0] = Minage[j+1];
                    Minage[j+1] = Minage[j];
                    Minage[j] = Minage[0];
                }
            }
        }
        
        printf("%s %d",Minage[0].name,Minage[0].age);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    16. MT1566 幼者

    (1)题目描述
    输出结构体数组中年龄最小者的数据,请设计一个结构体,信息包括名字,年龄。输入5个人信息,然后再输出年龄最小的人的信息。

    格式

    输入格式: 输入名字性别为字符型,年龄整型
    .
    输出格式: 输出名字性别为字符型,年龄整型

    样例1

    输入格式:
    Mike 25
    Molly 37
    Tom 16
    Tony 15
    Nana 59
    .
    输出格式: Tony 15

    备注:

    各人年龄唯一,且均为正数。

    (2)参考代码

    #include
    struct AgeMan{
        char name[50];
        int age;
    };
    struct AgeMan Minage[10];
    int main()
    {
        int n,j,i;
        for(n=1;n<=5;n++) scanf("%s %d",&Minage[n].name,&Minage[n].age);
        for(i=0; i<5; i++){
            for(j=0; j<5-i; j++){
                if(Minage[j+1].age<Minage[j].age){
                    Minage[0] = Minage[j+1];
                    Minage[j+1] = Minage[j];
                    Minage[j] = Minage[0];
                }
            }
        }
        
        printf("%s %d",Minage[0].name,Minage[0].age);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    17. MT1567 员工薪水

    (1)题目描述
    有3个员工,从键盘输入数据,包括工号、姓名、薪水,工号薪水整型,姓名字符型,输出薪水最高的员工信息。不考虑非法输入等特殊情况。

    格式

    输入格式: 每行输入一个员工的数据,空格分隔。
    .
    输出格式: 输出薪水最高的员工信息

    样例1

    输入格式:
    101 mike 6688
    102 miya 6516
    103 tony 16648
    .
    输出格式: 103 tony 16648

    备注:

    各员工薪水唯一,且均为正数。

    (2)参考代码

    #include 
    
    using namespace std;
    struct yuangong{
        int id,salary;
        string name;
    }y[4];
    
    int main( )
    {
        for(int i=0;i<3;i++){
           cin>>y[i].id>>y[i].name>>y[i].salary;
        }
        
        int index=0,maxx=y[0].salary;
        for(int i=1;i<3;i++){
            if(maxx<y[i].salary){
                maxx = y[i].salary;
                index = i;
            }
        }
    
        cout<<y[index].id<<" "<<y[index].name<<" "<<maxx<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    18. MT1568 学生成绩

    (1)题目描述
    有3个学生,每个学生有3门课的成绩,从键盘输入数据,包括学号、姓名、三门课成绩,学号整型,姓名字符型,成绩实型,计算3门课程总平均成绩,以及平均分最高的学生信息。不考虑非法成绩等特殊情况。

    格式

    输入格式: 每行输入一个学生的数据,空格分隔。
    .
    输出格式: 输出平均分最高的学生信息

    样例1

    输入格式:
    101 mike 45 66 88
    102 miya 65 16 18
    103 tony 65 66 48
    .
    输出格式: 101 mike 45 66 88

    (2)参考代码

    #include 
    
    using namespace std;
    struct student{
        int id;
        double source1,source2,source3;
        string name;
    }s[1005];
    
    int main( )
    {
        double sum[105];
        for(int i=0;i<3;i++){
           cin>>s[i].id>>s[i].name>>s[i].source1>>s[i].source2>>s[i].source3;
           sum[i]=s[i].source1+s[i].source2+s[i].source3;
        }
        
        int index=0;
        double maxx=sum[0];
        for(int i=1;i<3;i++){
            if(maxx<sum[i]){
                maxx = sum[i];
                index = i;
            }
        }
    
        cout<<s[index].id<<" "<<s[index].name<<" "<<s[index].source1<<" "<<s[index].source2<<" "<<s[index].source3;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    19. MT1569 小码哥学编程

    (1)题目描述
    小码哥买了3本编程书,他想先学习C语音,请帮他把C相关的书挑出来。请设计一个结构体,管理书籍的信息,信息包括种类,书名(不含空格),价格。输入书籍的信息,然后再输出所有的C相关的书信息。

    格式

    输入格式: 按行输入种类,书名为字符型,价格整型
    .
    输出格式: 按行输出种类,书名为字符型,价格整型

    样例1

    输入格式:
    c cprimer 50
    java hijava 30
    c cbeginner 23
    .
    输出格式:
    c cprimer 50
    c cbeginner 23

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct book{
        string name,kind;
        int price;
    }b[1005];
    
    int main( )
    {
        for(int i=0;i<3;i++) cin>>b[i].kind>>b[i].name>>b[i].price;
        for(int i=0;i<3;i++){
            if(b[i].kind=="c"){
                cout<<b[i].kind<<" "<<b[i].name<<" "<<b[i].price<<endl;
            }
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    20. MT1570 感冒来袭

    (1)题目描述
    冬天到了,社区居民被流感侵袭,校医院组织大家打疫苗,一共应该打3针才结束。社区有3个居民,从键盘输入数据,包括姓名、年龄、疫苗打了几针,年龄、疫苗整型,姓名字符型,输出所有还没打够针的人信息。不考虑非法输入等特殊情况。

    格式

    输入格式: 每行输入一个学生的数据,空格分隔。
    .
    输出格式: 输出所有还没打够针的人信息

    样例1

    输入格式:
    mike 45 3
    miya 15 1
    tony 65 2
    .
    输出格式:
    miya 15 1
    tony 65 2

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct cun{
        string name;
        int num,age;
    }c[1005];
    
    int main( )
    {
        for(int i=0;i<3;i++) cin>>c[i].name>>c[i].age>>c[i].num;
        for(int i=0;i<3;i++){
            if(c[i].num<3){
                cout<<c[i].name<<" "<<c[i].age<<" "<<c[i].num<<endl;
            }
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    21. MT1571 贫困补助

    (1)题目描述
    有3个贫困学生,按家庭年收入发放贫困补助,从键盘输入数据,包括学号、姓名、家庭年收入,学号年收入整型,姓名字符型,年收入低于1万的发5000,低于2万的发3000,低于3万发1000,其他不发。

    输出所有人信息及补助。

    不考虑非法输入等特殊情况。

    格式

    输入格式: 每行输入一组数据,空格分隔。
    .
    输出格式: 输出所有信息

    样例1

    输入格式:
    101 mike 4566
    102 miya 15161
    103 tony 65664
    .
    输出格式:
    101 mike 4566 5000
    102 miya 15161 3000
    103 tony 65664 0

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct STUDENT{
        int number;
        char name[20];
        int income;
    };
    
    int main( )
    {
        STUDENT stu;
        int subsidy;
        for(int j=0;j<3;j++){
            scanf("%d %s %d",&(stu.number),stu.name,&(stu.income));
    
            if(stu.income<10000) subsidy=5000;
            else if(stu.income<20000) subsidy=3000;
            else if(stu.income<30000) subsidy=1000;
            else subsidy=0;
            printf("%d %s %d %d\n",stu.number,stu.name,stu.income,subsidy);
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    22. MT1572 交网费

    (1)题目描述

    小码哥又喜欢看电视又喜欢打电话聊天,每个月都要花掉很多网费。从键盘输入数据,包括第几季度、该季度网费、话费,其全部整型,计算小码哥今年花了多少钱。不考虑非法输入等特殊情况。

    格式

    输入格式: 每行输入一组数据,空格分隔。
    .
    输出格式: 输出整型

    样例1

    输入格式:
    1 145 668
    2 100 651
    3 65 664
    4 250 800
    .
    输出格式: 3343

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct Cost{
        int quarter;
        int netFee;
        int telFee;
    }cost[4];
    
    int main( )
    {
        int ans=0;
        for(int i=0;i<4;i++){
            cin>>cost[i].quarter>>cost[i].netFee>>cost[i].telFee;
        }
        for(int i=0;i<4;i++){
            ans+=cost[i].netFee;
            ans+=cost[i].telFee;
        }
        cout<<ans<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    23. MT1573 囤白菜

    (1)题目描述
    疫情期间,社区居民纷纷前往超市囤积食品,小码哥买了白菜、萝卜和豆腐,从键盘输入数据,包括菜名、重量(斤)、单价(元),其中重量、单价实型,菜名字符型,计算小码哥囤菜一共花了多少钱。不考虑非法输入等特殊情况。

    格式

    输入格式: 每行输入一组数据,空格分隔。
    .
    输出格式: 输出实型

    样例1

    输入格式:
    baicai 10 3
    luobo 15 1
    doufu 5 4
    .
    输出格式: 65.000000

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct Vegetable{
        char foodName[1005];
        double wight,danjia;
    }Vegetable[3];
    
    int main( )
    {
        double res;
        for(int i=0;i<3;i++){
            cin>>Vegetable[i].foodName>>Vegetable[i].wight>>Vegetable[i].danjia;
        }
        for(int i=0;i<3;i++){
            res+=Vegetable[i].wight * Vegetable[i].danjia;
        }
        cout<<fixed<<setprecision(6)<<res<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    24. MT1574 花友

    (1)题目描述
    小码哥是个花友,整天沉迷于网上购买花卉,每个月都要花掉很多费用,今年又买了4种花,从键盘输入数据,包括花名、数量、单价,其中花名字符型,其他其全部整型,计算小码哥今年花了多少钱。不考虑非法输入等特殊情况。

    格式

    输入格式: 每行输入一组数据,空格分隔。
    .
    输出格式: 输出平均分最高的学生信息

    样例1

    输入格式:
    dujuan 115 30
    shancha 50 5
    yueji 65 20
    zhizi 25 8
    .
    输出格式: 5200

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct huarui{
        char name[1005];
        int num,price;
    }huarui[4];
    
    int main( )
    {
        int ans=0;
        for(int i=0;i<4;i++){
            cin>>huarui[i].name>>huarui[i].num>>huarui[i].price;
        }
        for(int i=0;i<4;i++){
            ans+=huarui[i].num*huarui[i].price;
        }
        cout<<ans<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    25. MT1575 遛狗

    (1)题目描述
    小码哥减肥很多年了,屡战屡败,依然贼心不死,天天计算食物的卡路里。今天他又没管住嘴,大吃了一顿,只好吃完出门遛狗。从键盘输入数据,包括3种食物名、卡路里,其中食物名字符型,卡路里整型。

    假定小区遛狗1圈消耗50卡路里,计算小码哥今天需要遛狗多少圈。

    不考虑非法输入等特殊情况。

    格式

    输入格式: 每行输入一组数据,空格分隔。
    .
    输出格式: 输出实型

    样例1

    输入格式:
    baicai 115
    doufu 505
    yangrou 6520
    .
    输出格式: 142.800000

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct Food{
        char name[1005];
        int calorie;
    }food[3];
    
    int main( )
    {
        double ans;
        for(int i=0;i<3;i++){
            cin>>food[i].name>>food[i].calorie;
        }
        for(int i=0;i<3;i++){
            ans+=food[i].calorie;
        }
        double res = ans*1.0/50;
        cout<<fixed<<setprecision(6)<<res<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    26. MT1576 减肥大计

    (1)题目描述
    小码哥酷爱减肥,他减肥的另一种方式是散步。目测他腹部,臀部,手臂,大腿脂肪比较多,从键盘输入这四种部位和脂肪含量,其中身体部位字符型,脂肪含量整型。

    假定散步1天减掉0.05公斤脂肪,计算小码哥需要散步多少天。

    不考虑非法输入等特殊情况。

    格式

    输入格式: 每行输入一组数据,空格分隔,如样例所示。
    .
    输出格式: 输出实型

    样例1

    输入格式:
    fubu 45
    tunbu 35
    shou 10
    tui 23
    .
    输出格式: 2260.000000

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct body{
        char name[50];
        int cal;
    }body[4];
    
    int main( )
    {
        int ans;
        for(int i=0;i<4;i++){
            cin>>body[i].name>>body[i].cal;
        }
        for(int i=0;i<4;i++){
            ans+=body[i].cal;
        }
        double res = ans*1.0/0.05;
        cout<<fixed<<setprecision(6)<<res<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    27. MT1577 赡养老人

    (1)题目描述
    小码哥的爷爷奶奶年纪大了,小码哥的父母每个季度初都会给爷爷奶奶家用。′小码哥的爷爷奶奶每个月会从手里剩下的钱里花掉3000元(如果钱不够3000,则将手里的钱花完再想别的办法)。年底如果盈余就会把盈余包成红包送给小码哥当作压岁钱,没有盈余就没有红包压岁钱。

    从键盘输入数据,包括第几个季度、汇款,都是整型。计算今年小码哥的压岁钱有多少钱。

    不考虑非法输入等特殊情况。

    格式

    输入格式: 每行输入一组数据,空格分隔。
    .
    输出格式: 输出整型

    样例1

    输入格式:
    1 14115
    2 15015
    3 16520
    4 18888
    .
    输出格式: 28538

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct Money{
        int quarter;
        int pay;
    }money[3];
    
    int main( )
    {
        for(int i=0;i<4;i++) cin>>money[i].quarter>>money[i].pay;
        int temp=0;
        for(int i=0;i<4;i++){
            temp+=money[i].pay-3000*3;
            if(temp<0) temp=0;
        }
        cout<<temp<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    28. MT1578 复数

    (1)题目描述
    用下面的结构体类型表示复数:

    struct COMPLEX { double r, i;};其中实部r、虚部i。

    编写四个函数分别实现复数的和、差、积、商计算,在主函数中输入数据并调用这些函数得到复数运算结果。

    格式

    输入格式: 输入四个实数((可能有小数),空格分隔。
    .
    输出格式: 输出见样例。

    样例1

    输入格式: -1 -2 -3 -4
    .
    输出格式:
    -4.000-6.000i
    2.000+2.000i
    -5.000+10.000i
    0.440+0.080i

    备注:

    输出实型保留三位小数。

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct COMPLEX{
        double r,i;
    
        COMPLEX operator+(const COMPLEX &a){
            COMPLEX rtn;
            rtn.r = this->r+a.r;
            rtn.i = this->i+a.i;
            return rtn;
        }
    
        COMPLEX operator-(const COMPLEX &a){
            COMPLEX rtn;
            rtn.r = this->r-a.r;
            rtn.i = this->i-a.i;
            return rtn;
        }
    
        COMPLEX operator*(const COMPLEX &a){
            COMPLEX rtn;
            rtn.r = this->r*a.r-this->i*a.i;
            rtn.i = this->r*a.i+ this->i*a.r;
            return rtn;
        }
    
        COMPLEX operator/(const COMPLEX &a){
            COMPLEX rtn;
            rtn.r = (this->r*a.r+this->i*a.i)/(a.r*a.r+a.i*a.i);
            rtn.i = (this->i*a.r-this->r*a.i)/(a.r*a.r+a.i*a.i);
            return rtn;
        }
    
    };
    
    int main( )
    {
        COMPLEX a,b,c;
        scanf("%lf %lf %lf %lf",&a.r,&a.i,&b.r,&b.i);
    
        c = a+b;
        if(c.i>0) printf("%.3lf+%.3lfi\n",c.r,c.i);
        else if(c.i<0) printf("%.3lf%.3lfi\n",c.r,c.i);
    
        c = a-b;
        if(c.i>0) printf("%.3lf+%.3lfi\n",c.r,c.i);
        else if(c.i<0) printf("%.3lf%.3lfi\n",c.r,c.i);
    
        c = a*b;
        if(c.i>0) printf("%.3lf+%.3lfi\n",c.r,c.i);
        else if(c.i<0) printf("%.3lf%.3lfi\n",c.r,c.i);
    
         c = a/b;
        if(c.i>0) printf("%.3lf+%.3lfi\n",c.r,c.i);
        else if(c.i<0) printf("%.3lf%.3lfi\n",c.r,c.i);
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    29. MT1579 线段与线段的关系

    (1)题目描述
    设计分数类型:

    struct FRACTION { int fenzi, fenmu;};

    fenzi是分子fenmu是分母,编写四个函数分别实现分数的和、差、积、商计算,在主函数中输入4个正整数,分别表示两个分数的分子分母,然后调用这些函数得到分数运算结果。

    格式

    输入格式: 输入4个正整数,空格分隔。
    .
    输出格式: 输出4行,分别代表和、差、积、商。注意输出格式。

    样例1

    输入格式: 1 2 3 4
    .
    输出格式:
    5/4
    -1/4
    3/8
    2/3

    备注:

    1、要求输出最简分数。
    2、若为负数,负号写开头位置。
    3、若分母为1,则不输出分母。
    4、不会出现分母为0的特殊情况。

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct Fraction{
        int fenzi,fenmu;
    }fraction[2];
    
    int gcd(int x,int y){
        return (!y)?x:gcd(y,x%y);
    }
    
    void myPrint(int zi,int mu){
        bool isPositive = true;
        if((zi*mu)<0) isPositive = false;
        zi = abs(zi);
        mu = abs(mu);
        if(mu==1){
            if(isPositive) printf("%d\n",zi);
            else printf("-%d\n",zi);
        }else{
            if(isPositive) printf("%d/%d\n",zi,mu);
            else printf("-%d/%d\n",zi,mu);
        }
    }
    
    int main( )
    {
        int zi=0,mu=0;
    
        for(int i=0;i<2;i++){
            cin>>fraction[i].fenzi>>fraction[i].fenmu;
        }
    
        // 加
        zi = fraction[0].fenzi*fraction[1].fenmu+fraction[1].fenzi*fraction[0].fenmu;
        mu = fraction[0].fenmu*fraction[1].fenmu;
        int temp = gcd(zi,mu);
        zi /= temp;
        mu /= temp;
        myPrint(zi, mu);
    
        //减
        zi = fraction[0].fenzi*fraction[1].fenmu-fraction[1].fenzi*fraction[0].fenmu;
        mu = fraction[0].fenmu*fraction[1].fenmu;
        temp = gcd(zi,mu);
        zi /= temp;
        mu /= temp;
        myPrint(zi, mu);
    
        //乘
        zi = fraction[0].fenzi*fraction[1].fenzi;
        mu = fraction[0].fenmu*fraction[1].fenmu;
        temp = gcd(zi,mu);
        zi /= temp;
        mu /= temp;
        myPrint(zi, mu);
    
        //除
        zi = fraction[0].fenzi*fraction[1].fenmu;
        mu = fraction[0].fenmu*fraction[1].fenzi;
        temp = gcd(zi,mu);
        zi /= temp;
        mu /= temp;
        myPrint(zi, mu);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    30. MT1580 补考

    (1)题目描述
    设有一个学校人员信息表,其中学生信息包括姓名、号码、性别、班级和成绩,成绩整型,其他字符型,输入人员数据,统计学生不及格的人数。

    格式

    输入格式: 第一行输入人数,随后每行一个学生的信息,共n位学生。
    .
    输出格式: 输出整型

    样例1

    输入格式:
    5
    zhang 2020001 F 101 98
    li 2020002 F 101 28
    wang 2020003 M 102 18
    deng 2020009 M 103 44
    cao 2020011 F 104 91
    .
    输出格式: 3

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct Student{
        char name[20];
        char tel[15];
        char sex[5];
        char classID[20];
        int grade;
    }stduent[1005];
    
    int main( )
    {
        int n,cnt=0;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>stduent[i].name>>stduent[i].tel>>stduent[i].sex>>stduent[i].classID>>stduent[i].grade;
        }
        for(int i=0;i<n;i++){
            if(stduent[i].grade<60) cnt++;
        }
        cout<<cnt<<endl;
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    31. MT1581 讲师

    (1)题目描述
    设有一个学校人员信息表,其中教师信息包括姓名、号码、性别、职称和工资,工资整型,其他字符型,输入人员数据,统计职称为讲师的人数。

    格式

    输入格式: 第一行输入教师人数n,随后每行一个教师的信息,共n位教师。
    .
    输出格式: 输出整型

    样例1

    输入格式:
    5
    zhang 101 F jiaoshou 98
    li 102 F jiangshi 28
    wang 103 M jiangshi 18
    deng 109 M jiangshi 44
    cao 111 F jiaoshou 91
    .
    输出格式: 3

    (2)参考代码

    import java.util.Scanner;
    import java.util.*;
    
    class Main {
    
       public static void main(String[] args) {
    
          Scanner sc = new Scanner(System.in);
          int n = sc.nextInt();
          int count=0;
          for(int i=0;i<n;i++){
             String name = sc.next();
             String tel = sc.next();
             String sex = sc.next();
             String level = sc.next().toLowerCase();
             int salary = sc.nextInt();
             if(level.contains("jiangshi")) count++;
          }
          System.out.println(count);
          sc.close();
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    32. MT1582 星期几

    (1)题目描述
    设计日期类型如下: struct DATE { int month, day;};

    已知1月1日是星期五(假设该年是非闰年),计算输入的日期为星期几。用整数1、2、3等等分别代表周一、周二、周三等等。

    格式

    输入格式: 输入月、日,整型,空格分隔
    .
    输出格式: 输出整型

    样例1

    输入格式: 12 25
    .
    输出格式: 6

    备注:

    周日用7表示。

    (2)参考代码

    #include 
    using namespace std;
    
    struct DATE{
    int month,day;
    };
    
    int main( )
    {DATE date;
        int tempday,MOD;
        cin >> date.month >> date.day;
    int a[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
    tempday=a[date.month-1]+date.day+5;
    MOD = tempday%7;
    MOD = MOD+6;
    if(MOD>7) MOD = MOD%7;
    printf("%d",MOD);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    33. MT1583 日期

    (1)题目描述
    设计日期类型如下: struct DATE { int month, day;};

    假设某一年是非闰年。编写函数计算输入的日期加减n天(正为后,负为前)的日期。不考虑超出该年的情况。

    格式

    输入格式: 第一行月、日,空格分隔,第二行输入n,整型。
    .
    输出格式: 输出年月日,空格分隔,整型

    样例1

    输入格式:
    12 21
    4
    .
    输出格式: 12 25

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct DATE{
        int month,day;
    };
    
    int DATEtoDAY(DATE date){
        int count=0;
        int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        for(int i=1;i<=date.month;i++)
            count += m[i-1];
        count += date.day;
        return count;
    }
    
    DATE DAYtoDATE(int count){
        DATE date;
        int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        int i=1;
        while(count>m[i]){
            count -= m[i];
            i++;
        }
        date.month = i;
        date.day = count;
        return date;
    }
    
    int main( )
    {
        DATE date;
        scanf("%d %d",&date.month,&date.day);
        int n;
        scanf("%d",&n);
        int count=DATEtoDAY(date);
        count += n;
        date = DAYtoDATE(count);
        printf("%d %d",date.month,date.day);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    34. MT1584 日期差

    (1)题目描述
    设计日期类型如下: struct DATE { int month, day;};

    假设某一年是非闰年。输入该年内的两组日期,计算两个日期间的差((天数)。统一用后输入的日期减去先输入的日期。

    格式

    输入格式: 分两行输入两组月、日,空格分隔。
    .
    输出格式: 输出整型

    样例1

    输入格式:
    12 21
    12 19
    .
    输出格式: -2

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct DATE{
        int month,day;
    };
    
    int DATEtoDAY(DATE date){
        int count=0;
        int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        for(int i=1;i<=date.month;i++)
            count += m[i-1];
        count += date.day;
        return count;
    }
    
    DATE DAYtoDATE(int count){
        DATE date;
        int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        int i=1;
        while(count>m[i]){
            count -= m[i];
            i++;
        }
        date.month = i;
        date.day = count;
        return date;
    }
    
    int main( )
    {
        DATE date1,date2;
        scanf("%d %d",&date1.month,&date1.day);
        scanf("%d %d",&date2.month,&date2.day);
        int n=DATEtoDAY(date2)-DATEtoDAY(date1);
        printf("%d",n);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    35. MT1585 两点距离

    (1)题目描述
    用下面的数据类型分别表示点: struct POINT { int x, y; };

    输入两个点的坐标值x和y,编写函数求两点距离。

    格式

    输入格式: 输入整型,空格分隔。每行一个点的坐标。
    .
    输出格式: 输出实型(两位小数)

    样例1

    输入格式:
    -20 20
    20 -10
    .
    输出格式: 50.00

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct Point{
        int x,y;
    }point[2];
    
    int main( )
    {
        for(int i=0;i<2;i++){
            cin>>point[i].x>>point[i].y;
        }
        double dist=0;
        dist = sqrt(pow(point[0].x-point[1].x,2.0)+pow(point[0].y-point[1].y,2.0));
        cout<<fixed<<setprecision(2)<<dist<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    36. MT1586 矩形面积

    (1)题目描述
    用下面的数据类型分别表示点和矩形:struct POINT{//点
    int x, y;//坐标值x和y
    };
    struct RECT{//矩形
    POINT It, rb;//矩形的左上角和右下角
    };
    有一个矩形,矩形的边分别和x,y轴平行,输入矩形两个点的坐标值x和y,编写函数求矩形面积。不考虑溢出之类的特殊情况。

    格式

    输入格式: 输入整型,空格分隔。每行一个点的坐标。
    .
    输出格式: 输出整型

    样例1

    输入格式:
    -20 20
    20 -10
    .
    输出格式: 1200

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct Point{
        int x,y;
    }point[2];
    
    struct Rect{
        Point lt,rb;
    }rect;
    
    int main( )
    {
        int ans;
        for(int i=0;i<2;i++){
            cin>>point[i].x>>point[i].y;
        }
        rect.lt = point[0],rect.rb = point[1];
        int length =0,width=0;
        length = abs(rect.lt.x-rect.rb.x);
        width = abs(rect.lt.y-rect.rb.y);
        ans=length*width;
        cout<<ans<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    37. MT1587 圆面积

    (1)题目描述
    用下面的数据类型分别表示点和圆:
    struct POINT {//点
    int x, y;//坐标值x和y
    };
    struct CIRCLE{//圆
    POINT C;//圆心
    double r;//半径
    };
    依次输入圆心的坐标值x、y和半径,编写函数求圆面积。

    格式

    输入格式: 输入整型,空格分隔。
    .
    输出格式: 输出实型,保留2位小数。

    样例1

    输入格式: -20 20 50
    .
    输出格式: 7853.98

    备注:

    PI取3.1415926

    (2)参考代码

    #include 
    
    using namespace std;
    double res,PI=3.1415926;
    struct Point{
        int x,y;
    }point;
    
    struct Rect{
        Point c;
        double r;
    }rect;
    
    int main( )
    {
        cin >> point.x >> point.y >>rect.r;
        rect.c = point;
        res = PI*rect.r*rect.r;
        cout<<fixed<<setprecision(2)<<res<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    38. MT1588 最大的长方体

    (1)题目描述
    输入n (n<10000)个长方体的长l,宽w,高h,求其表面积最大的立方体。

    格式

    输入格式: 第一行一个整数n , (0 .
    输出格式: 一个数字,代表最大的立方体的编号

    样例1

    输入格式:
    3
    1 2 3
    2 3 4
    3 4 5
    .
    输出格式: 3

    (2)参考代码

    #include 
    
    using namespace std;
    typedef long long ll;
    struct Cuboid{
        int l,w,h;
    }cuboid[1005];
    
    int main( )
    {
        ll n;
        int index=0,maxx=0;
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>cuboid[i].l>>cuboid[i].w>>cuboid[i].h;
        }
        for(int i=1;i<=n;i++){
            ll temp=0;
            temp=(cuboid[i].l*cuboid[i].w+cuboid[i].l*cuboid[i].h+cuboid[i].h*cuboid[i].w)*2;
            if(temp>maxx){
                maxx = temp;
                index = i;
            }
        }
        cout<<index<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    39. MT1589 园的边框

    (1)题目描述
    用下面的数据类型分别表示点和圆:
    struct POINT {//点
    int x, y;//坐标值x和y
    };
    struct CIRCLE {//圆
    POINT C;//圆心
    double r;//半径
    };
    依次输入圆心的坐标值x、 y和半径,再在第二行输入第2个点的坐标,判断第2个点是否在圆的边框上。

    格式

    输入格式: 输入整型,空格分隔。
    .
    输出格式: 输出YES或者NO

    样例1

    输入格式:
    -20 20 50
    0 0
    .
    输出格式: NO

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct Point{
        int x,y;
    }point[2];
    
    struct Rect{
        Point c;
        double r;
    }rect;
    
    int main( )
    {
        cin>>point[0].x>>point[0].y>>rect.r;
        cin>>point[1].x>>point[1].y;
        rect.c = point[0];
        double dist=0;
        dist = sqrt(pow(point[0].x-point[1].x,2.0)+pow(point[0].y-point[1].y,2.0));
        if(abs(dist-rect.r)<0.000001) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    40. MT1590 矩形的边框

    (1)题目描述
    用下面的数据类型分别表示点和矩形:
    struct POINT{//点
    int x, y;//坐标值x和y
    };
    struct RECT {//矩形
    POINT It, rb;//矩形的左上角和右下角
    };
    输入矩形两个点的坐标值x和y,再输入第3个点的坐标,判断第3个点是否在矩形的边框上。

    格式

    输入格式:
    第一行输入左上角坐标
    第二行输入右下角坐标
    第三行输入第三个点的坐标输入整型,空格分隔。
    .
    输出格式: 输出YES或者NO

    样例1

    输入格式:
    -20 20
    20 -10
    20 10
    .
    输出格式: YES

    备注:

    矩形平行于x、y轴

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct Point{
        int x,y;
    }point[5];
    
    struct Rect{
        Point lt,rb;
    }rect;
    
    int main( )
    {
        bool flag=false;
        for(int i=1;i<=3;i++) cin>>point[i].x>>point[i].y;
        rect.lt = point[1];
        rect.rb = point[2];
        if(point[3].x==rect.lt.x || point[3].x == rect.rb.x){
            if(point[3].y >= rect.rb.y && point[3].y <=rect.lt.y) 
                flag=true;
        }
    
        if(point[3].y == rect.lt.y || point[3].y == rect.rb.y){
            if(point[3].x >= rect.lt.x && point[3].x <= rect.rb.x)
                flag=true;
        }
    
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    41. MT1591 圆的内部

    (1)题目描述
    用下面的数据类型分别表示点和圆:
    struct POINT{//点
    int x, y;l/坐标值x和y
    };
    struct CIRCLE{//圆
    POINT c;//圆心
    double r;l/半径
    };
    输入圆心的坐标值xy和半径,再在第二行输入第2个点的坐标,判断第2个点是否在圆的内部上。

    格式

    输入格式: 输入整型,空格分隔。
    .
    输出格式: 输出YES或者NO

    样例1

    输入格式:
    -20 20 50
    0 0
    .
    输出格式: YES

    (2)参考代码

    #include 
    
    using namespace std;
    
    struct Point{
        int x,y;
    }point[2];
    
    struct Rect{
        Point c;
        double r;
    }rect;
    
    int main( )
    {
        cin>>point[0].x>>point[0].y>>rect.r;
        cin>>point[1].x>>point[1].y;
        rect.c = point[0];
        double dist=0;
        dist = sqrt(pow(point[0].x-point[1].x,2.0)+pow(point[0].y-point[1].y,2.0));
        if(rect.r-dist>0) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    42. MT1592 矩形的内部

    (1)题目描述
    用下面的数据类型分别表示点和矩形:
    struct POINT {//点
    int x, y;//坐标值x和y
    };
    struct RECT{//矩形
    POINT It, rb;//矩形的左上角和右下角
    };
    输入矩形两个点的坐标值x和y,再输入第3个点的坐标,判断第3个点是否在矩形的内部((不含边框)。

    格式

    输入格式: 输入整型,空格分隔。每行一个点的坐标。
    .
    输出格式: 输出YES或者NO

    样例1

    输入格式:
    -20 20
    20 -10
    -20 -10
    .
    输出格式: NO

    备注:

    矩形平行于x、y轴

    (2)参考代码

    #include
    using namespace std;
    
    struct POINT{
        int x,y;
    };
    
    struct RECT{
        POINT lt,rb;
    };
    int main(){
        RECT rect;
        POINT point;
        scanf("%d %d", &(rect.lt.x), &(rect.lt.y));
        scanf("%d %d", &(rect.rb.x), &(rect.rb.y));
        scanf("%d %d", &(point.x), &(point.y));
    
        if((point.x > rect.lt.x && point.x<rect.rb.x)&&(point.y > rect.rb.y && point.y < rect.lt.y))
            printf("YES\n");
        else printf("NO\n");
    return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    43. MT1593 点到线

    (1)题目描述
    用下面的数据类型分别表示点和线:
    struct POINT{//点
    int x, y;//坐标值x和y
    };
    struct LINE{//线
    POINT s, e;//线的两端
    };
    输入线段两个端点的坐标值x和y,再输入第3个点的坐标,计算第3个点距这条线的最近距离。

    格式

    输入格式: 输入整型,空格分隔。每行一个点的坐标。
    .
    输出格式: 输出实型,保留两位小数

    样例1

    输入格式:
    -20 20
    20 -10
    0 0
    .
    输出格式: 4.00

    备注:
    此题求的是点到直线最短距离

    (2)参考代码

    #include
    using namespace std;
    struct POINT{ //点
        int x,y; //坐标值×和y
    };
    struct LINE{ //线
        POINT s,e; //线的两端
    };
    int main()
    {
        POINT p;
        LINE line;
        double s,d,h;
        scanf("%d%d", &line.s.x, &line.s.y);
        scanf("%d%d", &line.e.x, &line.e.y);
        scanf("%d%d", &p.x, &p.y);
        s=fabs(line.s.x*(line.e.y-p.y)+line.e.x*(p.y-line.s.y)+p.x*(line.s.y-line.e.y))/2;
        d=sqrt((line.e.x-line.s.x)*(line.e.x-line.s.x)+(line.e.y-line.s.y)*(line.e.y-line.s.y));
        h=s*2/d;
        printf("%.2lf\n",h);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    44. MT1594 点到圆

    (1)题目描述
    用下面的数据类型分别表示点和圆:
    struct POINT{//点
    int x, y;//坐标值x和y
    };
    struct CIRCLE{//圆
    POINT c;//圆心
    double r;//半径
    };
    依次输入圆心的坐标值x、y和半径,再在第二行输入第2个点的坐标,计算第2个点距这个圆的最近距离。如果点在圆内部或者边框上,距离计为0。

    格式

    输入格式: 第一行输入圆心坐标和半径,第二行输入第二个点。空格分离。其中除半径为实型外,其他均为整型。
    .
    输出格式: 输出实型距离(保留两位小数)。如果在圆内部或边框上,输出0。

    样例1

    输入格式:
    -20 20 50
    0 0
    .
    输出格式: 0

    样例2

    输入格式:
    1 2 4.5
    4 7
    .
    输出格式: 1.33

    (2)参考代码

    #include 
    
    using namespace std;
    
    double res;
    
    struct Point{
        int x,y;
    }point[2];
    
    struct Circle{
        Point c;
        double r;
    }circle;
    
    int main( )
    {
        cin>>point[0].x>>point[0].y>>circle.r;
        cin>>point[1].x>>point[1].y;
        circle.c = point[0];
        double dist = 0;
        dist = sqrt(pow(point[1].x - circle.c.x,2.0)+pow(point[1].y-circle.c.y,2.0));
        if(dist - circle.r<=0) cout<<0<<endl;
        else{
            res = dist-circle.r;
            cout<<fixed<<setprecision(2)<<res<<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    45. MT1595 点到矩形

    (1)题目描述
    用下面的数据类型分别表示点和矩形:
    struct POINT {//点
    int x, y;/I坐标值x和y
    };
    struct RECT{//矩形
    POINT lt, rb;//矩形的左上角和右下角
    };
    输入矩形两个点的坐标值x和y,再输入第3个点的坐标,计算第3个点距这个矩形的最近距离。如果点在矩形内部或者边框上,距离计为0。

    格式

    输入格式: 输入整型,空格分隔。每行一个点的坐标。
    .
    输出格式: 输出实型。如果在矩形内部或边框上,输出0。

    样例1

    输入格式:
    -20 20
    20 -10
    20 10
    .
    输出格式: 0

    样例2

    输入格式:
    0 10
    10 0
    -1 -1
    .
    输出格式: 1.414214

    备注:

    矩形平行于x、y轴

    (2)参考代码

    #include
    using namespace std;
    struct POINT{ //点
        int x,y; //坐标值×和y
    };
    struct LINE{ //线
        POINT s,e; //线的两端
    };
    struct RECT{ //矩形
        POINT lt,rb; //矩形的左上角和右下角
    };
    double pointToPoint(POINT a,POINT b){
        double d =sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); return d;
    return d;
    }
    double pointToLine(POINT p,LINE l){
        double s=fabs(l.s.x*(l.e.y-p.y)+l.e.x*(p.y-l.s.y)+p.x*(l.s.y-l.e.y))/2;
        double d=pointToPoint(l.s,l.e);
        double h=s*2/d;
        return h;
    }
    int main()
    {
        POINT plt,plb,prt,prb,point;
        LINE lineLeft,lineRight,lineTop,lineBottom;
        RECT rect;
        scanf("%d %d",&plt.x,&plt.y);
        scanf("%d %d",&prb.x,&prb.y);
        scanf("%d %d",&point.x,&point.y);
        plb.x=plt.x,plb.y=prb.y;
        prt.x=prb.x,prt.y=plt.y;
        lineLeft.e=plb,lineLeft.s=plt;
        lineRight.e=prb,lineRight.s=prt;
        lineBottom.e=plb,lineBottom.s=prb;
        lineTop.e=plt,lineTop.s=prt;
        rect.lt=plt,rect.rb=prb;
        if(plt.x<=point.x&point.x<=prt.x&plb.y<=point.y&&point.y<=plt.y)
            printf("0");
    
        else{
            //矩形×范围内上方或下方
            if(plt.x<=point.x&point.x<=prt.x){
                double d1=pointToLine(point,lineTop);double d2=pointToLine(point,lineBottom);printf("%lf",min(d1,d2));
                return 0;
            }
            //矩形y范围内左侧或右侧
            if(plb.y<=point.y&&point.y<=plt.y){
                double d1=pointToLine(point,lineLeft);
                double d2=pointToLine(point,lineRight);
                printf("%lf",min(d1,d2));
                return 0;
            }
    
            //其他情况,取最近的顶点距离作为距离
        double d1=pointToPoint(point,plt);
        double d2=pointToPoint(point,plb);
        double d3=pointToPoint(point,prt);
        double d4=pointToPoint(point,prb);
        printf("%lf",min(min(d1,d2),min(d3,d4)));
        return 0;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    46. MT1596 线到矩形

    (1)题目描述
    用下面的数据类型分别表示点、线和矩形:
    struct POINT {//点n的阶乘
    int x, y;l/坐标值x和y
    };
    struct LINE{//线P
    OINT s, e;//线的两端
    };
    struct RECT {//矩形
    POINT It, rb;//矩形的左上角和右下角
    };
    输入矩形两个点的坐标值,再输入线段的端点的坐标值,判断线段是否会与矩形面相交,输出YES或者NO。

    线段与矩形面相交的定义:只要线段上有一点在矩形框上或者框内,则认为该线段与矩形面相交。

    格式

    输入格式: 第一行输入矩形两个点的坐标值,第二行输入线段的端点的坐标值,空格分隔。
    .
    输出格式: 输出YES或者NO

    样例1

    输入格式:
    -20 20 20 -10
    20 10 -20 -10
    .
    输出格式: YES

    (2)参考代码

    #include
    using namespace std;
    struct POINT{
    int x,y;
    };
        struct LINE{
        POINT s,e;
    };
    struct RECT{
        POINT lt,rb;
    };
    bool intersect(LINE AB,LINE CD){//判断线段CD是否与直线AB相交
    //直线AB的直线方程为:
    // f(x,y)=(y-A.y)*(A.x-B.x)-(x-A.x)*(A.y-B.y)=0
        POINT A=AB.e,B=AB.s;
        POINT C=CD.e,D=CD.s;
        int fc=(C.y-A.y)*(A.x-B.x)-(C.x-A.x)*(A.y-B.y);
        int fd=(D.y-A.y)*(A.x-B.x)-(D.x-A.x)*(A.y-B.y);
        if(fc*fd<=0) return true;
        else return false;
    }
    //判断两线段是否相交
    bool interSegment(LINE AB,LINE CD){
        if(intersect(AB, CD) && intersect(CD, AB)) return true;
        else return false;
    }
    
    bool inRect(POINT point,RECT rect){
        if(point.x<=rect.rb.x&point.x>=rect.lt.x&&point.y<=rect.lt.y&&point.x>=rect.rb.y)return true;
        else return false;
    }
    
    int main (){
        int x1,y1,x2,y2,x3,y3,x4,y4; cin >>x1>>y1>>x2>>y2;
        cin >>x3>>y3>>x4>>y4;
        POINT LT {x1,y1}, LB {x1,y2}, RT {x2,y1}, RB {x2,y2}, lineA {x3,y3},lineB{x4,y4};
        RECT rect { LT , RB };
        LINE diag1{ LT , RB },diag2{ LB , RT }, lineAB { lineA , lineB };
        bool flag = false ;
        if ( inRect ( lineA , rect )|| inRect(lineB, rect ))
            flag = true ;
        if ( interSegment ( lineAB ,diag1) || interSegment ( lineAB ,diag2))
            flag = true ;
        if ( flag )  cout <<"YES ";
        else  cout <<"NO";
        return 0;
     }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    47. MT1597 平行线

    (1)题目描述
    用下面的数据类型表示线:

    struct POINT {//点
    int x, y;/l坐标值x和y
    };

    struct LINE{//线
    POINT s, e;//线的两端
    };

    输入2个线段的端点的坐标值x和y,判断两条线段所在直线是否为平行线。如果两线段共线,判为不平行。

    格式

    输入格式:输入整型,空格分隔。按照先起点(x,y)再终点(x,y)的次序。每行一个线段的信息。
    .
    输出格式: 输出YES或者NO

    样例1

    输入格式:
    -20 20 20 -10
    0 0 5 0
    .
    输出格式: NO

    (2)参考代码

    #include 
    
    using namespace std;
    struct POINT{
    	int x,y;
    };
    struct LINE{
    	POINT s,e;
    };
    
    bool parallel (LINE AB,LINE CD){
    	POINT A =AB.e,B = AB.s;
    	POINT C =CD.e,D = CD.s;
    	if(A.x==B.x||C.x==D.x||A.y==B.y||C.y==D.y)
    		if(A.x==B.x&&C.x==D.x||A.y==B.y&&C.y==D.y)
    			return true;
    	if((B.y-A.y) * (D.x - C.x )==(D.y-C.y)*(B.x-A.x))
    		return true;
    	return false;
    }
    int main( ){
    	POINT A,B,C,D;
    	cin>>A.x>>A.y>>B.x>>B.y>>C.x>>C.y>>D.x>>D.y; 
    	bool flag =false;
    	if(parallel(LINE{A,B},LINE{C,D}))
    		flag =true;
    	if(parallel(LINE{A,C},LINE{C,D}))
    		flag =false;
    	if(flag)
    		cout <<"YES";
    	else cout <<"NO";
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    48. MT1598 交叉线

    (1)题目描述
    用下面的数据类型表示线:
    struct POINT {//点
    int x, y;l/坐标值x和y
    };
    struct LINE{//线
    POINT s, e;//线的两端
    };
    输入2个线段的端点的坐标值x和y,判断两条线段是否交叉、求交叉点位置。如果不交叉或者重叠则输出-1。

    格式

    输入格式: 输入整型,空格分隔。按照先起点(x,y)再终点(x,y)的次序。每行一个线段的信息。
    .
    输出格式: 输出交叉点坐标,实型,保留2位小数。

    样例1

    输入格式:
    -20 20 -20 -10
    0 0 5 0
    .
    输出格式: -1

    (2)参考代码

    #include 
    using namespace std;
    
    struct POINT {
    	int x, y;
    };
    struct LINE {
    	POINT s, e;
    };
    struct EQUATION{//直线斜截式方程:y=ax+b
    	LINE line;
    	double a;
    	double b;
    	bool vertical;//是否垂直,即斜率不存在
    };
    
    EQUATION get_equa(LINE line){
    	EQUATION equation;
    	equation.line = line;
    	if (line.e.x == line.s.x)
    		equation.vertical = true;
    	else {
    		equation.a =(line.s.y - line.e.y)* 1.0 / (line.s.x - line.e.x);
    		equation.b = line.s.y - equation.a* line.s.x;
    		equation.vertical = false;
    	}
    return equation;
    }
    
    //判断线的交点是不是在线段两个端点区域内
    bool judge_point(LINE l1,LINE l2,double x, double y){
    	if((x-l1.s.x)* (x-l1.e.x)<= 0 && (y-l1.s.y) *(y-l1.e.y)<= 0
    	&&(x-l2.s.x)* (x-l2.e.x) <= 0 &&(y-l2.s.y) *(y-l2.e.y)<= 0)
    		return true;
    	else
    		return false;
    }
    
    //计算交点:存在一个斜率不存在的时候
    void vertTrue(EQUATION vert,EQUATION normal){
    double x = vert.line.e.x;
    double y = normal.a*x +normal.b;
    if (judge_point(vert.line,normal.line,x,y))
    	printf( "%.2lf %.2lf\n",x,y);
    else
    	printf("-1\n");
    }
    
    //斜率都存在的时候
    void vertFalse(EQUATION normal1,EQUATION normal2){
    double diff_a = normal1.a - normal2.a;
    if(diff_a ==0)
    printf("-1\n");else {
    double x =-(normal1.b - normal2.b)/(normal1.a - normal2.a);double y = normal1.a*x +normal1.b;
    if(judge_point(normal1.line,normal2.line,x,y))
    	printf("%.2lf %.2lf\n", x,y);
    else
    	printf("-1\n");
    	}
    }
    
    int main(){
    LINE l1,l2;
    scanf("%d%d%d%d",&l1.s.x,&l1.s.y,&l1.e.x,&l1.e.y);
    scanf("%d%d%d%d",&l2.s.x,&l2.s.y,&l2.e.x,&l2.e.y);
    EQUATION line1 = get_equa(l1);
    EQUATION line2 = get_equa(l2);
    if (line1.vertical && !line2. vertical)
    vertTrue(line1,line2);
    else if(!line1.vertical && line2.vertical)
    vertTrue(line2,line1);
    else if(line1.vertical && line2.vertical)
    printf("-1\n");
    else
    	vertFalse(line1,line2);
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    49. MT1599 成绩排名

    (1)题目描述
    输入n个学生的名字以及其学习成绩,求学习成绩成绩从高到低排序的名单。如果成绩相同,则按字典序降序排序。

    格式

    输入格式:
    第一行一个整数n , (0 接下来包含n行,第一行包含空格分开的1个字符串和1个整数,分别代表学生的名字和学习成绩。
    .
    输出格式: 学生的综合测评成绩从高到低排序的名单。如果成绩相同,则按字典序降序排序。

    样例1

    输入格式:
    3
    XiaoMing 1
    XiaoLi 2
    XiaoHuang 3
    .
    输出格式:
    XiaoHuang
    XiaoLi
    XiaoMing

    (2)参考代码

    #include 
    using namespace std;
    typedef long long ll ;
    #define MAX_NUM 10010
    #define PI 3.1415926
    double res;
    int ans, n, m, k,len,cnt = 0,minn = MAX_NUM, maxx = 0;
    char ch, s1[MAX_NUM] = "", s2[MAX_NUM] = "";
    bool flag = false;
    int a[MAX_NUM]= {0},b[MAX_NUM]= {0};
    double x,s,t;
    
    struct Student{
    string name;
    int grade;
    }student[ MAX_NUM];
    
    bool cmp(Student s1,Student s2) { //cmp true不用交换位置false需要交换位置
    if( s1.grade != s2.grade) {
    	if(s1.grade > s2.grade)
    		return true;
    	else
    		return false;
    }else {
    	if(s1.name > s2.name)
    		return true;
    	else
    		return false;
    	}
    }
    
    int main( ) {
    scanf( "%d\n",&n);
    for(int i = 0; i < n; i++) {
    cin >> student[i].name >> student[i].grade;
    }
    sort(student, student+n,cmp);for( int i = 0; i < n; i++) {
    cout << student[i].name << endl;
    }
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    50. MT1600 综合测评

    (1)题目描述
    综合测评成绩Z,指的是学生学习成绩X,思想品德成绩S,体育素质成绩T的加权相加(Z=R * X +s * S +t * T )。

    输入n个学生的名字,学习成绩X,思想品德成绩S,体育素质成绩T,求综合测评成绩从高到低排序的名单。如果成绩相同,则按字典序降序排序。

    格式

    输入格式:
    第一行一个整数n , (O .
    第二行包含三个实数(和不一定为1) x,s,t分别代表学习成绩X,思想品德成绩S,体育素质成绩T所占的权重
    .
    接下来包含n行,第一行包含空格分开的一个字符串和三个整数,分别代表学生的名字,X,S,T
    .
    输出格式: 学生的综合测评成绩从高到低排序的名单。如果成绩相同,则按字典序降序排序。

    样例1

    输入格式:
    3
    0.3 0.3 0.3
    XiaoMing 1 2 3
    XiaoLi 2 3 4
    XiaoHuang 3 4 5
    .
    输出格式:
    XiaoHuang
    XiaoLi
    XiaoMing

    (2)参考代码

    #include 
    using namespace std;
    typedef long long ll ;
    #define MAX_NUM 10010
    #define PI 3.1415926
    double res;
    int ans, n, m, k,len,cnt = 0,minn = MAX_NUM, maxx = 0;
    char ch, s1[MAX_NUM] = "", s2[MAX_NUM] = "";
    bool flag = false;
    int a[MAX_NUM]= {0},b[MAX_NUM]= {0};
    double x,s,t;
    
    struct Student{
    string name;
    int grade;
    int X,S,T;
    double zongfen;
    }student[ MAX_NUM];
    
    bool cmp(Student s1,Student s2) { //cmp true不用交换位置false需要交换位置
    if( s1.zongfen != s2.zongfen) {
    	if(s1.zongfen > s2.zongfen)
    		return true;
    	else
    		return false;
    }else {
    	if(s1.name > s2.name)
    		return true;
    	else
    		return false;
    	}
    }
    
    int main( ) {
    scanf( "%d\n",&n);
    scanf("%lf %lf %lf\n", &x,&s,&t);for(int i = 0; i < n; i++) {
    cin >> student[i ].name >> student[i].X >> student[i].S >>
    student[i].T;
    student[i].zongfen = x*student[i].X + s*student[i].S +t*student[i].T;
    }
    sort(student, student+n,cmp);for( int i = 0; i < n; i++) {
    cout << student[i ].name << endl;
    }
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    结语

    感谢大家一直以来的不断支持与鼓励,码题集题库中的新手村600题现已全部完结,之后会逐步跟进黄金,钻石,星耀,王者的题,尽请期待!!!
    同时,也希望这些题能帮助到大家,一起进步,祝愿每一个算法道路上的“苦行僧”们,都能够历经磨难,终成正果,既然选择了这条路,走到了这里,中途放弃,岂不是太过可惜?

    另附中国计算机学会的杰出会员、常务理事轩哥博士的B站视频讲解链接https://space.bilibili.com/518554541/?spm_id_from=333.999.0.0,供大家更好的进行学习与刷题~( ̄▽ ̄~)~

    愿你的结局,配得上你一路的颠沛流离。

  • 相关阅读:
    OBS Studio免费开源录屏工具
    基于RuoYi-Flowable-Plus的若依ruoyi-nbcio支持本地图片上传与回显的功能实现(一)
    基于ssm服装购物系统
    bootstrap学习:bootstrap定制化 根据bootstrap实现自己的Ui库
    GeForce RTX 2080显卡驱动安装(linux系统)
    36_多态
    2022-11-16 mysql列存储引擎-支持COUNT中DISTINCT-需求分析
    一点点学共形几何(1) 微分几何之拓扑空间简介
    Qt多线程技术
    Flutter快学快用12 列表样式:实践 Flutter 中内容多样式展示方式
  • 原文地址:https://blog.csdn.net/m0_54754302/article/details/126129868