Part1:
一元二次方程求解
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main() {
- float a, b, c, x1, x2;
- float delta, real, imag;
- printf("Enter a, b, c:");
- while(scanf("%f%f%f", &a, &b, &c)) {
- if(a == 0)
- printf("not quadratic equation.\n");
- else {
- 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 {
- real = -b/(2*a);
- imag = sqrt(-delta) / (2*a);
- printf("x1 = %f + %fi, x2 = %f - %fi\n", real, imag, real, imag);
- }
- }
- printf("Enter a, b, c:\n");
- }
- system("pause");
- return 0;
- }
猜数游戏
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main() {
- int guessNumber;
- int ans; // 存放用户猜的数字
- srand((time(0))); // 以时间函数作为随机种子
- guessNumber = 1 + rand()%100; // 生成 1~100 之间的随机数
- do {
- printf("your guess number is(1~100):");
- scanf("%d", &ans);
- if(ans <guessNumber)
- printf("%d is lower than the number.\n", ans);
- else if(ans> guessNumber)
- printf("%d higher then the number.\n", ans);
- }while(ans != guessNumber);
- printf("Congratulations. you hit it~\n");
- system("pause"); // 在 devc 中运行时, 这一行可以去掉
- return 0;
- }
Part2
输出最大最小数
- #include <stdio.h>
- #include <stdlib.h>
- int main() {
- int number, max, min, n;
- n=1;
- printf("输入第 %d 个数:", n);
- scanf("%d", &number);
- max = number;
- min = number;
- while(n<5) {
- n++;
- printf("输入第 %d 个数:", n);
- scanf("%d", &number);
- if(number>max)
- max = number;
- else if(number<min)
- min = number;
- }
- printf("最大数为: %d\n", max);
- printf("最小数为: %d\n", min);
- system("pause");
- return 0;
- }
Part3
编程输出 101-200 之间所有素数, 并输出这一区间内素数个数.
- #include<stdio.h>
- #include<math.h>
- int main(){
- int m,i,k,h=0,leap=1;
- printf("\n");
- for(m=101;m<=200;m++){
- k=sqrt(m+1);
- for(i=2;i<=k;i++){
- if(m%i==0){
- leap=0;
- break;
- }
- }
- if(leap){
- printf("%-4d",m);
- h++;
- if(h%10==0){
- printf("\n");
- }
- }
- leap=1;
- }
- printf("\nThe total is %d",h);
- }
将一个长整型数 s 的每一数位上的奇数依次取出来, 构成一个新的数, 起高位仍在高位, 低位仍在低位.
- #include<stdio.h>
- int main()
- {
- unsigned long s,t=0,p=1;
- scanf("%u",&s);
- while(s!=0)
- {
- if((s%10)%2!=0)
- {
- t=t+(s%10)*p;
- p=p*10;
- }
- s=s/10;
- }
- printf("%u\n",t);
- return 0;
- }
实验总结:
1. 符号在中英文格式下不同, 导致程序运行错误
2. 不等式顺序不同, 导致程序运行错误
number>max 对
number 在前 字母 n 在后
实验三
来源: http://www.bubuko.com/infodetail-3293420.html