C语言程序设计精髓第4周编程题

C语言程序设计精髓第4周编程题

1 分数比较

#include <stdio.h>
int main()
{
    int a, b, c, d, res;
    
    printf("Input a/b, c/d:"  );
    scanf("%d/%d,%d/%d", &a, &b, &c, &d);
    
    res = a * d - b * c;
    if (res > 0)
        printf("%d/%d>%d/%d\n", a, b, c, d);
    else if (res < 0)
        printf("%d/%d<%d/%d\n", a, b, c, d);
    else
        printf("%d/%d=%d/%d\n", a, b, c, d);
    return 0;
}

2 存款利率计算器v2.0

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

int main()
{
    double rate, capital, deposit;
    int year;
char ch;

    printf("Input rate, year, capital:");
    scanf("%lf,%d,%lf", &rate, &year, &capital);

    printf("Compound interest (Y/N)?");
    scanf(" %c", &ch);

    if(ch == 'Y' || ch == 'y')
        deposit = capital * pow(1 + rate, year);
    if(ch == 'N' || ch == 'n')
        deposit = capital * (1 + rate * year);

    printf("deposit = %.4f\n", deposit);

    return 0;
}

3 存款利率计算器v3.0

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

int main()
{
    double rate, capital, deposit;
    int year;
    char ch;

    printf("Input capital, year:");
    scanf("%lf,%d", &capital, &year);

    printf("Compound interest (Y/N)?");
    scanf(" %c", &ch);

    switch (year)
    {
        case 1 :    rate = 0.0225;  break;
        case 2 :    rate = 0.0243;  break;
        case 3 :    rate = 0.0270;  break;
        case 5 :    rate = 0.0288;  break;
        case 8 :    rate = 0.0300;  break;
        default :   printf("Error year!\n");  return 0;
    }

    if(ch == 'Y' || ch == 'y')
        deposit = capital * pow(1 + rate, year);
    if(ch == 'N' || ch == 'n')
        deposit = capital * (1 + rate * year);

    printf("rate = %.4f, deposit = %.4f\n", rate, deposit);

    return 0;
}

4 博弈论之Best Response

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

int main()
{
    float A, B, compete, standard;

    printf("Input percent of A and B:");
    scanf("%f%f", &A, &B);

    compete = A * 10 + B * 6;
    standard = A * 8 + B * 10;

    printf("compete = %.4f\nstandard = %.4f\n", compete, standard);
    if(compete > standard)
        printf("The Best Response is compete!");
    else
        printf("The Best Response is standard!");

    return 0;
}

1 检测用户错误输入

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

int main()
{
    int a, b, t;

    if(scanf("%d %d", &a, &b) == 2)
        printf("a = %d, b = %d\n", a, b);
    else
        printf("Input error!");

    return 0;
}

2 闰年判断

#include <stdio.h>

int main()
{
    int year;

    scanf("%d", &year);

    if (year >= 1)
    {
        if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
            printf("Yes\n");
        else
            printf("No\n");
    }
    else
        printf("Input error!\n");

    return 0;
}

3 程序改错v1.0

#include<stdio.h>
int main()
{
    int score;
    char grade;

    printf("Please input score:\n");
    if ((scanf("%d", &score) != 1) || (score < 0 || score > 100))
        printf("Input error!\n");
    else
    {
        if (score >= 90)
            grade = 'A';
        else if (score >= 80)
            grade = 'B';
        else if (score >= 70)
            grade = 'C';
        else if (score >= 60)
            grade = 'D';
        else
            grade = 'E';
        printf("grade: %c\n", grade);
    }
    return 0;
}

4 字符类型判断

#include<stdio.h>
int main()
{
    char ch;

    printf("Input simple:\n");
    scanf("%c", &ch);

    if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
        printf("It is an English character.\n");
    else if (ch >= '0' && ch <= '9')
        printf("It is a digit character.\n");
    else
        printf("It is other character.\n");

    return 0;
}

5 快递费用计算

#include<stdio.h>

#define EPS 1E-6

int main()
{
    int zone, w;
    float weight, price = 0;

    scanf("%d,%f", &zone, &weight);


    w = weight - (int)(weight) <= EPS ? (int)(weight) : (int)(weight) + 1;
    // w = (int)(weight + 0.99999999);

    switch (zone)
    {
        case 0:  price = 10 + 3 * (w - 1);     break;
        case 1:  price = 10 + 4 * (w - 1);     break;
        case 2:  price = 15 + 5 * (w - 1);     break;
        case 3:  price = 15 + 6.5 * (w - 1);   break;
        case 4:  price = 15 + 10 * (w - 1);    break;
        default: printf("Error in Area\n");
    }
    printf("Price: %5.2f\n", price);

    return 0;
}

6 数位拆分v2.0

#include <stdio.h>

int main()
{
    int n, a, b;
    printf("Please input n:\n");
    scanf("%d", &n);

    a = n / 100;
    b = n % 100;
	printf("%d,%d\n", a, b);
	printf("sum=%d,sub=%d,multi=%d\n", a + b, a - b, a * b);

	if (b != 0)
        printf("dev=%.2f,mod=%d\n", (float)a / b, a % b);
    else
        printf("The second operator is zero!\n");

    return 0;
}

7 出租车计价

#include <stdio.h>

int main()
{
    float distance, fee = 0;
    int time;

    printf("Input distance and time:");
    scanf("%f,%d", &distance, &time);

    fee += 8;
    if (distance > 3)
        if (distance <= 10) fee += (distance - 3) * 2;
        else fee += 7 * 2 + (distance - 10) * 3;

    fee += time / 5 * 2;

    printf("fee = %.0f\n", fee);

    return 0;
}

8 数据区间判断

#include <stdio.h>

int main()
{
    int n;

    printf("Please enter the number:\n");
    scanf("%d", &n);

    if (n <= 0 || n >= 10000)
        printf("error!\n");
    else if (n >= 1000)
        printf("%d: 1000-9999\n", n);
    else if (n >= 100)
        printf("%d: 100-999\n", n);
    else if (n >= 10)
        printf("%d: 10-99\n", n);
    else
        printf("%d: 0-9\n", n);

    return 0;
}

9 计算一元二次方程的根v2.0

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

#define EPS 1e-6

main()
{
	float  a, b, c, disc, p, q;
	printf("Please enter the coefficients a,b,c:\n");
	scanf("%f,%f,%f", &a, &b, &c);

	if (fabs(a) <= EPS)
	{
		printf("error!\n");
		exit(0);
	}

	disc = b * b - 4 * a * c;
	if (disc < -EPS)
	{
		printf("error!\n");
	}
	else
	{
		p = - b / (2 * a);
		q = sqrt(fabs(disc)) / (2 * a);
		printf("x1=%7.4f, x2=%7.4f\n", p + q, p - q);
	}

	return 0;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在博弈论中,有一种决策称为Best Response,通俗的意思就是选择一种策略使得团体利益最大化。C语言学习成绩的评定方式分为两种,一种是自由刷题模式(compete),没有固定标准,刷题越多者排名越靠前,其期末分数越高;另一种是规定每个人必须做够多少道题(standard),达到要求就能取得相应分数。 假设一个班级中的学生分为A、B两类,A类同学学习热情很高,乐于做题,采用compete模式可以获得成就感并且在期末拿到高分,compete模式可以让他们有10分的收益;采用standard模式他们也可以在期末拿到高分,但不能满足他们的求知欲,standard模式可以让他们有8分的收益。B类同学仅仅希望期末拿高分,如果采用compete模式,他们竞争不过A类同学,期末成绩不理想,因此compete模式能给他们6分的收益;如果采用standard模式,他们可以完成规定任务并拿到高分,因此standard模式可以让他们有10分的收益。 编程输入A类和B类同学分别占班级总人数的百分比,分别计算并输出采用compete和standard两种刷题模式下的全班总收益,并输出这个班级在这场博弈中的Best Response是哪种模式。 【注】程序中使用的数据类型为float 程序运行结果示例1: Input percent of A and B:0.2 0.8↙ compete = 6.8000 standard = 9.6000 The Best Response is standard! 程序运行结果示例2: Input percent of A and B:0.8 0.2↙ compete = 9.2000 standard = 8.4000 The Best Response is compete! 程序运行结果示例3: Input percent of A and B:0.5 0.5↙ compete = 8.0000 standard = 9.0000 The Best Response is standard! 输入提示信息:"Input percent of A and B:" 输入格式:"%f%f" 输出格式:"compete = %.4f\nstandard = %.4f\n" 输出提示信息:"The Best Response is compete!" 输出提示信息:"The Best Response is standard!"

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值