大学C语言学习笔记,c语言学习笔记

运算符优先级:

59d4a8198a1047d7e7eae56a7f918391.png

1.100以内素数筛选法:

#include#include

using namespacestd;intmain()

{int sum = 0, a[100] = { 0};for (int i = 2; i < sqrt(100.0); i++)

{

sum=i;if (a[sum] == 0)

{while (sum < 100)

{

sum+=i;if (sum < 100)a[sum] = 1;

}

}

}for (int i = 2; i < 100; i++)

{if (a[i] == 0)cout << i << " ";

}return 0;

}

2.cin && cin.get &&getchar && cin.getline

cin.get(c, 10, '

') 每次读最多9个字符,如果提前碰到结束标识符,则小于9

cin不能读空格/回车,cin.get可以;getchar不跳过任何字符

cin/cin.get 缓冲区指针停在终止字符前;

cin.getline 缓冲区指针停在终止字符后;

3.引入文件 :#include “xxx.h”

4.m个苹果放进n个盘子有多少种情况:

int count(int m, intn)

{if (m <= 1 || n <= 1)return 1;//一个苹果或者1个盘子的情况

if (m

else

//苹果数大于盘子数,分解成盘子空和盘子不空的情况

return count(m, n - 1) + count(m -n, n);

}

5.逆波兰表达式计算

doublenotation(){char str[10];

cin>>str;switch (str[0])

{case '+':return notation() +notation();case '-':return notation()-notation();case '*':return notation() *notation();case '/':return notation() /notation();default:returnatof(str);

}

}

6.汉诺塔问题

void move(int m, char x, char y, charz)

{if (m == 1)

{

cout<< "把一个盘子从" << x << "移动到" << z <

}else{

move(m- 1, x, z, y);

cout<< "把一个盘子从" << x << "移动到" << z <

move(m- 1, y, x, z);

}

}

7.二进制打印

int binary(intn)

{if (n / 2 != 0)

binary(n/ 2);if (n!=0)

cout<< n % 2;return 0;

}

8.

533300b04490fa48560418d8eb622ff8.png

先计算后置++

9.

abcfe83d57c20097016a4733259b58bb.png

原因:p2的基类型是int类型,占4个字节,不可分割,因此系统会跨过4个字节

31006c7f53caee809ca1721861df864c.png

179b490e9303b9fa6ac20ae901b8beba.png

10.指针与数组

2408878e4e6bf38ab8fe43184523cc92.png

11.

1b6bb49277ee6c14db1b11582013137d.png

数组名前加&,扩大a的管辖范围,由数组第一个元素扩大到整个数组

* (&a),缩小&a的管辖范围,由整个数组地址缩小为数组第一个元素地址;

*a,缩小a的管辖范围,由数组第一个元素地址缩小为数组第一个元素的值;

12.二维数组

级别排序:&a>a>a[0]>a[0][0]

&a:指向整个二维数组的指针

a:指向数组a[0]的指针

a[0]:指向 a[0][0]的指针

a[0][0]:数组第一个元素的值

c05bac39f02fb05444314c2e4fa7fad8.png

13.

fde8df9852d935479e21b01a2e118939.png

static int b=0; 仅在第一次调用函数的时候执行,下次直接跳过;

14.结构体应用

统计n个学生里生日相同的学生学号,并按日期顺序输出

52643904b294430f9500f507513d1148.png

注:count 数组用于记录生日是当前日期是第几个学生;j用于记录生日是当前日期的学生数量

count[k] 拿出结构体里面生日是当前日期的学生序号

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值