C语言算法和数组

1、C语言算法

if (i = 2)

{

语句;

}

//这不是判断,是赋值,结果为真

default 可以放在switch任意位置

tab/shift tab 缩进/对齐

char 也属于整型家族的,因为字符存储的时候通过ASII码存储的

case中没有brak继续下面的case程序

switch( c )     c为int long char   不能用float

#define _CRT_SECURE_NO_WARNINGS	
//将3个整数从大到小排序
#include <stdio.h>
void Swap(int* px, int* py)
{
	int temp = &px;
	*px = *py;
	*py = temp;

}
int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	scanf("%d %d %d", &a, &b, &c);
	//调整
	//Swap(&a , &b)
	if (a < b)
	{
		int temp = a;
		a = b;
		b = temp;
	}
	//Swap(&b,&c)
	if (a < c)
	{
		int temp = a;
		a = c;
		c = temp;
	}
	printf("%d %d %s\n", a, b, c);
	return 0;
}

//打印1--100之间的所有3的倍数

#include <stdio.h>
//打印1--100之间的所有3的倍数
int main()
{
	for (int i = 1; i < 101; i++)
	{
		if (i % 3 == 0)
		{
			printf("%d ", i);
		}
	}
	return 0;
}

//求两个数的最大公约数

#define _CRT_SECURE_NO_WARNINGS	
#include <stdio.h>
//求两个数的最大公约数
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	int min = (a < b) ? a : b;
	int m = min;
	while (1)
	{
		if (a % m == 0&& b % m== 0)
		{
			break;
		}
		m--;
	}
	printf("%d\n", n);
	return 0;
}
//暴力求解,不能高效,时间要使用长

辗转相除法

24 % 18  = 6;

18 %  6   = 0;=> 6

#define _CRT_SECURE_NO_WARNINGS	
#include <stdio.h>
//求两个数的最大公约数
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	while (a % b)
	{
		int c = a % b;
		a = b;
		b = c;
	}
	printf("%d\n", b);
	return 0;
}

//最小公倍数  =   a   ×   b    ÷   最大公因数

while(真)

{

    语句;//if为假,则break

}

//编写1--100中出现多少个9

#include <stdio.h>
//编写1--100中出现多少个9
int main()
{
	int count = 0;
	for (int i = 0; i < 101; i++)
	{
		//判断各位是不是9
		if (i % 10 == 9)
		{
			count++;
		}if (i / 10 == 9)
		{
			count++;
		}
	}
	printf("count = %d\n", count);
	return 0;
}

//计算1/1-1/2+1/3-1/4...-1/100

#include <stdio.h>
int main()
{
	double sum = 0;
	int flag = 1;
	for (int i = 1; i < 101; i++)
	{
		sum = sum + flag * (1.0 / i);
		flag = - flag;
	}
	printf("%lf\n", sum);
	return 0;
}

//求10个整数中的最大值

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

//在屏幕上输出9×9乘法表

#include <stdio.h>
int main()
{
	for (int i = 1; i < 10; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			printf("%d×%-2d = %d\t", i, j, i * j);
		}
		printf("\n");
	}
	return 0;
}

 

void fun(int x)
{
//申请了空间
//使用空间
//释放空间
}

main()函数可以任意位置

栈区:局部变量,形式参数,返回值(临时用的)

堆区:动态内存分配,malloc,free,calloc,realloc

静态区:静态变量,全局变量

{   } 块中的变量不能再函数内部块外部访问

void test(int arr[])
{
	arr[0] = 3;    //arr[0] ->  *(arr+0)
	arr[1] = 4;    //arr[1] ->  *(arr+1)
}

2、数组

数组的创建,数组是相同类型的集合

     type_t     arr_name

int arr[10];

char ch[5];

double date[20];

double date2[15+5];

int n = 10;

int arr[n];     只能在支持C99编译器下能运行,C99标准之前,数组的大小必须是常量或者常量表达式,在C99之后,数组的大小可以是变量,为了支持变长数组

int  arr[10] = {1,2,3,4};//不完全初始化,剩余的元素默认初始化为零

=>int arr[10] = {1,2,3,4,0,0,0,0,0,0};

char ch[10]= {'a','b','c'};后面默初始化为'\0'  =>  a  b  c  \0  \0  \0  \0  \0  \0  \0

char ch2[10] = "abc";        =>   a  b  c  \0  \0  \0  \0  \0  \0  \0

char ch3[] = "abc";     =>  'a'   'b'     'c'      '\0'

int arr[] = {1,2,3,4,5,6,7,8,9,10};

 

#include <stdio.h>
int main()
{
	int arr[10] = { 0 };
	int size = sizeof(arr) / sizeof(arr[0]);
	for (int i = 0; i < size; i++)
	{
		printf("%d ", arr[i]);
	}
	//打印数组的每一个元素
	for (int i = 0; i < size; i++)
	{
		printf("&arr[% d] = % p\n", i, &arr[i]);
	}
	//数组在内存中是连续存放的,而且是递增的
	return 0;
}

 可以看出元素1和元素2相差4个字节,也就是int的大小。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值