吉林大学 程序设计基础 2022级 OJ期末考试 2.23

本人能力有限,发出只为帮助有需要的人。

以下为实验课的复盘,内容会有大量失真,请多多包涵。

1.双手剑士的最优搭配

每把剑有攻击力和防御力两个属性。双手剑士可以同时拿两把剑,其得到攻击力为两把剑中的攻击力的最大值,防御力为两把剑中的防御力的最小值。现在想让双手剑士的攻击力和防御力之和最大。输入n作为剑的个数,再输入2n分别对应每把剑的攻击力和防御力,要求输出最优解为哪两把剑(当攻击力和防御力之和相同时优先选择编号靠前的两把剑)

输入:3 10 4 5 10 7 8

输出:1 2

题解为

#include <stdio.h>
int max(int x,int y)//最大值函数
{
    if(x>y)
        return x;
    return y;
}
int min(int x,int y)//最小值函数
{
    if(x<y)
        return x;
    return y;
}
int main(void)
{
    int a[100][1]={0},n;//设置二维数组
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d %d",&a[i][0],&a[i][1]);//a[i][0]为攻击力,a[i][1]为防御力
    int maxSum=0,attack=0,defend=0;
    int x,y;
    for(int i=0;i<n;i++)//二重循环遍历
    {
        for(int j=i+1;j<n;j++)
        {
            defend=min(a[i][1],a[j][1]);
            attack=max(a[i][0],a[j][0]);
            if(defend+attack>maxSum)
            {
                maxSum=defend+attack;//找当前的最优解
                x=i;y=j;//用x,y记录
            }
        }
    }
    printf("%d %d",x+1,y+1);//注意此题从1开始计数
    return 0;
}

2.一列数转化二级制中1的个数(11.17)

输入一个正整数n,之后输入n个非负整数组成一个数组,将数组中的每个元素转化成二进制数,输出数组中每个二进制数中1的个数,输出每个数前有一个空格

样例:
输入:5 1 2 3 4 5

输出: 1 1 2 1 2

#include <stdio.h>
int main(void)
{
    int n,i,a[100],b[100],flag;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
    {
        flag=0;
        while(a[i]!=0)//将数组的数字化成二进制
        {
            if(a[i]%2==1)
                flag++;//计算二进制中1的数量
            a[i]/=2;
            b[i]=flag;//用两个数组
        }
    }
    for(i=0;i<n;i++)
        printf(" %d",b[i]);
    return 0;
}

3.一周中的下一个热天(11.10)

向一个长度为7的数组中输入七天的气温(范围为-10到+10)

找到每天以后更热(温度大于此天)的一天,输出其间隔的天数

样例(题目所给样例忘了,这个是新编的)

输入1 1 -1 2 3 1 4

输出3 2 1 1 2 1 0

原题如下

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    int temperature[7],i,j;
    for(i=0;i<7;i++)
        scanf("%d",&temperature[i]);
    int days[7]={0};
    for(i=0;i<7;i++)
    /*
    
    */
    for(i=0;i<7;i++)
        printf("%d ",days[i]);
    return 0;
}

解答

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    int temperature[7],i,j;
    for(i=0;i<7;i++)
        scanf("%d",&temperature[i]);
    int days[7]={0};//考了数组初始化,但对此题的作答无影响
    for(i=0;i<7;i++)
    {
        int flag=0;
        for(j=i+1;j<7;j++)//两层循环
        {
            flag++;
            if(temperature[j]>temperature[i])
            {
                days[i]=flag;//构建新数组
                break;
            }
        }
    }
    for(i=0;i<7;i++)
        printf("%d ",days[i]);
    return 0;
}

4.删除链表中的重复元素

给出一个结构体链表,包含姓名、学号、年龄三个要素。输入一个n,要求输入n个三要素后,再输入一个数字,删除年龄为这个数字的链表节点,并输出链表。

输入:

3

1 zhangsan 18

2 lisi 19

3 wangwu 18

18

输出

2 lisi 19

原题

​
#include <stdio.h>
#include <malloc.h>
struct cell
{
    int x;
    char name[1000];//字符数字储存姓名
    int age;
    struct cell* next;
};

struct cell *build(int num)//输入链表
{
	struct cell *tmp;
    struct cell *headA = (struct cell*)malloc(sizeof(struct cell));
	scanf("%d %s %d",&headA->x,headA->name,&headA->age);//注意字符串的输入方法
    struct cell *end = headA;
	for(int i=0; i<num-1; i++)//输入num个元素
	{
		tmp = (struct cell*)malloc(sizeof(struct cell));
	    scanf("%d %s %d",&tmp->x,tmp->name,&tmp->age);
		end->next = tmp;
		end = tmp; 
	}
	end->next = NULL;
	return headA;
}

void print(struct cell* head)//输出链表
{
    struct cell* p;
    p=head;
    while(p!=NULL)
    {
        printf("%d %s %d\n",p->x,p->name,p->age);
        p=p->next;
    }
}
void release(struct cell* head)//释放链表所占用的空间
{
    struct cell *p,*tmp;
    p=tmp=head->next;
    while(p!=NULL)
    {
        tmp=p;
        p=p->next;
        free(tmp);
    }
    p=head=tmp=NULL;
}

struct cell* delCell(struct cell *head,int n)
{
/*


*/
}

int main(void)
{
    struct cell*head;
    int num,n;
    scanf("%d",&num);
    head=build(num);
    scanf("%d",&n);
    head=delCell(head,n);
    print(head);
    release(head);
    return 0;
}

​

题解

#include <stdio.h>
#include <malloc.h>
struct cell
{
    int x;
    char name[1000];//字符数字储存姓名
    int age;
    struct cell* next;
};

struct cell *build(int num)//输入链表
{
	struct cell *tmp;
    struct cell *headA = (struct cell*)malloc(sizeof(struct cell));
	scanf("%d %s %d",&headA->x,headA->name,&headA->age);//注意字符串的输入方法
    struct cell *end = headA;
	for(int i=0; i<num-1; i++)//输入num个元素
	{
		tmp = (struct cell*)malloc(sizeof(struct cell));
	    scanf("%d %s %d",&tmp->x,tmp->name,&tmp->age);
		end->next = tmp;
		end = tmp; 
	}
	end->next = NULL;
	return headA;
}

void print(struct cell* head)//输出链表
{
    struct cell* p;
    p=head;
    while(p!=NULL)
    {
        printf("%d %s %d\n",p->x,p->name,p->age);
        p=p->next;
    }
}
void release(struct cell* head)//释放链表所占用的空间
{
    struct cell *p,*tmp;
    p=tmp=head->next;
    while(p!=NULL)
    {
        tmp=p;
        p=p->next;
        free(tmp);
    }
    p=head=tmp=NULL;
}

struct cell* delCell(struct cell *head,int n)
{
    while(head->age==n)//当头节点的值要删除时,将头节点向后挪
        head=head->next;
    struct cell *p,*p0;
    p=head;
    while(p!=NULL)
    {
        if(p->age==n)//删除节点的标准操作
        {
            p0->next=p->next;
            p=p0;
        }
        p0=p;
        p=p->next;
    }
    return head;
}


int main(void)
{
    struct cell*head;
    int num,n;
    scanf("%d",&num);
    head=build(num);
    scanf("%d",&n);
    head=delCell(head,n);
    print(head);
    release(head);
    return 0;
}


 

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年12月20日
下一篇 2023年12月20日

相关推荐