chapter3

数组的复制:

#include <string.h>
memcpy(b, a, sizeof(int)*k);
memcpy(b, a, sizeof(double)*k);
memcpy(b, a, sizeof(a));


有编号为1~n的n盏灯,第一个人把所有灯打开,第2个人按下所有编号为2的倍数的开关,第3个人按下所有编号为3的倍数的开关,

依次类推,一共有k个人,问最后有哪些灯开着?

#include <stdio.h>
int main()
{
    int n, k;
    scanf("%d%d",&n, &k);
    int a[1001] = {0};
    for(int i = 1; i <= k; i++)
        for(int j = 1; j <= n; j++)
            if(j % i == 0)
                a[j] = !a[j];
    int first = 1;
    for(int i = 1; i <= n; i++)
        if(a[i] == 1)
    {
        if(first)
        {
            first = 0;
            printf("%d", i);
        }
        else
            printf(" %d", i);
    }
    printf("\n");
    return 0;
}


memset的使用:

#include <stdio.h>
#include <string.h>
#define MAXN 1000+10
int a[MAXN];
int main()
{
    memset(a, 0, sizeof(a));
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= k; i++)
        for(int j = 1; j <= n; j++)
            if(j % i == 0)
                a[j] = !a[j];
    int first = 1;
    for(int i = 1; i <= n; i++)
        if(a[i])
    {
        if(first)
        {
            printf("%d", i);
            first = 0;
        }
        else
            printf(" %d", i);
    }
    printf("\n");
    return 0;
}


sprintf   itoa atoi函数的使用


竖式问题:找出所有形如abc*de的算式,算式中所有数字来自输入的数字集合。

#include <stdio.h>
#include <string.h>
int main()
{
    char s[20], buf[99];
    int abc, de, x, y, z, ok, count = 0, i;
    scanf("%s", s);
    for(abc = 100; abc <= 999; abc++)
        for(de = 10; de <= 99; de++)
        {
            x = abc*(de%10);
            y = abc*(de/10);
            z = abc*de;
            sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);
            ok = 1;
            for(i = 0; i < strlen(buf); i++)
                if(strchr(s, buf[i]) == NULL)
                    ok = 0;
            if(ok)
            {
                printf("<%d>\n", ++count);
                printf("%5d\nX%4d\n-----\n%5d\n%5d\n-----\n%5d\n", abc, de, x, y, z);
            }
        }
    printf("The number of solutions = %d\n", count);
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值