C语言零基础入门习题(四)分苹果

前言

        C语言是大多数小白走上程序员道路的第一步,在了解基础语法后,你就可以来尝试解决以下的题目。放心,本系列的文章都对新手非常友好。


Tips:题目是英文的,但我相信你肯定能看懂

一、Mr. Wang wants to give some apples in a box to some children, if everyone can get apple, and the number of apples for every children are different, how many apples should there be at least in this box?

Here is a sample run:

<output>

Enter the number of children: 8 <enter icon>

The minimum number of apples: 36

<end output>

#include <stdio.h>

int main()
{
    int a,b;
    printf("Enter the number of children: ");
    scanf("%d",&a);
    b = (1+a) * a / 2;
    printf("The minimum number of apples: %d",b);
    return 0;
}

二、Write a program that prompts the user to enter two float point data and calculate the remainder.

Here is a sample run:

<output>

Enter the formula: 73.263 % 0.9973 <enter icon>

73.263000 % 0.9973000 = 0.460100

<end output>

#include <stdio.h>

int main()
{
    double a,b,c;
    printf("Enter the formula: ");
    scanf("%lf %% %lf",&a,&b);
    c=a-(int)(a/b)*b;
    printf("%lf %% %lf = %lf",a,b,c);
    return 0;
}

三、Write a program to add two fractions, the user enters both fractions at the same time, separated by a plus sign:

Here is a sample run:

<output>

Enter two fractions separated by a plus sign: 5/6+3/4<enter icon>

The sum is 38/24

<end output>

#include <stdio.h>

int main()
{
    int a,b,c,d;
    printf("Enter two fractions separated by a plus sign: ");
    scanf("%d/%d+%d/%d",&a,&b,&c,&d);
    printf("The sum is %d/%d",a*d+b*c,d*b);
    return 0;
}

四、IPv4 can be stored in memory, such as 192.168.1.1 needs 11 bits, but if you convert it into hexadecimal format, it will be c0a80101, need eight bits only. Please input a legal IP address (four decimal numbers less than 255 separated by period sign), and print the corresponding hexadecimal format.

Here is a sample run:

<output>

Enter your IP address: 192.168.0.1<enter icon>

c0a80001

<end output>

#include <stdio.h>

int main()
{
    int a,b,c,d;
    printf("Enter your IP address: ");
    scanf("%d.%d.%d.%d",&a,&b,&c,&d);
    printf("%02x%02x%02x%02x",a,b,c,d);
    return 0;
}

五、For a positive integer n, divide n into the sum of two nonnegative integers and make the product of these two nonnegative integers as large as possible. What is the maximum product of these two nonnegative integers?

Here is a sample run:

<output1>

7 <enter icon>

12

<end output1>

<output2>

6 <enter icon>

9

<end output2>

#include <stdio.h>

int main()
{
    int a,b;
    scanf("%d",&a);
    if (a%2==0)
        b=a*a/4;
    else
        b=(a+1)*(a-1)/4;
    printf("%d",b);
    return 0;
}

六、Design a calculator visualizes process of multiplying in columns, you need to input an expression like 87*65, the number should be positive integer less than 100, and show the column multiplication according to the sample run.

Here is a sample run:

<output>

Enter your multiplication expression: 89*13<enter icon>

   89

*  13

-----

  267

  89

-----

1157

<end output>

#include <stdio.h>

int main()
{
    int a,b,c,d;
    printf("Enter your multiplication expression: ");
    scanf("%d*%d",&a,&b);
    printf("%5d\n",a);
    printf("*%4d\n",b);
    printf("-----\n");
    printf("%5d\n",a*(b%10));
    printf("%4d\n",a*(b/10));
    printf("-----\n");
    printf("%5d\n",a*b);
    return 0;
}


总结

以上就是本文全部内容,你学会了吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值