解一元二次方程:
# include <stdio.h> # include <math.h> int main(void) { double a; double b; double c; double x1; double x2; double det; char ch; do { printf("请输入a b c 三个系数的值:\n"); printf("a = "); scanf("%lf", &a); printf("b = "); scanf("%lf", &b); printf("c = "); scanf("%lf", &c); det = b*b - 4*a*c; if (det > 0) { x1 = (-b + sqrt(det)) / 2*a; x2 = (-b - sqrt(det)) / 2*a; printf("方程有两个解:x1 = %lf x2 = %lf\n", x1, x2); } else if(det == 0) { x1 = (-b + sqrt(det))/2*a; printf("方程有一个解: x1 = x2 = %lf\n", x1); } else { printf("方程没有解\n"); } printf("您想继续吗?(Y/N)\n"); scanf(" %c", &ch); //ch前必须加一空格 (空白符问题) }while ('y' == ch || 'Y' == ch); return 0; }
# include <stdio.h> int main(void) { int i; printf("请输入您的成绩:"); scanf("%d", &i); if (i > 100) printf("一切皆有可能!!!\n"); else if(i >= 90 && i <= 100) printf("您的等级是:优秀\n"); else if (i >= 80 && i < 90) // <= 所得结果为一逻辑值,是布尔型 其值为0或1,永远小于90,即此式永远成立! 其值永远为真(1) printf("您的等级是:良好\n"); else if (i >= 60 && i < 80) printf("您的等级是:及格\n"); else if (i >= 0 && i < 60) printf("抱歉!您的等级是:不及格\n"); else printf("请不要开玩笑!\n"); return 0; }
# include <stdio.h> int main(void) { double i, j, k; printf("请输入三个数(中间以空格隔开):"); scanf("%lf %lf %lf", &i, &j, &k); if (i > j) { if (i > k) { printf("最大值为:%lf\n", i); if (j > k) printf("数的大小顺序为:%lf > %lf > %lf\n", i, j, k); else printf("数的大小顺序为:%lf > %lf > %lf\n", i, k, j); } else { printf("最大值为:%lf\n", k); printf("数的大小顺序为:%lf > %lf > %lf\n", k, i, j); } } else { if (j > k) { printf("最大值为:%lf\n", j); if (i > k) printf("数的大小顺序为:%lf > %lf > %lf\n", j, i, k); else printf("数的大小顺序为:%lf > %lf > %lf\n", j, k, i); } else { printf("最大值为:%lf\n", k); printf("数的大小顺序为:%lf > %lf > %lf\n", k, j, i); } } return 0; }
# include <stdio.h> int main(void) { int val; int m; int sum = 0; printf("请输入需要判断的数:"); scanf("%d", &val); m = val; while (m) //while循环为重点! { sum = sum * 10 + m % 10; m /= 10; } if (sum == val) { printf("此数是回文数!\n"); } else { printf("此数不是回文数!\n"); } return 0; }
# include <stdio.h> int jieCheng(int n); int main(void) { int i, n; int mul = 1; int sum = 0; printf("请输入n:"); scanf("%d", &n); for (i=1; i<=n; ++i) { mul *= i; sum += mul; } printf("阶乘之和为:%d\n", sum); return 0; } //递归 int jieCheng(int n) { int mul; if (1 == n) { return mul; } else { mul=n*jieCheng(n-1); } return mul; }
生成随机数,进行概率预测:
目前计算机中的随机数都是伪随机数
# include <stdio.h> # include <stdlib.h> # include <time.h> enum Random { Up, Down, Left }; int main(void) { int r; //定义枚举变量数组,包含10个元素,产生的随机数r作为其下标 Random rd[10] = {Up, Up, Up, Up, Up, Down, Down, Down, Left, Left}; srand((int)time(0)); //初始化随机数种子 r=(int)(rand()%9+1); //得到0到9中的一个随机数 //printf("%d\n", r); //测试随机数r //使用switch判断选择情况 switch (rd[r]) { case Up: printf("您的选择是向上走!\n"); break; case Down: printf("您的选择是向下走!\n"); break; case Left: printf("您的选择是向左走!\n"); break; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/lfhappypain/article/details/47281597