循环小示例,菲波拉契序列,循环解一元二次方程以及switch示例程序

# include <stdio.h>

int main(void)
{
	int n;
	int i;
	int f1, f2, f3;

	f1 = 1;
	f2 = 1;

	printf("请输入您需要求的想的序列:");
	scanf("%d", &n);

	for (i=3; i<n; i++)
	{
		f3 = f1 + f2;
		f1 = f2;
		f2 = f3;
		printf("%d ", f3);
	}

	return 0;
}

 

一元二次方程求解:

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

			int main(void)
			{	
				double a, b, c;
				double delta;
				double x1, x2;
				char ch;
				
				do {
					printf("请输入一元二次方程的三个系数:\n");
					printf("a = ");
					scanf("%lf", &a);
					
					printf("b = ");
					scanf("%lf", &b);
					
					printf("c = ");
					scanf("%lf", &c);
					
					delta = b*b - 4*a*c;
					
					if (delta > 0)
					{
						x1 = (-b + sqrt(delta))  / (2*a);
						x2 = (-b - sqrt(delta)) / (2*a);
						printf("有两个解,x1 = %f, x2 = %f\n", x1, x2);
					}
					else if (0 == delta)
					{
						x1 = x2 = (-b) / (2*a);
						printf("有唯一解,x1 = x2 = %f\n", x1, x2);
					}
					else
					{
						printf("无实数解!");
					}

					printf("您想继续么(Y/N):");
					scanf(" %c", &ch);	//%c前面必须得加一个空格 原因略,这是一个空白符的问题

				} while ('y'==ch || 'Y'==ch);
				
				return 0;
			}

 

switch 示例:

# include <stdio.h>

int main(void)
{
	int val;
	
	printf("请输入您要进入的楼层:");
	scanf("%d", &val);
	
	switch (val)
	{
	case 1:
		printf("1层开!\n");
		break;
	case 2:
		printf("2层开!\n");
		break;
	case 3:
		printf("3层开!\n");
		break;
	default:
		printf("还没盖到这一层!\n");
		break;
	}
	
	return 0;
}

 

你可能感兴趣的:(c,算法)