快乐的牛奶商 c语言6,C语言程序设计基础实训手册

本文提供了一系列C语言编程练习,涵盖基础运算、算法实现、数据结构、文件操作等多个方面。从勾股定理计算、温度预测到个人所得税计算,再到排序算法和素数筛选,每个题目旨在提升编程能力和问题解决技巧。通过这些实训,学生能够巩固课堂所学,提高程序设计能力。
摘要由CSDN通过智能技术生成

《C语言程序设计基础实训手册》由会员分享,可在线阅读,更多相关《C语言程序设计基础实训手册(20页珍藏版)》请在人人文库网上搜索。

1、程序设计基础实训手册说 明在完成了计算机导论2(C程序设计)课程的学习和上机实验后,需要进行一个程序设计实践训练,以巩固和提高同学们的程序设计能力。每位同学至少需独立完成本手册所给出的题目总量的50%。目录程序设计基础实训手册1说 明2题目1 勾股定理3题目2 冰箱温度3题目3 分段函数3题目4 N的平方根3题目5 计算个人所得税额4题目6 算式求解4题目7 自由落体的球4题目8 素性判定4题目9 可逆素数5题目10 厄拉多赛筛法求素数5题目11 对称数5题目12 计算cosx5题目13 分鱼5题目14 爱因斯坦数学题5题目15 阶乘6题目16 排序6题目17 素数排序6题目18 多项式6题目。

2、19 杨辉三角形7题目20 魔方阵7题目21 字符串处理-17题目22 字符串处理-27题目23 校验和8题目24 删除子串8题目25 求公共前缀8题目26 URL8题目27 Morse电码8题目28 复数运算9题目29 文件操作-19题目30 文件操作-29题目31 文件操作-39题目32 文件操作-410题目33 日期有关-110题目34 日期有关-210题目35 输出日历10题目36 再次输出日历10题目37 约瑟夫问题11题目38 汉诺塔11题目39 哥德巴赫猜想12题目40 怒刷GPA12题目41 走楼梯13题目42 n皇后问题13题目43 按钮阵列13题目44 快乐的牛奶商14题目。

3、45 计算密码14题目46 Franky的游戏15题目47 炸金花15题目48 Game of Life16附录A 常用标准库函数原型17题目1 勾股定理勾股定理指出:直角三角形两条直角边的平方和等于斜边的平方。例如,如果一个直角三角形的两条直角边长分别为3和4,那么斜边长必定为5。整数3、4、5一起构成了一个勾股三元组,这样的三元组有无数个。给定两个正整数m和n,mn,一个勾股三元组可以由下列公式产生:side1 = m2-n2side2 = 2mnhypotenuse = m2+n2当m=2,n=1时,根据公式就产生了三元组(side1=3,side2=4,hypotenuse=5)。编写。

4、一个程序,将m、n的值作为输入,并通过上面的公式将产生的勾股三元组的值显示出来。题目2 冰箱温度编写一个程序,输入断电后所经过的时间(小时),预测冰箱内的温度()。假设该温度(T)由下式给出:其中t为断电后经过的时间。程序应提示用户输入该时间,以整数小时和分钟表示。需要注意的是,经历时间应该转化为小时。例如,如果用户输入2 30(2小时30分钟),那么就要将其转化为2.5小时。题目3 分段函数请编写程序完成下面的计算要求。注:若x的值不在区间0,50),则提示输入错误。题目4 N的平方根数N的平方根可通过下式的迭代运算来近似计算:式中NG表示下一次的猜测值,LG表示上一次猜测值。编写一个函数利。

5、用上述方法计算平方根。第一次猜测值就是LG的初始值,程序利用公式计算NG。检测NG和LG的差值,看这两次猜测值是否几乎相等。如果是,NG就作为平方根;否则,用新的猜测值NG更新上次的猜测LG,重复上述过程(再计算新的NG、检测NG和LG的差,等等)。循环一直进行,直到NG与LG之差小于0.005。第一次猜测值为1.0。编写main函数,调用你所编写的求平方根函数,用下面的数据测试:4、120.5、88、36.01、10 000、0.25。题目5 计算个人所得税额 编写一个程序,输入为某人的月工资,输出为其每月应缴纳的个人所得税额。要求定义一个函数,接收一个表示月工资的数,返回对应的个人所得税额。

6、。针对工资薪金收入的个人所得税计算公式为:个人所得税额=应纳税所得额税率 - 速算扣除数其中,应纳税所得额 =月工资 - 三险一金 - 起征点三险一金为月工资的19%,起征点为3500元税率和速算扣除数分别与不同的应纳税所得额对应,如下表所示:级数全月应纳税所得额税率(%)速算扣除数(元)1不超过1,500元302超过1,500元至4,500元的部分101053超过4,500元至9,000元的部分205554超过9,000元至35,000元的部分2510055超过35,000元至55,000元的部分3027556超过55,000元至80,000元的部分3555057超过80,000元的部分45。

7、13505例如,某人月工资8000元,按规定19%缴纳三险一金,那么:其应纳税所得额=8000 - 800019% - 3500=2980元,对应税率和速算扣除数分别为10%和105元,因此,其个人所得税额为298010%-105=193元。题目6 算式求解 若有下述计算关系,试编写程序求十进制数字A、B、C、D的值。题目7 自由落体的球 设一球从100m高度自由落下,每次落地后反跳回原来高度的一半,再落下。求它在第10次落地时,共经过多少米?第10次反弹的高度是多少?该球经过多少次反弹后停止运动,此时该球的运动路程是多少(精确至小数点后第6位数字)?题目8 素性判定编写一个程序,判断输入的一。

8、个整数是否为素数。题目9 可逆素数 编写一个程序,求四位的可逆素数。可逆素数指:将一个素数的各位数字的顺序倒过来所构成的反序数也是素数。题目10 厄拉多赛筛法求素数大约在公元前250年,古希腊数学家厄拉多赛(Eratosthenes)提出一个造出不超过N的素数构造法,称为厄拉多赛筛法。它基于这样一个简单的性质:如果nN,且n是合数,则n必为一个不大于N的平方根的素数所整除。基本方法如下:先列出从2N的全体自然数,其中,2 是素数,在该数列中将2及其倍数去掉;接下来数列中最小的3是素数,再将数列中的3及其倍数去掉;接下来数列中最小的5是素数,重复该过程,直到数列为空。每次从数列中取出的最小数构成。

9、不超过N的全体素数。题目11 对称数将一个数的数码倒过来所得到的新数叫做原数的反序数,如果一个数等于它的反序数,则称它为对称数。例如,12321、3、151、44、6776等都是对称数。编写一个函数f,判断十进制正整数k是否是对称数。要求:在main函数中输入一个正整数k并调用该函数f,若k是对称数,再判断k的二进制形式是否也是对称数。若k及其二进制都是对称数,则输出k的十进制值及二进制值。题目12 计算cosx编制程序,利用下式计算ucosx的近似值,直到最后一项的绝对值小于。 题目13 分鱼 A、B、C、D、E五人在某天夜里合伙去捕鱼,到第二天凌晨时都疲惫不堪,于是各自找地方去睡觉。日上三。

10、竿,A第一个醒来,他将鱼分为五份,把多余的一条鱼扔掉,拿走自己的一份;B第二个醒来,也将鱼分为五份,把多余的一条鱼扔掉,拿走自己的一份;C、D、E依次醒来,也按同样的方法拿鱼。试编程计算他们至少捕了多少条鱼。题目14 爱因斯坦数学题 有一条长阶梯,若每步跨2阶,则最后剩余1阶,若每步跨3阶,则最后剩2阶,若每步跨5阶,则最后剩4阶,若每步跨6阶则最后剩5阶,若每步跨7阶,最后才正好一阶不剩。请问,这条阶梯共有多少阶? 题目15 阶乘请编写程序计算n!并输出,要求输入n的值且应满足n aj+1) swaptag = 1;temp = aj; aj = aj+1; aj+1 = temp;void。

11、 SelectSort(int a, int n) int i,j,t;int temp; for(i = 0; i0),找出这n个数中所有的素数,并按照从小到大的次序依次输出这些素数。题目18 多项式一元多项式的一般形式如下: A(x) = p0+p1x+p2x2+ +pixi+ + pnxn 请编写一个程序,将输入的两个一元多项式相加(提示:对于多项式的每一项,输入其系数和指数),最输出和多项式。可以用下面的数据进行测试。(1)(2)(3)(4)(5)(6)题目19 杨辉三角形编写一个程序,输入一个正整数n(不超过15),输出n行的杨辉三角形。例如,输入6时的输出如下:题目20 魔方阵Do。

12、le Rob算法生成奇数阶魔方阵的过程为:从1开始,依次插入各自然数,直到为止。选择插入位置原则为: 第一个位置在第一行的正中; 新位置应当处于最近一个插入位置的右上方,若右上方位置已超出方阵的上边界,则新位置取应选列的最下一个位置;若超出右边界则新位置取应选行的最左一个位置; 若最近一个插入元素为N的整数倍,则选下面一行同列的位置为新位置。题目21 字符串处理-1编写程序,对于读入的一个字符串,删除其中所有的空格。例如,将字符串“ xidian university ”中的空格删除后,得到“xidianuniversity”。题目22 字符串处理-2编写程序,将一个字符序列中的数字字符拼接成。

13、一个整数(串中数字个数不超过9个),输出该整数及其平方根,例如,若输入的字符序列为“aj?3v87.y=:61w!0#”,则输出及622.58。题目23 校验和通信信道常常含有噪声,因此设计了很多方法来保证数据的可靠传输,一种成功的方法是利用校验和。消息的校验和首先通过消息中各字符的整数编码的和计算,然后计算该和除以64的余数,并在此结果上加一个空格字符的整数编码。因为这个值在可输出字符范围内,它也被显示成字符。编写程序,接收以一个句点结尾的单行消息,并且显示每条消息的校验和字符。当用户输入只包含一个句点的消息时,要求程序结束。(字符的编码为其在ASCII字符集中的编码)题目24 删除子串编写。

14、程序,其中包括一个函数,此函数的功能是:对一个长度为N 的字符串从其第K个字符起,删去M个字符,组成长度为N-M的新字符串(其中N、MC”表示将一个圆盘从A柱移到C柱(该圆盘也是A柱最上边的盘),其他类推。题目39 哥德巴赫猜想 “自然科学的皇后是数学。数学的皇冠是数论。哥德巴赫猜想,则是皇冠上的明珠。”哥德巴赫猜想表述为:任一大于2的偶数都可写成两个质数(素数)之和。下面请编写程序验证对于比较小的偶数其猜想是否成立。输入为一个偶数M(2灭 或灭-亮)。现在给出了这个阵列的初始亮灭状态,编程找一种操作让灯全灭。题目44 快乐的牛奶商 牛奶包装是一个如此低利润的生意,所以尽可能低的控制初级产品(。

15、牛奶)的价格就十分重要。请帮助快乐的牛奶供应商以尽可能廉价的方式取得他们所需的牛奶。快乐的牛奶供应商从一些农民那里购买牛奶,每个农民所出售的牛奶价格不一定相同。而且由于每只母牛每天只能生产一定量的牛奶,所以农民每天只有一定量的牛奶可以卖。显然,快乐的牛奶供应商每天从农民那里所能购买的牛奶量,少于或等于农民所能提供的最大牛奶量。若给出快乐牛奶供应商每日的牛奶需求量,连同每个农民可提供的牛奶量和每加仑的价格,请编程计算快乐的牛奶供应商完成牛奶收购任务应支付的最小费用。(假设每天农民生产的牛奶总量对快乐的牛奶供应商来说是足够的)输入数据的第一行为两个整数,N 和 M:第一个整数N(01. FILE 。

16、*fopen( const char *fname, const char *mode );The fopen() function opens a file indicated by fname and returns a stream associated with that file. If there is an error, fopen() returns NULL. mode is used to determine how the file will be treated (i.e. for input, output, etc)ModeMeaningModeMeaningrOp。

17、en a text file for readingr+Open a text file for read/writewCreate a text file for writingw+Create a text file for read/writeaAppend to a text filea+Open a text file for read/writerbOpen a binary file for readingrb+Open a binary file for read/writewbCreate a binary file for writingwb+Create a binary。

18、 file for read/writeabAppend to a binary fileab+Open a binary file for read/write2. int fclose( FILE *stream );The function fclose() closes the given file stream, deallocating any buffers associated with that stream. fclose() returns 0 upon success, and EOF otherwise.3. int feof( FILE *stream );The 。

19、function feof() returns a nonzero value if the end of the given file stream has been reached.4. int fgetc( FILE *stream );The fgetc() function returns the next character from stream, or EOF if the end of file is reached or if there is an error.5. int fputc( int ch, FILE *stream );The function fputc(。

20、) writes the given character ch to the given output stream. The return value is the character, unless there is an error, in which case the return value is EOF.6. char *fgets( char *str, int num, FILE *stream );The function fgets() reads up to num - 1 characters from the given file stream and dumps t。

21、hem into str. fgets() will stop when it reaches the end of a line, in which case str will be terminated with a newline. If fgets() reaches num - 1 characters or encounters the EOF, str will be null-terminated. fgets() returns str on success, and NULL on an error.7. int fputs( const char *str, FILE *。

22、stream );The fputs() function writes an array of characters pointed to by str to the given output stream. The return value is non-negative on success, and EOF on failure.8. int fprintf( FILE *stream, const char *format, . );The fprintf() function sends information (the arguments) according to the sp。

23、ecified format to the file indicated by stream. fprintf() works just like printf() as far as the format goes. The return value of fprintf() is the number of characters outputted, or a negative number if an error occurs. 9. int fscanf( FILE *stream, const char *format, . );The function fscanf() reads。

24、 data from the given file stream in a manner exactly like scanf(). The return value of fscanf() is the number of variables that are actually assigned values, or EOF if no assignments could be made.10. int fread( void *buffer, size_t size, size_t num, FILE *stream );The function fread() reads num num。

25、ber of objects (where each object is size bytes) and places them into the array pointed to by buffer. The data comes from the given input stream. The return value of the function is the number of things read. You can use feof() or ferror() to figure out if an error occurs.11. int fwrite( const void 。

26、*buffer, size_t size, size_t count, FILE *stream );The fwrite() function writes, from the array buffer, count objects of size size to stream. The return value is the number of objects written.12. FILE *freopen( const char *fname, const char *mode, FILE *stream );The freopen() function is used to rea。

27、ssign an existing stream to a different file and mode. After a call to this function, the given file stream will refer to fname with access given by mode. The return value of freopen() is the new stream, or NULL if there is an error.13. void rewind( FILE *stream );The function rewind() moves the fil。

28、e position indicator to the beginning of the specified stream, also clearing the error and EOF flags associated with that stream.14. long ftell( FILE *stream );The ftell() function returns the current file position for stream, or -1 if an error occurs.15. int fseek( FILE *stream, long offset, int or。

29、igin );The function fseek() sets the file position data for the given stream. The origin value should have one of the following values (defined in stdio.h):NameExplanationSEEK_SETSeek from the start of the fileSEEK_CURSeek from the current locationSEEK_ENDSeek from the end of the filefseek() returns。

30、 zero upon success, non-zero on failure. You can use fseek() to move beyond a file, but not before the beginning. Using fseek() clears the EOF flag associated with that stream.16. int fflush( FILE *stream );If the given file stream is an output stream, then fflush() causes the output buffer to be wr。

31、itten to the file. If the given stream is of the input type, then fflush() causes the input buffer to be cleared. fflush() is useful when debugging, if a program segfaults before it has a chance to write output to the screen. Calling fflush( STDOUT ) directly after debugging output will ensure that 。

32、your output is displayed at the correct time.#include 17 . void srand( unsigned seed );The function srand() is used to seed the random sequence generated by rand(). For any given seed, rand() will generate a specific random sequence over and over again.18. int rand( void );The function rand() return。

33、s a pseudorandom integer between zero and RAND_MAX.#include 19. time_t time( time_t *time );The function time() returns the current time, or -1 if there is an error. If the argument time is given, then the current time is stored in time.20. clock_t clock( void );The clock() function returns the processor time since the program started, or -1 if that information is unavailable. To convert the return value to seconds, divide it by CLOCKS_PER_SEC. (Note: if your compiler is POSIX compliant, then CLOCKS_PER_SEC is always defined as .。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值