C语言基础编程题(1月21日)


在这里插入图片描述

1.打印菱形

  (1)     (2)
   *       *
  ***     * *
 *****   *   *
******* *     *
 *****   *   *
  ***     * *
   *       *
#include <stdio.h>
#include <stdlib.h>

void print_space(int times);
void print_star(int times);

int main() {
    for (int i = 0; i < 7; i++) {
        //abs(3-i) is the offset value relative to the fourth row.
        print_space(abs(3 - i));
        print_star(7 - abs(3 - i) * 2);
        print_space(abs(3 - i));
        //each line ends with '\n'.
        printf("\n");
    }

    return 0;
}

void print_space(int times) {
    while (times-- != 0) {
        printf(" ");
    }
}
void print_star(int times) {
    while (times-- != 0) {
        printf("*");
    }
}
#include <stdio.h>
#include <stdlib.h>

void print_space(int times);

int main() {
    for (int i = 0; i < 7; i++) {
        //print outside space.
        print_space(abs(3 - i));
        //print a star
        printf("*");
        //print inside space.
        print_space(3 - abs(3 - i));
        //overwrite output by '\b'
        printf("\b");
        print_space(3 - abs(3 - i));
        printf("*");
        print_space(abs(3 - i));
        printf("\n");
    }

    return 0;
}

void print_space(int times) {
    while (times-- != 0) {
        printf(" ");
    }
}

2.求最大公约数

#include <stdio.h>

int gre_con_div(int a, int b) {
    return (b == 0) ? a : gre_con_div(b, a%b);
}

int main() {
    /*int fir_num = 0, sec_num = 0;

    printf("please enter the first number:");
    scanf("%d", &fir_num);
    printf("please enter the second number:");
    scanf("%d", &sec_num);*/

    printf("the greatest common divisor is %d", gre_con_div(36, 24));

    return 0;
}

3.通过π/4求π(精确到小数点后七位)

Get the value of π \pi π by π 4 = 1 − 1 3 + 1 5 − 1 7 + 1 9 ⋯ \frac{\pi}{4}=1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}\cdots 4π=131+5171+91.

#include <stdio.h>

int main() {
    double ans = 0;

    //can use a variate instead of (double)(2 * i + 1) to optimize performence.
    for (int i = 0; (1 / (double)(2 * i + 1)) >= 0.00000001; i++) {
        ans = (i % 2 == 0) ? ans + 1 / (double)(2 * i + 1) : ans - 1 / (double)(2 * i + 1);
    }

    printf("the value of pi is %.7lf", 4 * ans);

    return 0;
}

4.表达式求值

Caculate the value of 2 1 + 3 2 + 5 3 + ⋯ \frac{2}{1}+\frac{3}{2}+\frac{5}{3}+\cdots 12+23+35+.

#include <stdio.h>

double caculate(int count, double ans, double num, double den) {
    ans += (num + den) / num;
    //printf("count:%d, ans:%lf\n", count, ans);
    return (count++ < 20) ? caculate(count, ans, num + den, num) : ans;
}

int main() {
    printf("the value of the expression is %.7lf", caculate(0, 0, 2, 1));

    return 0;
}

5.求100~200间的所有素数(每行打印5个)

#include <stdio.h>

int is_prime_num(int num) {
    for (int i = 2; i < num; i++)
        if (num % i == 0)
            return 0;
    return 1;
}

int main() {
    for (int i = 100, count = 0; i <= 200; i++)
        if (is_prime_num(i) == 1) {
            printf("%d,", i);
            if (++count % 5 == 0)
                printf("\b \n");
        }

    return 0;
}

6.计算字符串中的单词数

#include <stdio.h>

int count(char *str) {
    int i = 0, count = 1;

    if (str[0] == 0)
        return 0;
    else {
        while (str[i] != '\0')
            if (str[i++] == 32)
                count ++;

        return count;
    }
}

int main() {
    char str[] = "Sakura Uni Pilot Schneider Zebra";

    printf("total %d words in this character string", count(str));

    return 0;
}

7.将整数转换为2进制、10进制、16进制的数字串

#include <stdio.h>

int main() {
    int num = 255;

    printf("Decimal:%d\n", num);
    printf("Hexadecimal:%x\n", num);

    printf("Binary:");
    for (int i = 0; i < 16; i++) {
        printf("%d", (num & 0x8000) == 0x8000 ? 1 : 0);
        num = num << 1;
    }

    return 0;
}

8.打印字符串、复制字符串、连接字符串

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

//manfacture wheels
void str_out(const char *src);
void str_cpy(char *dest, const char *src);
void str_cat(char *dest, const char *src);

int main() {
    char *str = "Sakura Uni Pilot Schneider Zebra.";
    char *target = (char*)malloc(MAX_SIZE * sizeof(char));

    str_out(str);

    str_cpy(target, str);
    str_out(target);

    str_cat(target, str);
    str_out(target);

    return 0;
}

void str_out(const char *src) {
    while (*src != '\0')
        printf("%c", *src++);
    printf("\n");
}

void str_cpy(char *dest, const char *src) {
    while (*src != '\0')
        *dest++ = *src++;
    *dest = '\0';
}

void str_cat(char *dest, const char *src) {
    while (*dest != '\0')
        dest++;
    while (*src != '\0')
        *dest++ = *src++;
    *dest = '\0';
}

9.整数数组的冒泡排序(从小到大)

#include <stdio.h>
#include <stdlib.h>

int main() {
    int arr[10];

    printf("before sort:");
    for (int i = 0; i < 10; i++) {
        arr[i] = rand() % 100;
        printf("%d ", arr[i]);
    }
    printf("\n");

    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9-i; j++) {
            if (arr[j] > arr[j+1]) {
                arr[j] = arr[j] + arr[j+1];
                arr[j+1] = arr[j] - arr[j+1];
                arr[j] = arr[j] - arr[j+1];
            }
        }
    }

    printf("after sort:");
    for (int i = 0; i < 10; i++)
        printf("%d ", arr[i]);

    return 0;
}

10.对于任意正整数都可以找出至少一串连续奇数,它们的和等于该整数的立方,验证[2,20]之间的数均满足这一性质

#include <stdio.h>

int is_meet_exp(int x) {
    int i, j, sum;
    //start accumulation from 1, if it exceeds the given value, start again from 3, and so on
    for (i = 1; i <= x; i += 2) {
        sum = 0;
        //start accumulation 
        for (j = i; sum <= x; j += 2) {
            sum += j;
            if (sum == x) {
                //if the conditions are met, output the expression
                printf("%d=", x);
                while (sum != 0) {
                    printf("%d+", j);
                    sum -= j;
                    j -= 2;
                }
                printf("\b \n");
                return 1;
            }
        }
    }
    return 0;
}

int main() {
    for (int i = 2; i <= 20; i++)
        if(is_meet_exp(i * i * i) == 0)
            printf("%d doesn't meet this expression", i);

    printf("all data satisfy the expression");

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值