1.一个数组中只有两个数字是出现一次,其他所有数字都出现了两次,找出这两个数字,编程实现。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[] = {4, 8, 8, 4, 5, 9, 3, 5};
int count = 0;
int sz = sizeof(arr)/sizeof(arr[0]) - 1;
int i = 0, j = 0;
for (i=0; i<sz; i++)
{
count = 0;
for (j=0; j<sz+1; j++)
{
if (arr[i] == arr[j])
{
count ++;
}
}
if (count == 1)
{
printf("%d ", arr[i]);
}
}
system ("pause");
return 0;
}
2.喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以多少汽水。编程实现。
#include <stdio.h>
#include <stdlib.h>
int num (int n)
{
if (n != 0)
{
if (n != 1)
{
if (n%2 == 0)
{
return n + num(n/2);
;
}
else
{
return (n - 1) + num((n/2) + 1);
}
}
else
{
return 1;
}
}
else
{
return 0;
}
}
int main()
{
int n = 0;
printf ("请输入钱数:\n");
scanf ("%d", &n);
printf ("%d元可以喝%d瓶汽水!!\n", n, 2*n-1);
printf ("%d元可以喝%d瓶汽水!!\n", n, num(n));
system ("pause");
return 0;
}
3.模拟实现strcpy
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char* my_strcpy(char* dest, const char* str)
{
const char* ret = str;
assert (str);
while (*dest++ = *str++)
{
;
}
*dest = *str;
}
int main ()
{
char arr1[] = "abcde";
char arr2[10] = {'0'};
my_strcpy (arr2, arr1);
printf ("%s\n", arr2);
system ("pause");
return 0;
}
4.模拟实现strcat
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char* my_strcat (char* dest, const char* str)
{
const char* ret = str;
assert (str);
while (*dest)
{
dest++;
}
while (*dest++ = *str++)
{
;
}
*dest = *str;
return (char *)ret;
}
int main()
{
char arr1[] = "fdaifnd";
char arr2[10] = "bc";
my_strcat (arr2, arr1);
printf ("%s\n", arr2);
system ("pause");
return 0;
}