|
5鱼币
本帖最后由 liyifeng295641 于 2024-1-3 15:45 编辑
1. 输入一元二次方程的三个系数 a,b,c,求出方程的根,如果没有跟显示 Error。
2. 把一个整数数组改为逆序,并将结果输出,例如:原数组a[N]={1,2,3,4,5},变为a[N]={5,4,3,2,1}。
3. 从键盘上输入若干个整数,其值在 0-9 之间,用-1 作为结束输入的标记,统计 0-9 中各个数
的个数,并输出。
这样行吗?
- #include <stdio.h>
- #include <math.h>
- // 一元二次方程求解
- void solve_quadratic_equation() {
- double a, b, c, determinant, root1, root2;
- printf("Enter coefficients a, b and c: ");
- scanf("%lf %lf %lf", &a, &b, &c);
- determinant = b*b - 4*a*c;
- if (determinant > 0) {
- root1 = (-b + sqrt(determinant)) / (2*a);
- root2 = (-b - sqrt(determinant)) / (2*a);
- printf("Roots are real and different:\n%.2lf\n%.2lf\n", root1, root2);
- }
- else if (determinant == 0) {
- root1 = -b/(2*a);
- printf("Roots are real and same:\n%.2lf\n", root1);
- }
- else {
- printf("Error\n");
- }
- }
- // 逆序整数数组
- void reverse_array() {
- int n, i;
- printf("Enter the number of elements: ");
- scanf("%d", &n);
- int arr[n];
- printf("Enter %d elements:\n", n);
- for(i = 0; i < n; i++) {
- scanf("%d", &arr[i]);
- }
- printf("Reversed array: ");
- for(i = n - 1; i >= 0; i--) {
- printf("%d ", arr[i]);
- }
- printf("\n");
- }
- // 统计数字次数
- void count_numbers() {
- int num, count[10] = {0};
- printf("Enter numbers between 0 and 9 (enter -1 to end): ");
- while(1) {
- scanf("%d", &num);
- if(num == -1)
- break;
- if(num >= 0 && num < 10)
- count[num]++;
- }
- printf("Count of numbers:\n");
- for(int i = 0; i < 10; i++) {
- printf("%d: %d\n", i, count[i]);
- }
- }
- int main() {
- solve_quadratic_equation();
- reverse_array();
- count_numbers();
- return 0;
- }
复制代码
|
|