C语言靠前必备基本算法

2015-7-10 海滨 程序就是一个世界

1.最大公约数【欧几里得算法】、最小公倍数【这两个数相乘除以最大公约数】

#include<stdio.h>
void CommonFactorMultiple(int m, int n, int *p, int *q);


int main()
{
	//factor最大公约数 multiple 最小公倍数
	int m, n, factor, multiple;
	printf("请输入两个自然数(以空格隔开):");
	scanf("%d%d",&m,&n);
	CommonFactorMultiple(m, n, &factor, &multiple);
	printf("%d和%d的最大公约数是%d\n", m, n, factor);
	printf("%d和%d的最小公倍数数是%d\n", m, n, multiple);
	return 0;				//将0返回操作系统,表示程序正常结束
}
void CommonFactorMultiple(int m, int n, int *p, int *q)
{
	int mTemp = m, nTemp = n;
	int r = m%n;
	while (r!=0)
	{
		m = n;
		n = r;
		r = m%n; 
	}
	*p = n;
	*q = mTemp*nTemp / n;
}

2.水仙花数【水仙花数是指一个三位数,其各位数字的立方和等于该数本身。例如153】

#include<stdio.h>
int isFloowerNum(int m);


int main()
{
	int m;
	printf("请输入一个三位数:");
	scanf("%d", &m);
	if (isFloowerNum(m))
		printf("%d是水仙花数", m);
	else
		printf("%d不是水仙花数", m);
	return 0;				//将0返回操作系统,表示程序正常结束
}

int isFloowerNum(int m)
{
	int ge, shi, bai;
	bai = m / 100;
	m %= 100;
	shi = m / 10;
	ge = m % 10;
	bai = bai*bai*bai;
	shi = shi*shi*shi;
	ge = ge*ge*ge;
	//还原m
	m = 100 * bai + m;
	if (bai + shi + ge == m)
		return 1;
	else
		return 0;

}
}

3.鸡兔同笼【鸡有2只脚,兔子有4只脚,假设笼子里共有M只头N只脚,问鸡和兔子各有多少?】

数学思维
x+y=M
2z+4y=N

#include<stdio.h>
#define M 10
#define N 30

int main()
{
	int rabbit, chicken, flag = 0;
	//循环逐个判定
	for (rabbit = 0; rabbit <= M; rabbit++) { chicken = M - rabbit; if (4 * rabbit + 2 * chicken == N) { printf("兔子共%d只,鸡共%d只", rabbit, chicken); return 0; //将0返回操作系统,表示程序正常结束 } } printf("无解"); return 0; //将0返回操作系统,表示程序正常结束 } 

4.n!【递归】

#include<stdio.h>
int Fac(int n);

int main()
{
	int m, n = 4;
	m = Fac(n);
	printf("%d! = %d\n", n, m);
	return 0; 	//将0返回操作系统,表示程序正常结束

}


int Fac(int n)
{
	int fac;
	if (n == 1 || n == 0)
		fac = 1;
	else
		fac = n * Fac(n - 1);
	return fac;
}

5.九九乘法表

#include<stdio.h>
int Fac(int n);

int main()
{
	int i,j;
	for (i = 1; i <= 9; i++)
	{
		for (j = 1; j <= i; j++)
			printf("%dX%d=%d ", i, j, i*j);
		printf("\n");
	}
	
	return 0; 	//将0返回操作系统,表示程序正常结束

}

6.百元百鸡

#include<stdio.h>

int main()
{
	int cocks, hens, chicks;
	cocks = 0;
	while (cocks<=19)
	{
		hens = 0;
		while (hens<=33)
		{
			chicks = 100.0 - cocks - hens;
			if (5.0*cocks + 3.0*hens + chicks / 3.0 == 100.0)
				printf("公鸡有%d只,母鸡有%d只,小鸡有%d只",cocks,hens,chicks);
			hens++;
		}
		cocks++;
	}
	
	return 0; 	//将0返回操作系统,表示程序正常结束

}

7.三角形面积

#include<stdio.h>
#include<math.h>


double TriAngle(double a, double b, double c);

int main()
{
	double x, y, z, area;
	printf("请输入三角形三条边的边长(以空格间隔):");
	scanf("%lf%lf%lf", &x, &y, &z);
	area = TriAngle(x, y, z);

	if (area == 0)
		printf("输入的数据不能构成三角形");
	else
		printf("三角形的面积为:%6.2f\n", area);
	return 0; 	//将0返回操作系统,表示程序正常结束
}

8.哥德巴赫猜想【含有素数判定】(任意一个大于2的偶数都可以分解为两个素数之和。)


#include<stdio.h>


int isPN(int n);
int isEven(int n);

int main()
{
	int num, n1, n2;
	printf("请输入一个偶数:");
	scanf("%d", &num);
	if (!isEven(num))
	{
		printf("这不是一个偶数");
		return 0;
	}

	for (n1 = 2; n1 <= num / 2; n1++)
	{
		n2 = num - n1;
		if (isPN(n2) && isPN(n1))
			printf("%d可分解为%d,%d\n", num, n1, n2);
	}

	return 0; 	//将0返回操作系统,表示程序正常结束
}

int isEven(int n)
{
	if (n % 2 == 0)
		return 1;
	else
		return 0;
}


int isPN(int n)
{
	int i;
	for (i = 2; i <= n / 2; i++)
	{
		if (n % i == 0 && n != 2)
			return 0;
	}
	return 1;
}


9.矩阵对角线之和

#include<stdio.h>

/*
1 2 3
4 5 6
7 8 9
*/

//矩阵对角线之和
int main()
{
	int num[3][3] = {1,2,3,4,5,6,7,8,9},i,j,count=0;
	for (i = 0; i < 3; i++)
		for (j = 0; j < 3; j++)
			if (j == i || i + j == 3 - 1)
				count += num[i][j];

	printf("%d", count);

	return 0; 	//将0返回操作系统,表示程序正常结束
}

10.字符统计

#include<stdio.h>

void CharCount(char str[], int *p, int *q);

int main()
{
	char ch[80];	//定义一个尽可能长的字符数组以接收从键盘输入的字符
	int countSum = 0, countNoSpace = 0;
	printf("请输入一个字符串:");
	gets(ch);	//注意不能用scanf函数 因为要接受空格
	CharCount(ch, &countSum, &countNoSpace);
	printf("字符数(计空格):%d\n", countSum);
	printf("字符数(不计空格):%d\n", countNoSpace);


	return 0; //将0返回操作系统,表示程序正常结束

}

void CharCount(char str[], int *p, int *q)
{
	int count1 = 0, count2 = 0, i;
	for (i = 0; str[i] != '\0'; i++)
	{
		count1++;
		if (str[i] != '\40')  //空格的ASCII码为32,相当于八进制数40
			count2++;
	}
	*p = count1;
	*q = count2; //保存运算结果
	return; //结束函数的执行
}

标签: C语言备考

发表评论:


Powered by 海滨Blog
sitemap