当前位置:网站首页>浙大版《C语言程序设计(第3版)》题目集 练习7-4 找出不是两个数组共有的元素
浙大版《C语言程序设计(第3版)》题目集 练习7-4 找出不是两个数组共有的元素
2022-04-22 06:11:00 【InTheSolitude】
浙大版《C语言程序设计(第3版)》题目集 练习7-4 找出不是两个数组共有的元素
自己做的真的很屎,别喷我我啥都没学!!!
简述题目:[7-4]https://pintia.cn/problem-sets/12/problems/318
给定两个整型数组,本题要求找出不是两者共有的元素。
输入格式:
输入分别在两行中给出两个整型数组,每行先给出正整数N(≤20),随后是N个整数,其间以空格分隔。
输出格式:
在一行中按照数字给出的顺序输出不是两数组共有的元素,数字间以空格分隔,但行末不得有多余的空格。题目保证至少存在一个这样的数字。同一数字不重复输出。
输入样例:
10 3 -5 2 8 0 3 5 -15 9 100
11 6 4 8 2 6 -5 9 0 100 8 1
输出样例:
3 5 -15 6 4 1
易错提示:不是公共的,在数组内部可以重复(我第一次就错在没读题)
思路:判断条件,标记输出。
上代码
#include <stdio.h>
typedef struct array{
int data;
int flag;
}array;//因为要输出负数就不拿数组标记了,
//用结构体去实现我的想法,简单。
//----------------------------
//两个几乎一样的区间查找函数
int search1(array*c,int start,int end,int num)
{
int i,ret=0;
for(i=start;i<end;i++)
{
if((c+i)->data==num){
//(c+i)->flag=0;这个区间查重是不会覆盖已有标记的
ret=1;
}
}
return ret;
}
int search2(array*c,int start,int end,int num)
{
int i,ret=0;
for(i=start;i<end;i++)
{
if((c+i)->data==num){
(c+i)->flag=0;//这个区间查重是会覆盖已有标记的!
ret=1;
}
}
return ret;
}
int main()
{
//--------开始读入-----------
int m,n,i;
scanf("%d",&m);
int a[m];
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&n);
int b[n];
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
//以上遍历读取了两个数组
//----接下来顺序合并成一个结构体数组去处理! -------
int sum=m+n;
array c[sum];
for(i=0;i<m;i++)
{
c[i].data=a[i];
c[i].flag=1;
if(search1(c,0,i,c[i].data)==1) c[i].flag=0;
//只在第一个数组内查重,即使重复也要保留一个
}
for(i=m;i<sum;i++)
{
//开始处理第二个数组,涉及组内查重和组间查重!
c[i].data=b[i-m];
c[i].flag=1;
if(search1(c,m,i,c[i].data)==1) c[i].flag=0;//组内查重
if(search2(c,0,m,c[i].data)==1) c[i].flag=0;
//从头开始所有数组内查重,如果重复全部删除
}
//---------最后输出-----------------
int first=1;//避免行末多余空格的基操
for(i=0;i<sum;i++)
{
if(c[i].flag==1){
if(first==1){
printf("%d",c[i].data);
first=0;
}else{
printf(" %d",c[i].data);
}
}
}
return 0;
}
反思:如果多个组之间找不重叠部分那就除过第一组每次新增的组先1.组内查重2.组间删重
版权声明
本文为[InTheSolitude]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45809066/article/details/104399737
边栏推荐
- What is the learning route of switching to digital IC Verification? Is there time to start turning now? 985 master's degree, second year of Graduate School of Electronics
- Parseexception unparseable date time conversion exception
- STM32 learning record 0005 - JLINK download and debugging
- 【Bug小记】页面刷新后获取不到sessionstorage数据
- Activity支持多窗口显示
- 如何成为IC验证工程师?
- Introduction to IC Analog Layout - learning notes on layout Basics (3)
- STM32 timer synchronization trigger code experimental verification and sharing
- .NET日常想法随手记------比较两个json文件并获取标签相同但值却不同的数据
- leetcode 617 合并二叉树
猜你喜欢

Introduction to IC Analog Layout - learning notes on layout Basics (5)

paging

集成电路模拟版图入门-版图基础学习笔记(一)

Nacos集群架构

ASP.NET日常开发随手记------iis服务器支持下载apk

Introduction to IC Analog Layout - learning notes on layout Basics (I)

RSS入坑指南

Lcfnet series can to optical fiber Ethernet equipment realizes ultra long-distance optical fiber communication between can networks

旋转选择器 WheelPicker 的使用

用大写的字段接受最终首字母却变小写
随机推荐
ASP.NET日常开发随手记------后台执行js脚本
Difference between analog IC design and digital IC design, including salary table
Leetcode 1218: Longest definite difference subsequence
MATLAB:女声转男声
集成电路模拟版图入门-版图基础学习笔记(二)
ASP.NET日常开发随手记------发送邮件
Parseexception unparseable date time conversion exception
实验室安全考试
Li Kou 299: number guessing game
腾讯云对象存储服务的使用
ASP.NET日常开发随手记------导出Excel
Can bus record diagnostic assistant
Distributed task scheduling and computing framework: powerjob advanced features - container 03
STM32 timer ocref output configuration timx - > CCER
如何成为IC验证工程师?
Digital IC design and CS?
Nacos service provider registration
微信支付 iframe子页面 无响应
Does microelectronics major make chips? What is the chip related to?
力扣299:猜数字游戏