1.一个数组中有2个数字是出现一次,其他所有数字出现了2次。找出这两个出现一次的数字,编程实现 2.喝汽水3.模拟实现strcpy4.模拟实现strcat

博客包含三个编程相关问题。一是找出数组中仅出现一次的两个数字;二是计算一定规则下喝汽水的瓶数;三是模拟实现字符串拷贝和连接库函数,均与信息技术编程紧密相关。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.一个数组中有2个数字是出现一次,其他所有数字出现了2次。找出这两个出现一次的数字,编程实现。

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int arr[] = { 1, 2, 3, 4, 1, 2, 3, 5 };
	int x = 0;
	int y = 0;
	int pos = 0;
	int tmp = 0;
	int i = 0;
	int sz = sizeof arr / sizeof arr[0];
	for (i = 0; i < sz; i++)
	{
		tmp = tmp^arr[i];
	}
	for (i = 0; i < 32; i++)
	{
		if (((tmp >> i) & 1) == 1)
		{
			pos = i;
			break;
		}
	}
	for (i = 0; i < sz; i++)
	{
		if (((arr[i] >> pos) & 1) == 1)
		{
			x = x^arr[i];
		}
		else
		{
			y = y^arr[i];
		}
	}
	printf("%d %d",x,y);
	system("pause");
	return 0;
}

2.喝汽水,1瓶汽水1元,2个空瓶子可以换一瓶汽水
求:可以喝多少瓶


#include<stdio.h>
#include<stdlib.h>
int main()
{
	int money = 20;
	int total = money;
	int empty = total;
	while (empty >= 2)
	{
		total += empty / 2;
		empty = empty / 2 + empty % 2;
	}
	printf("你可以喝%d瓶水\n", total);
	system("pause");
	return 0;
}

3.模拟实现strcpy(字符串拷贝库函数)

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *Mystrcpy(char*dest, const char *src)
{
	char*p = dest;
	assert(dest != NULL && src != NULL);
	while (*dest++ = *src++);
	return p;
}
int main()
{
	char dest[20] = {0};
	char*src = "hello";
	Mystrcpy(dest, src);
	printf("%s\n", dest);
	system("pause");
	return 0;
}

4.模拟实现strcat(字符串连接函数)


#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
char *Mystrcat(char *dest, const char *src)
{
	char *p = dest;
	assert(dest != NULL && src != NULL);
	while (*dest != '\0')
	{
		dest++;
	}
	while (*dest++ = *src++);
	return p;
}
int main()
{
	char dest[30] = "hello ";
	char *src = "tianjia";
	Mystrcat(dest, src);
	printf("%s\n", dest);
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值