2018.3.17
1. 一个数组中只有两个数字是出现一次,其他所有数字都出现了两次。 找出这两个数字,编程实现。
int main()
{
int result[10] = {0};
int a[18] = {0};
int n;
int i;
int max = 0;
printf("Please input a number for counting: ");
scanf("%d", &n);
printf("Please input some numbers: ");
for(i = 0; i<n; i++)
{
scanf("%d", &a[i]);
if(a[i]>max) max = a[i];
}
while(n--)
{
for(i =0; i<=max; i++)
{
if(a[n] == i)
{
result[i]++;
break;
}
}
}
for(i = 0; i<=max; i++)
{
if(result[i] == 1)
{
printf("%d ", i);
}
}
return 0;
}
2. 喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以喝多少汽水。编程实现。
#include<stdio.h>
int main()
{
int money = 0;
int count = 0;
scanf("%d", &money);
count += money;
while(money /= 2)
{
count += money;
}
printf("%d\n", count);
return 0;
}
3. 模拟实现strcpy
#include<stdio.h>
#include<string.h>
#include<assert.h>
char *my_strcpy(char *des, const char *source)
{
char *p = des;
const char *q = source;
assert(des);
assert(source);
while(*p++ = *q++)
{
;
}
return p;
}
int main(){
char a[10] = "abcdefg";
char b[10];
memset(b, 0, 10);
my_strcpy(b, a);
printf("%s\n", b);
return 0;
}
4. 模拟实现strcat
#include<stdio.h>
#include<assert.h>
char *my_strcat(char *des, const char *source)
{
char *de = des;
const char *sou = source;
assert(des);
assert(source);
while(*de) de++;
while(*de++ = *sou++)
{
;
}
}
int main()
{
char a[15] = "Oh, my ";
char *b = "god!";
my_strcat(a, b);
printf("%s\n", a);
return 0;
}