做民宿哪个网站好,美团网站开发合作商,公司网站布局,WordPress在哪里添加备案本人能力有限#xff0c;发出只为帮助有需要的人。
以下为实验课的复盘#xff0c;内容会有大量失真#xff0c;请多多包涵。
1.双手剑士的最优搭配
每把剑有攻击力和防御力两个属性。双手剑士可以同时拿两把剑#xff0c;其得到攻击力为两把剑中的攻击力的最大值#…本人能力有限发出只为帮助有需要的人。
以下为实验课的复盘内容会有大量失真请多多包涵。
1.双手剑士的最优搭配
每把剑有攻击力和防御力两个属性。双手剑士可以同时拿两把剑其得到攻击力为两把剑中的攻击力的最大值防御力为两把剑中的防御力的最小值。现在想让双手剑士的攻击力和防御力之和最大。输入n作为剑的个数再输入2n分别对应每把剑的攻击力和防御力要求输出最优解为哪两把剑当攻击力和防御力之和相同时优先选择编号靠前的两把剑
输入3 10 4 5 10 7 8
输出1 2
题解为
#include stdio.h
int max(int x,int y)//最大值函数
{if(xy)return x;return y;
}
int min(int x,int y)//最小值函数
{if(xy)return x;return y;
}
int main(void)
{int a[100][1]{0},n;//设置二维数组scanf(%d,n);for(int i0;in;i)scanf(%d %d,a[i][0],a[i][1]);//a[i][0]为攻击力a[i][1]为防御力int maxSum0,attack0,defend0;int x,y;for(int i0;in;i)//二重循环遍历{for(int ji1;jn;j){defendmin(a[i][1],a[j][1]);attackmax(a[i][0],a[j][0]);if(defendattackmaxSum){maxSumdefendattack;//找当前的最优解xi;yj;//用xy记录}}}printf(%d %d,x1,y1);//注意此题从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(i0;in;i)scanf(%d,a[i]);for(i0;in;i){flag0;while(a[i]!0)//将数组的数字化成二进制{if(a[i]%21)flag;//计算二进制中1的数量a[i]/2;b[i]flag;//用两个数组}}for(i0;in;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.hint main(void)
{int temperature[7],i,j;for(i0;i7;i)scanf(%d,temperature[i]);int days[7]{0};for(i0;i7;i)/**/for(i0;i7;i)printf(%d ,days[i]);return 0;
}
解答
#include stdio.h
#include stdlib.hint main(void)
{int temperature[7],i,j;for(i0;i7;i)scanf(%d,temperature[i]);int days[7]{0};//考了数组初始化但对此题的作答无影响for(i0;i7;i){int flag0;for(ji1;j7;j)//两层循环{flag;if(temperature[j]temperature[i]){days[i]flag;//构建新数组break;}}}for(i0;i7;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)//输入链表
{
/**/
}void print(struct cell* head)//输出链表
{struct cell* p;phead;while(p!NULL){printf(%d %s %d\n,p-x,p-name,p-age);pp-next;}
}
void release(struct cell* head)//释放链表所占用的空间
{struct cell *p,*tmp;ptmphead-next;while(p!NULL){tmpp;pp-next;free(tmp);}pheadtmpNULL;
}struct cell* delCell(struct cell *head,int n)
{while(head-agen)//当头节点的值要删除时将头节点向后挪headhead-next;struct cell *p,*p0;phead;while(p!NULL){if(p-agen)//删除节点的标准操作{p0-nextp-next;pp0;}p0p;pp-next;}return head;
}int main(void)
{struct cell*head;int num,n;scanf(%d,num);headbuild(num);scanf(%d,n);headdelCell(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 i0; inum-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;phead;while(p!NULL){printf(%d %s %d\n,p-x,p-name,p-age);pp-next;}
}
void release(struct cell* head)//释放链表所占用的空间
{struct cell *p,*tmp;ptmphead-next;while(p!NULL){tmpp;pp-next;free(tmp);}pheadtmpNULL;
}struct cell* delCell(struct cell *head,int n)
{while(head-agen)//当头节点的值要删除时将头节点向后挪headhead-next;struct cell *p,*p0;phead;while(p!NULL){if(p-agen)//删除节点的标准操作{p0-nextp-next;pp0;}p0p;pp-next;}return head;
}int main(void)
{struct cell*head;int num,n;scanf(%d,num);headbuild(num);scanf(%d,n);headdelCell(head,n);print(head);release(head);return 0;
}