【知识碎片】2024_05_13

本文记录了两道代码题【自除数】和【除自身以外数组的乘积】(利用了前缀积和后缀积,值得再看),第二部分记录了关于指针数组和逗号表达式的两道选择题。


每日代码


自除数

. - 力扣(LeetCode)

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int div_self(int x) {
    if (x == 0)
        return 0;
    int num = x;
    while (x) {
        int bit = x % 10;
        if (bit == 0)
            return 0;
        if (num % bit != 0)
            return 0;
        x /= 10;
    }
    return 1;
}
int* selfDividingNumbers(int left, int right, int* returnSize) {
    int* returnnums = (int*)malloc(sizeof(int)*(right - left + 1));
    int pos = 0;
    for (int i = left; i <= right; i++) {
        if (div_self(i))
            returnnums[pos++] = i;
    }
    *returnSize = pos;
    return returnnums;
}

除自身以外数组的乘积

. - 力扣(LeetCode)

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* productExceptSelf(int* nums, int numsSize, int* returnSize) {
    int *ans = malloc(sizeof(int) * numsSize);
    *returnSize = numsSize;
    ans[0] = 1;
    for (int i = 1; i < numsSize; i++) {
        ans[i] = ans[i - 1] * nums[i - 1];
    }
    int suf = 1;
    for (int i = numsSize - 1; i >= 0; i--) {
        ans[i] *= suf;
        suf *= nums[i];
    }
    return ans;
}

利用前缀积和后缀积。(很遗憾,代码是搬来的,只懂了思路,明天一定要亲自运行出来!)


C语言碎片知识 


下列程序的输出是( )

#include<stdio.h>
int main()
{
    int a [12]= {1,2,3,4,5,6,7,8,9,10,11,12},*p[4],i;
    for(i=0;i<4;i++)
    p[i]=&a [i*3];
    printf("%d\n",p[3][2]);
    return 0;
}

A: 上述程序有错误 B: 6 C: 8 D: 12

答案:D 

p是一个指针数组!数组里存放的是指针,分别存放了a[0],a[3],a[6],a[9]的地址。类似于一个四行三列的数组。


以下逗号表达式的值为( )

(x= 4 * 5 , x * 5) , x + 5;

A: 25 B: 20 C: 100 D: 45

答案:A

逗号表达式从前往后计算,结果为最后一项的值,题目等价于x=20,20*5,20+5。x*5时也并没有改变x的值。


-The End-

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王_哈_哈 Jw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值