求解两道题,入门级的😂(我还是不会)

img


img

//1
#include<stdio.h>
int main(){
    double a,b,c,s=1,day,month,year;
    scanf("%lf %lf %lf",&a,&b,&c);
    for(int i=1;i<=365;i++){
        s=s*(1+a);
    }
    day=(s-1)*100;
    s=1;
    for(int i=1;i<=12;i++){
        s=s*(1+b);
    }
    month=(s-1)*100;
    year=c*100;
    printf("%.2lf%% %.2lf%% %.2lf%% ",day,month,year);
    if(day<month){
        if(day<year)  printf("%.2lf%%",day);
        else  printf("%.2lf%%",year);
    }else{
        if(month>year) printf("%.2lf%%",year);
        else printf("%.2lf%%",month);
    }
    return 0;
}
//2
#include<stdio.h>
int main(){
    double a,b,p,s,z=1;
    int c;
    scanf("%lf %lf %d",&a,&b,&c);
    s=a;
    for(int i=1;i<=c;i++){
        s=s*(1+b);
    }
    p=(s-a)/a*100;
    for(int i=1;i<=365;i++){
        z=z*(1+b);
    }
    printf("%.2lf %.2lf%% %.2lf%%",s,p,z*100);
    return 0;
}


 觉得有用的话采纳一下哈

两个程序有共同的地方,我写代码,分成两个main函数,上下注释切换就行,我在求次方的函数上做了优化,时间效率为o(logn)比普通的直接相乘快,是快速幂乘法的思想,望采纳,谢谢


#include<stdio.h>
#include<stdlib.h>
double Min(double a,double b){return a<b?a:b;}

// 快速幂
double fastPower(double base,int power){
    if(base==0.0) return 0.0;
    int flag  = power;
    if(power<0) power = -power;
    double ans = 1.0;
    while(power>0){
        if(power%2==1)
            ans = ans * base;
        power /= 2;
        base = base*base;        
    }
    return flag>0?ans:1/ans;
}

// 测试:0.001 0.030 0.365
// 44.03% 42.58% 36.50% 36.50%
int main(){
    double a,b,c;
    while(scanf("%lf %lf %lf",&a,&b,&c)!=EOF){
        a=(fastPower(1+a,365)-1)*100;
        b=(fastPower(1+b,12)-1)*100;
        c=(fastPower(1+c,1)-1)*100;
        printf("%.2f%% %.2f%% %.2f%% %.2f%%\n",a,b,c,Min(Min(a,b),c));
    }
}

// 测试:1000 0.003 365
//     2984.29 198.43% 298.43%
// int main(){
//   double money,rate,total,proportion,yearRate;
//   int days;
//    while(scanf("%lf %lf %d",&money,&rate, &days)!=EOF){
//         total=(fastPower(1+rate,365))*money;
//         proportion=(total-money)/money*100;
//         yearRate=total/money*100;
//         printf("%.2f %.2f%% %.2f%%\n",total, proportion, yearRate);
//    }
// }