C 语言教程 在线

1635c-exercise-example2

#include "stdafx.h"


/*
Section 与 Percent 对应关系:
   利润<=Section[i] 时 , 提成为Percent[i]
*/ 
int Section[] = { 100000, 200000, 400000, 600000, 1000000 };
double Percent[] = { (double)10 / 100, 7.5 / 100, (double)5 / 100, (double)3 / 100, 1.5 / 100 };

//超过最大限定值时的提成比
double OtherPercent = (double)1 / 100;

int Len = sizeof(Section) / sizeof(int);


/*
说明:
  index 在[0,Len) 范围内时,说明 Profit<= Section[Len-1] 或 Profit<=Section[0]
  index = Len 时, 说明 Profit > Section[Len-1]
*/
int Split(const double& profit)
{
    int index = -1, i=0;
    if (profit <= 0.0)
        return index;
    for (; i < Len; i++)
    {
        if ((profit/Section[i]) >1.0)
            continue;

        index = i;
        break;
    }

    //profit>Section[Len-1]时,index赋值为Len
    if (i == Len)
        index = i;
    return index;
}



int _tmain(int argc, _TCHAR* argv[])
{
    double Profit=0.0,  GiveOut=0.0;
    //输入利润
    printf_s("利润:");
    scanf_s("%lf", &Profit);

    

    //计算应发放奖金
    int index = Split(Profit);
    double tempprofit = Profit;
    while (index>=0)
    {
        //超过最大限定值
        if (index == Len)
        {
            GiveOut += (tempprofit - Section[Len-1]) * OtherPercent;
            tempprofit = Section[Len-1];
        }
        else
        {
            double temp;
            //tempprofit 是否在 Section[0] 范围内
            temp = (index-1)>=0? tempprofit-Section[index-1] : tempprofit-0;
            GiveOut += temp * Percent[index];
            tempprofit = (index-1)>=0 ? Section[index-1]:0;
        }
        index = Split(tempprofit);
    }

    printf_s("应发放奖金:%g\n", GiveOut);
    return 0;
}

1634c-exercise-example2

利用 switch 的击穿现象,分享个计算方法。

#include <stdio.h>

int main(){
    double d;
    int money = 100000;
    float res=0.0;
    int flag;
    scanf("%lf",&d);
    flag = (int)(d/money);
    flag = flag >10?10:flag;
    switch(flag){
        case 10:
            res += (d-10*money)*0.01;
            d = 10*money;
        case 9:
        case 8:
        case 7:
        case 6:
            res += (d-6*money)*0.015;
            d = 6*money;
        case 5:
        case 4:
            res+= (d-4*money)*0.03;
            d = 4*money;
        case 3:
        case 2:
            res += (d-2*money)*0.05;
            d = 2*money;
        case 1:
            res += (d-money)*0.075;
            d = money;
        case 0:
            res += d *0.1;
    }
    
    printf("%.2f\n",res);
    return 0;
}

1633c-exercise-example2

尝试使用循环优化代码的适用性:

#include<stdio.h>
int main()
{
    int i;
    double lirun;
    double jiangjin = 0;
    float fanwei[] = {100000, 200000, 400000, 600000, 1000000};
    float ticheng[] = {0.1, 0.075, 0.05, 0.03, 0.015, 0.01};
    printf("您好,请问您的净利润是多少?\n");
    scanf("%lf", &lirun);
    for (i=0;i<5;i++)
    {
        if (lirun < fanwei[i])
        {
            jiangjin += lirun * ticheng[i];
            break;
        }
        else
        {
            jiangjin += fanwei[i] * ticheng[i];
            lirun -= fanwei[i];
        }
    }
    printf("奖金是%.2lf\n", jiangjin);

    return 0;
}

1632c-exercise-example2

优化如下:

#include <stdio.h>

#define WAN 10000

int main()
{
    double I = 0; // 利润
    double B = 0; // 奖金
    
    scanf("%lf", &I);
    I /= WAN;

    if (I > 100 * WAN)
    {
        B += ((I - 100) * 0.01);
        I = 100;
    }

    if (I > 60)
    {
        B += ((I - 60) * 0.015);
        I = 60;
    }
    if (I > 40)
    {
        B += ((I - 40) * 0.03);
        I = 40;
    }
    
    if (I > 20)
    {
        B += ((I - 20) * 0.05);
        I = 20;
    }

    if (I > 10)
    {
        B += ((I - 10) * 0.075);
        I = 10;
    }

    B += (I * 0.1);

    printf("%lf", B);
}

1631c-exercise-example1

深搜法(DFS)写此题:

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

int  b[4],arr[4];//定义两个数组用来类比
int Count=0;//计数器
void DFS(int step){
    if(step==5){
        if(arr[1]!=arr[2]&& arr[1]!=arr[3]&&arr[2]!=arr[3]){//判断哪些符合条件
            Count++;
            printf("%d%d%d\n",arr[1],arr[2],arr[3]);//输出可用排列
        }
        return ;
    }

    for(int i=1;i<=4;i++){
        if(b[i]==0){
            arr[step]=i;
            b[i]=1;//排除重复
            DFS(step+1);//自己调用自己
            b[i]=0;
        }
    }
    return ;
}

int main(void)
{
    DFS(1);
    printf("共有%d种",Count);
    return 0;
}