- 一个数组中只有两个数字是出现一次,其他所有数字都出现了两次。
#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元,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
- 模拟实现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;
}