实验作业3,这次我觉得我还行
1
#include<stdio.h> #include<math.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) != EOF) { if (a == 0) printf("not quadratic equation.\n\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=%.2f,x2=%.2f\n\n", x1, x2); } else { real = -b / (2 * a); imag = sqrt(-delta) / (-2 * a); printf("x1=%.2f+%.2fi,x2=%.2f-%.2f\n\n", real, imag, real, imag); } } printf("Enter a,b,c"); } return 0; }
实验2
#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 10 int main() { int x, n; srand(time(0)); n = 0; do { n++; x = rand()%10 ; printf("%3d\n", x); }while(n < N); printf("\n"); return 0; }
3
#include<math.h> #include<stdio.h> int main() { int n,a,m; a = 0; for (n = 101; n <= 200; n++) { for (m = 2; m <= sqrt(n); ++m) if (n % m == 0) break; if (m > sqrt(n)) { printf("% d\t", n); a++; if (a % 5 == 0) printf("\n"); } } printf("\n"); printf("101~200之间共有%d个素数", a); return 0; }
4思路:(1)此数对10取余,得到最末位,将末位对2取余,若为1,则为奇数,
(2)第一次取乘1,第二次乘10,以此类推。
#include<stdio.h>
int main() {
int a,s,t,m;
m = 1;
t = 0;
printf("Enter a number: ");
while (scanf("%d", &s) != EOF) {
while (s >= 1)
{
a = s % 10;
s = s / 10;
if (a % 2 != 0)
{
t = t + a * m;
m = m * 10;
}
}
printf("new number is %d\n", t);
printf("Enter a number: ");
t = 0;
m = 1;
}
return 0;
}
实验5
#include<stdio.h> int main() { int n,a,q; float s,x; s = 0; x = 1; q = 1; a = 1; printf("Enter n(0`10):"); while(scanf("%d", &n) != EOF) { for (; a <= n; a++) { q = (-1) * q * a; s = s - x / q; } printf("n=%d, s=%f\n\n", q, s); printf("Enter n(0`10):"); } return 0; }
实验6
#include<stdio.h> #include<stdlib.h> #include<time.h> int main() { int x, a, b, c; srand(time(0)); x = (rand() % 31 + 1); printf("猜猜2020年12月那一天是你的luck day\n\n"); printf("开始喽,你有三次机会,猜吧(1~31):"); scanf("%d", &a); printf("\n\n"); if (a == x) { printf("luck day"); } else if (a > x) { printf("你猜的日子晚了,luck day悄悄溜到前面了\n\n"); printf("再猜(1~31):"); scanf("%d", &b); printf("\n\n"); if (b == x) { printf("luck day"); } else if (b > x) { printf("你猜的日子晚了,luck day悄悄溜到前面了\n\n"); printf("再猜(1~31):"); scanf("%d", &c); printf("\n\n"); if (c == x) { printf("luck day"); } else if (c > x) { printf("你猜的日子晚了,luck day悄悄溜到前面了\n\n"); printf("次数用完啦。偷偷告诉你:12月,你的luck day是%d号", x); } else { printf("你猜的日期早了, luck day还没到呢\n\n"); printf("次数用完啦。偷偷告诉你:12月,你的luck day是%d号", x); } } else { printf("你猜的日期早了, luck day还没到呢\n\n"); scanf("%d", &c); if (c == x) { printf("luck day"); } else if (c > x) { printf("你猜的日子晚了,luck day悄悄溜到前面了\n\n"); printf("次数用完啦。偷偷告诉你:12月,你的luck day是%d号", x); } else { printf("你猜的日期早了, luck day还没到呢\n\n"); printf("次数用完啦。偷偷告诉你:12月,你的luck day是%d号", x); } } } else { { printf("你猜的日期早了, luck day还没到呢\n\n"); scanf("%d", &b); if (b == x) { printf("luck day"); } else if (b > x) { printf("你猜的日子晚了,luck day悄悄溜到前面了\n\n"); printf("再猜(1~31):"); scanf("%d", &c); printf("\n\n"); if (c == x) { printf("luck day"); } else if (c > x) { printf("你猜的日子晚了,luck day悄悄溜到前面了\n\n"); printf("次数用完啦。偷偷告诉你:12月,你的luck day是%d号", x); } else { printf("你猜的日期早了, luck day还没到呢\n\n"); printf("次数用完啦。偷偷告诉你:12月,你的luck day是%d号", x); } } else { printf("你猜的日期早了, luck day还没到呢\n\n"); scanf("%d", &c); if (c == x) { printf("luck day"); } else if (c > x) { printf("你猜的日子晚了,luck day悄悄溜到前面了\n\n"); printf("次数用完啦。偷偷告诉你:12月,你的luck day是%d号", x); } else { printf("你猜的日期早了, luck day还没到呢\n\n"); printf("次数用完啦。偷偷告诉你:12月,你的luck day是%d号", x); } } } } return 0; }
实验完成挺顺利的,但交作业的时候不知道为什么,一直在一个插入的c语言框里插入,无限套娃❓