代码练手4_23 喝汽水、数组出现两次的数、模拟strcpy,strcat

  1. 一个数组中只有两个数字是出现一次,其他所有数字都出现了两次。
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include <stdio.h>
#define SIZE(a) (sizeof(a) / sizeof(a[0]))

int main()
{
	int a[] = { 8, 10, 8, 2, 11, 9, 12, 11, 9, 2 };
	int i, sum = 0;
	int pos;
	int num1 = 0, num2 = 0;

	for (i = 0; i < SIZE(a); i++)
	{
		sum ^= a[i];
	}

	for (i = 0; i < 32; i++)
	{
		if (sum & 1 << i)
		{
			pos = i;
			break;
		}
	}

	for (i = 0; i < SIZE(a); i++)
	{
		if (a[i] & 1 << pos)
		{
			num1 ^= a[i];
		}
		else
		{
			num2 ^= a[i];
		}
	}

	printf("%d %d\n", num1, num2);
	system("pause");
	return 0;
}
  1. 喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以多少汽水。
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define  START_MONEY 20
int main (){
   int bottle = 0;
   int alreadydrink = 0;
   int money = START_MONEY;
   while (money > 0) {
   	alreadydrink += money;
   	bottle += money;
   	money = bottle / 2;
   	bottle %= 2;
   }
   printf("you can drink %d bottle of drink\n", alreadydrink);
   system("pause");
   return 0;
}

经典奥数题啊,答案是39,当然借一个瓶子可以多白嫖一瓶233333

  1. 模拟实现strcpy,strcat
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

char* Strcat(char* dest, char* source) {
	int i , j;
	for (i = 0; dest[i] != '\0'; ++i);
	for (j = 0; source[j] != '\0'; ++i, ++j) {
		dest[i] = source[j];
	}
	dest[i] = '\0';
	return dest;
}

char* Strcpy(char* dest, char* source) {
	int i;
	for (i = 0; source[i-1] != '\0'; ++i) {
		dest[i] = source[i];
	}
	return dest;
}

int main() {
	char str1[1024] = "kishere";
	char str2[1024] = " is god!";
	Strcat(str1, str2);
	printf("%s\n", str1);
	Strcpy(str2, str1);
	printf("%s\n", str2);
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值