2018.3.17

17 篇文章 0 订阅

2018.3.17

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

#include<stdio.h>
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值