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:

Enter the number of children: 8

The minimum number of apples: 36

#include 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:

Enter the formula: 73.263 % 0.9973

73.263000 % 0.9973000 = 0.460100

#include 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:

Enter two fractions separated by a plus sign: 5/6+3/4

The sum is 38/24

#include 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:

Enter your IP address: 192.168.0.1

c0a80001

#include 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:

7

12

6

9

#include int main()
{int a,b;scanf("%d",&a);if (a%2==0)b=a*a/4;elseb=(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:

Enter your multiplication expression: 89*13

   89

*  13

-----

  267

  89

-----

1157

#include 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;
}


总结

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


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部