做的一些例题

c语言math头文件中,pow函数的使用

pow() 函数用来求 x 的 y 次幂(次方),x、y及函数值实际上为double型 ,其在使用中的原型为:求x的y次方

double pow(double x, double y); 

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

int main(){
    double p = 0;
    double a = 0.07;
    double b = 10;
    
    p = pow((1 + a), b);
    printf("%lf",p);
    system("pause");
    return 0;
}

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

int main(){
    int p = 1000;
    double a = 0.03;
    double ret1 = p * (1 + a * 5);
    printf("一次存五年 %lf\n",ret1);
    double b = 0.021;
    double c = 0.0275;
    double ret2 = (p * (1 + 2 * b)) * (1 + 3 * c);
    printf("一次存两年再存三年 %lf\n", ret2);
    double ret3 = (p * (1 + 3 * c)) * (1 + 2 * b);
    printf("一次存三年再存两年 %lf\n", ret3);
    double d = 0.015;
    double ret4 = p * pow((1 + d), 5);
    printf("五年存取五次 %lf\n", ret1);
    double e = 0.0035;
    double ret5 = p * pow((1 + e / 4),4 * 5);
    printf("活期存五年 %lf\n", ret5);
    system("pause");
    return 0;
}

使用对数函数log的时候有头文件math直接可以用log(x);输出时保留几位小数就在"%"之后加".数字".

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

int main(){
    int d = 300000;
    int p = 6000;
    double r = 0.01;
    double m = log(p / (p - d * r)) / log(1 + r) + 0.05;
    printf("%.1lf",m);
    system("pause");
    return 0;
}

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

int main(){
    char c1 = 97;
    char c2 = 98;
    printf("%c %c\n",c1,c2);
    printf("%d %d", c1, c2);
    system("pause");
    return 0;
}

输出 a b; 97 98.(char类型输出对应的ASCII值)

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

int main(){
    char c1 = 197;
    char c2 = 198;
    printf("%c %c\n",c1,c2);
    printf("%d %d", c1, c2);
    system("pause");
    return 0;
}

char类型保存范围 -128 - 127所以char输出? ?

int 输出 -59 -58;

C语言中π怎么用?

在math.h头文件下用反三角函数 acos(-1.0)

double pi = acos(-1.0);

知圆半径,柱高求面积 表面积 体积(有幂函数pow/输出小数%.2/π的值反三角函数acos(-1.0))

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(){
    double r = 1.5;
    double h = 3;
    double pi = acos(-1.0);
    printf("圆周长为 %.2lf\n",pi * 2 * r);
    double b = pow(r, 2);
    printf("圆面积为 %.2lf\n",pi * b);
    printf("球表面积为 %.2lf\n",4 * pi * b);
    double c = pow(r,3);
    printf("球体积为 %.2lf\n",(4 / 3) * pi * c);
    printf("圆柱体积为 %.2lf\n",pi * b * h);
    system("pause");
    return 0;
}

输入三个数子,输出最大值:

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(){

    int a, b, c;
    int tmp = 0;
    scanf("%d %d %d", &a, &b, &c);
    if (a > b){
         tmp = a;
    }
    else  tmp = b;
    if (tmp > c){
        printf("最大数为%d\n",tmp);
    }
    else
    tmp = c;
    printf("最大数为%d\n",tmp);
    
    

    system("pause");
    return 0;
}

写区间不能直接写(0 < x < 100),必须要写成(0 < x && x < 100)拆开来写.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(){
    
    double x = 0;
    printf("请输入:");
    scanf("%lf",&x);
    if (x >= 90){
        printf("A");
    }
    else if (80 <= x && x < 90){
        printf("B");
    }
    else if (70 <= x && x < 80){
        printf("C");
    }
    else if (60 <= x && x < 70){
        printf("D");
    }
    else

    {
        printf("E");
    }
    system("pause");
    return 0;
}

输入四个数字把他们从小大大输出出来(不想用条件语句判断,试用冒泡排序做一下)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void BubbleSort(int arr[], int size){
    for (int bound = 0; bound < size; bound++){
        for (int ret = size - 1; ret > bound; ret--){
            if (arr[ret] < arr[ret - 1]){
                int tmp = arr[ret];
                arr[ret] = arr[ret - 1];
                arr[ret - 1] = tmp;
            }
        }
    }
}
int main(){
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    printf("请输入");
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int arr[] = { a, b, c, d };
    BubbleSort(arr, 4);

    for (int i = 0; i < 4; i++){
        printf("%d", arr[i]);
    }
    printf("\n");
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值