当前位置: 首页 > news >正文

【知识碎片】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-

http://www.lryc.cn/news/348244.html

相关文章:

  • Day53代码随想录动态规划part13:300.最长递增子序列、674. 最长连续递增序列、718. 最长重复子数组
  • 自己动手为wordpress注册一个Carousel轮播区块
  • 基于Springboot的实习生管理系统(有报告)。Javaee项目,springboot项目。
  • 良心实用的电脑桌面便利贴,好用的便利贴便签小工具
  • Eayswoole 报错 crontab info is abnormal
  • 移动 App 入侵与逆向破解技术-iOS 篇
  • 2024服贸会,参展企业媒体宣传报道攻略
  • CI/CD笔记.Gitlab系列.新用户管理
  • 前端 JS 经典:JS 基础类型和 typeof
  • Java入门基础学习笔记11——关键字和标识符
  • 设计模式-解释器模式(Interpreter)
  • 机器视觉任务中语义分割方法的进化历史
  • Java并发编程: Synchronized锁升级
  • Atcoder C - Routing
  • 升级! 测试萌新Python学习之连通数据库Pymsql增删改及封装(四)
  • 【大数据】containered学习笔记
  • 「TypeScript」TypeScript入门练手题
  • k8s 使用Docker和Containerd对比分析
  • MySQL 通过 systemd 启动时 hang 住了……
  • pat乙1033-旧键盘打字
  • Ubuntu安装VScode
  • c# - - - winform程序四个角添加圆角效果
  • Springboot 集成 Consul 实现服务注册中心-05
  • 【软考高项】四十六、项目管理科学计算之运筹学
  • 使用 Python 和 OpenCV 进行实时目标检测的详解
  • Android build.prop生成过程源码分析
  • 计算机网络教材——谢希仁教材与配套PPT课件和《计算机网络——自顶向下方法》
  • mysql 离线安装
  • 【C++】 string类:应用与实践
  • 巩固学习7