c语言作业题整理,C语言编程练习题(最新整理)

《C语言编程练习题(最新整理)》由会员分享,可在线阅读,更多相关《C语言编程练习题(最新整理)(11页珍藏版)》请在人人文库网上搜索。

1、本练习假设:整数 int 为 4 字节,运行环境为 VC一、整数位数有关的问题1、输入一个整数,判断它是几位数?#include void main()int a,n=0; scanf(%d,&a);printf(%dn,n);答案在后面思路:如何数出位数?while(a0)a/=10; n+;int digits(int a)int n=0; while(a0)a/=10; n+;return n;扩展:编制一个函数,参数为整数,返回该整数的位数。答案在后面2、编制一函数,得到一个整数的某一位的数字。int digit(int a,int n) /*返回:整数 a 的倒数第 n 位数,n 从。

2、 1 开始*/答案在后面思路:如何将倒数第 n 位用一个表达式取出?不能! 转变成怎样后就能?while(n1)a/=10; n-;return a%10;测试函数的例子:#include int digit(int a,int n); void main()int a,n,d; scanf(%d%d,&a,&n); d=digit(a,n);printf(%dn,d);二、关于整数的几个题目1、输入两个正整数 m 和 n,求其最大公约数和最小公倍数。#include void main()int m,n,a,b,d,c;scanf(%d,%d,&m,&n); if(mn)a=m;b=n;/*。

3、保证 ab*/ else a=n;b=m;while(d=a%b)0)/*每次循环都测试余数是否为 0*/a=b;b=d;d=b;/*得到最大公约数*/ c=m*n/d;/*得到最小公倍数*/printf(最大公约数是%dn 最小公倍数是%dn,d,c);算法分析:采用辗转相除法,先求出最大公约数 d,然后求最小公倍数 c=m*n/d。辗转相除法:余数=大数%小数,然后将小数和余数再作辗转相除。拓展:编出求最大公约数的函数。int mcd(int a,int b)int d,t;if(ab*/ while(d=a%b)0)/*每次循环都测试余数是否为 0*/a=b;b=d;return b;/。

4、*得到最大公约数*/测试上述函数:#include int mcd(int a,int b); void main()int m,n,d,c; scanf(%d,%d,&m,&n); d=mcd(m,n);c=m*n/d;printf(最大公约数是%dn最小公倍数是%dn,d,c);2、求出所有的水仙花数。水仙花数是一个三位数,其各位数字的立方和等于该数。例如153=13+53+33。#include void main()int s,a,b,c; for(s=100;s void main()int s,a,b,c; for(a=0;a void main()double s=1.0; in。

5、t i;for(i=1;i void main()答案在后面int s100=1;/*初始化第一位为 0,其它每一位为 0*/int i,j,m=0;/*m 指示最高位为 sm*/ for(i=1;i9)/*第 j 位需要进位*/sj+1+=sj/10; sj%=10;if(j+1m) m=j+1;/*最高位进位*/printf(2100 = ); for(j=m;j=0;j-)printf(%d,sj);printf(n);运行结果:2100 = 1267650600228229401496703205376扩展问题:如何求出 100!的精确值?缺陷:如果乘以一个很大的数如 20 亿,会使高。

6、一位与进位相加后溢出。四、大数运算用整数数组存放每一位,最高位用-1 存储,如 12 存为:a0=2,a1=1,a2=-1。1、输入和存储大数。/*转换大数:将字符串 c 中的数字保存到整数数组 a 中,a 最高位添加-1 标志*/ void LargeSet(char *c, int *a)char *p;for(p=c;*p!=0;p+);/*找到末尾作为最低位*/ p-;for(;p=c;p-,a+)*a=*p-0;*a=-1;编制一函数,将合法数字组成的字符串表示的大数按位拆分存储到整数数组中,最高位前存储-1。2、输出和获得数字串。编制一函数,将大数转换成字符串。/*转换大数:将大数。

7、 a 转换成字符串存入字符数组 c 中*/ void LargeGet(char *c, int *a)int *p; for(p=a;*p=0;p+); p-;for(;p=a;c+,p-)*c=*p+0;*c=0;3、大数相加。编制一函数,实现两个大数相加。void LargeAdd(int *a, int *b, int *s) /*大数相加:s=a+b*/int ka=0,kb=0; for(*s=0;ka=0|kb=0;s+)if(*a9) *(s+1)=1;*s%=10; else *(s+1)=0;if(*s0) *(s+1)=-1;else if(*(s-1)0) *s=-1;。

8、else *(s-1)=-1;验证上面的函数:#include void LargeSet(char *a, int *s); void LargeGet(char *a, int *s);void LargeAdd(int *a, int *b, int *s); void main()int a100,b100,s100;char c100; scanf(%s,c); LargeSet(c,a); scanf(%s,c); LargeSet(c,b); LargeAdd(a,b,s); LargeGet(c,s); printf(sum is %sn,c);运行结果:12345678901。

9、23456789012345678901234567890sum is 24691357802469135780扩展:可以编制大数相减、相乘、相除、乘方、开方等运算。有了这些运算,计算 100!或 2的 100 次方的精确值将变得很简单。“”“”At the end, Xiao Bian gives you a passage. Minand once said, people who learn to learn are very happy people. In every wonderful life, learning is an eternal theme. As a profess。

10、ional clerical and teaching position, I understand the importance of continuous learning, life is diligent, nothing can be gained, only continuous learning can achieve better self. Only by constantly learning and mastering the latest relevant knowledge, can employees from all walks of life keep up with the pace of enterprise development and innovate to meet the needs of the market. This document is also edited by my studio professionals, there may be errors in the document, if there are errors, please correct, thank you。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值