关闭

关闭

关闭

封号提示

内容

首页 118例c语言程序

118例c语言程序.pdf

118例c语言程序

qinhe_89186 2013-12-28 评分 0 浏览量 0 0 0 0 暂无简介 简介 举报

简介:本文档为《118例c语言程序pdf》,可适用于其他资料领域,主题内容包含输入两个正整数m和n求其最大公约数和最小公倍数。#include<stdioh>voidmain(){inthcf(int,int)*函数声明*in符等。

输入两个正整数m和n求其最大公约数和最小公倍数。#include<stdioh>voidmain(){inthcf(int,int)*函数声明*intlcd(int,int,int)*函数声明*intu,v,h,lprintf("Pleaseinputtwonumbers:n")scanf("d,d",u,v)h=hcf(u,v)printf("HCF=dn",h)l=lcd(u,v,h)printf("LCD=dn",l)}inthcf(intu,intv){intt,rif(v>u){t=uu=vv=t}while((r=uv)!=){u=vv=r}return(v)}intlcd(intu,intv,inth){return(u*vh)}输入一行字符分别统计出其中字母、空格、数字和其他字符的个数。#include<stdioh>intletter,digit,space,othersvoidmain(){voidcount(char)chartextprintf("Pleaseinputstring:n")gets(text)printf("string:n")puts(text)letter=digit=space=others=count(text)printf("letter:d,digit:d,space:d,others:dn",letter,digit,space,others)}voidcount(charstr){intifor(i=stri!=''i)if((stri>='a'stri<='z')||(stri>='A'stri<='Z'))letterelseif(stri>=''stri<='')digitelseif(stri==)spaceelseothers}输入一个正整数求出它是几位数输出原数和位数。#include<stdioh>intdigitvoidmain(){voidcount(char)chartextprintf("Pleaseinputnumbers:n")gets(text)printf("Numbers:n")puts(text)digit=count(text)printf("digit:dn",digit)}voidcount(charstr){intifor(i=stri!=''i)if(stri>=''stri<='')digit}输入一个正整数输出原数并逆序打印出各位数字。#include<stdioh>voidinvertLongInt(long)voidmain(){unsignedlongiNumberprintf("Pleaseinputanumber:n")scanf("ld",iNumber)printf("Theinputnumberis:ldn",iNumber)printf("Theinversenumberis:")invertLongInt(iNumber)}voidinvertLongInt(longx){if(x>=x<=)printf("dn",x)else{printf("d",x)invertLongInt(x)}}从键盘上输入若干学生的一门课成绩统计并输出最高成绩和最低成绩及相应的序号当输入负数时结束输入。从键盘上输入若干学生的一门课成绩计算出平均分当输入负数时结束输入。将结果输出。求!!!……!将结果输出。#include<stdioh>voidmain(){floats=,t=intnfor(n=n<=n){t=t*ns=st}printf("!!!……!=en",s)}打印以下图案:****************#include<stdioh>voidmain(){inti,jprintf("Thepictureis:n")staticcharpicture={{'','','','*'},{'','','*','*','*'},{'','*','*','*','*','*'},{'*','*','*','*','*','*','*'}}for(i=i<=i){for(j=j<=j)printf("c",pictureij)printf("n")}}打印以下图案:**********#include<stdioh>voidmain(){inti,jprintf("Thepictureis:n")charpicture={{'*'},{'*','*'},{'*','*','*'},{'*','*','*','*'}}for(i=i<=i){for(j=j<=j)printf("c",pictureij)printf("n")}}求下列试子的值:……将结果输出。#include<stdioh>voidmain(){floatsum=,t,s=intifor(i=i<=i){t=sisum=sumts=s}printf("……=fn",sum)}打印出~之间的所有水仙花数。#include<stdioh>voidmain(){inti,j,k,nprintf("~之间的所有水仙花数are:n")for(n=n<n){i=nj=ni*k=nif(n==i*i*ij*j*jk*k*k)printf("d",n)}printf("n")}求Sn=aaaaaa…aa…a之值n,a由键盘输入。#include<stdioh>voidmain(){inta,n,i=,sn=,tn=printf("a,n=:")scanf("d,d",a,n)while(i<=n){tn=tnasn=sntna=a*i}printf("aaaaaa…aa…a=dn",sn)}打印以下图案:****************************#include<stdioh>voidmain(){chara={'*','*','*','*','*','*','*'}inti,j,kcharspace=''printf("Thepictureis:")for(i=i<i){printf("n")printf("")for(j=j<=ij)printf("c",space)for(k=k<k)printf("c",ak)}printf("n")}打印以下图案:打印以下图案:编写一个统计学生成绩程序完成以下功能:输入个学生的门课成绩求出全班的总平均分将结果输出。#include<stdioh>#defineNstructstudent{charnumcharnamefloatscorefloatavr}stuNvoidmain(){inti,jfloatsum,averagefor(i=i<Ni)*输入数据*{printf("inputscoresofstudentd:n",i)printf("name:")scanf("s",stuiname)for(j=j<j){printf("scored:",j)scanf("f",stuiscorej)}}average=*计算*for(i=i<Ni){sum=for(j=j<j)sum=stuiscorejstuiavr=sumaverage=stuiavr}average=Nprintf("Namescorescoreaveragen")for(i=i<Ni){printf("ss",stuinum,stuiname)for(j=j<j)printf("f",stuiscorej)printf("fn",stuiavr)}printf("average=fn",average)}打印以下图案:*************************#include<stdioh>voidmain(){chara={'*','*','*','*','*'}inti,j,kcharspace=''printf("Thepictureis:")for(i=i<i){printf("n")printf("")for(j=j<=ij)printf("c",space)for(k=k<k)printf("c",ak)}printf("n")}给出年、月、日计算该日是该年的第几天。#include<stdioh>voidmain(){intsumday(int,int)intleap(intyear)intyear,month,day,daysprintf("inputdate(year,month,day):")scanf("d,d,d",year,month,day)printf("ddd",year,month,day)days=sumday(month,day)if(leap(year)month>=)days=daysprintf("isthedthdayinthisyearn",days)}intsumday(intmonth,intday){intdaytab={,,,,,,,,,,,,}intifor(i=i<monthi)day=daytabireturn(day)}intleap(intyear){intleapleap=year==year!=||year==return(leap)}求一个*的整型矩阵对角线元素之和。将原矩阵和求出的和输出。#include<stdioh>voidmain(){floata,sum=inti,jprintf("请输入元素:n")for(i=i<i)for(j=j<j)scanf("f",aij)for(i=i<i)sum=sumaiiprintf("对角线之和是:fn",sum)for(i=i<=i){for(j=j<=j)printf("f",aij)printf("n")}}求一个*的矩阵各行元素的平均值将原矩阵和求出的平均值全部输出。#include<stdioh>voidmain(){inta,s,i,j,kfor(i=i<i)for(j=j<j)scanf("d",*(ai)j)for(i=i<i){k=for(j=j<j)k=aijprintf("第d行的平均值是d",i,k)printf("n")}}求一个*的矩阵各列元素的平均值将原矩阵和求出的平均值全部输出。#include<stdioh>voidmain(){inta,s,i,j,kfor(i=i<i)for(j=j<j)scanf("d",*(ai)j)for(i=i<i){k=for(j=j<j)k=aijprintf("第d行的平均值是d",i,k)printf("n")}}求一个*的矩阵各列元素的最大值将原矩阵和求出的最大值全部输出。#include<stdioh>voidmain(){inta,s,i,j,kfor(i=i<i)for(j=j<j)scanf("d",*(ai)j)for(i=i<i){*(si)=*(*(aj))for(j=j<j)if(*(si)<*(*(ai)j))*(si)=*(*(ai)j)}for(i=i<i){printf("Line=dMax=d",j,sj)printf("n")}}求一个*的矩阵各行元素的最大值将原矩阵和求出的最大值全部输出。#include<stdioh>voidmain(){inta,s,i,j,kfor(i=i<i)for(j=j<j)scanf("d",*(ai)j)for(i=i<i){*(si)=*(*(ai))for(j=j<j)if(*(si)<*(*(ai)j))*(si)=*(*(ai)j)}for(i=i<i){printf("Row=dMax=d",i,si)printf("n")}}求一个M*N的矩阵中元素的最大值将原矩阵和求出的最大值全部输出。#include<stdioh>#defineN#defineMvoidmain(){intaN,i,j,k,max,m,nscanf("dd",m,n)for(i=i<mi)for(j=j<nj)scanf("d",*(ai)j)for(i=i<mi){for(j=j<nj){if(aij<=aij)max=aijelsemax=aij}ki=max}for(i=i<mi){if(ki<=ki)max=kielsemax=ki}printf("d",max)}判断一个N*N的矩阵是否为对称矩阵将原矩阵输出判断结果输出。#include<stdioh>#defineNvoidmain(){intaNN,i,j,k,nscanf("d",n)for(i=i<ni)for(j=j<nj)scanf("d",aij)for(i=i<ni){for(j=ij<nj){if(aij==aji)k=elsek=}}if(k=)printf("bushi")elseprintf("shin")for(i=i<ni)for(j=j<nj)printf("d",aij)}有一篇文章有三行文字每行有个字符。要求统计出其中英文大写字母、消协字母、数字、空格以及其他字符的个数。#include<stdioh>voidmain(){inti,j,big=,sma=,num=,spa=,oth=chartextfor(i=i<i){printf("请输入行d:n",i)gets(texti)for(j=j<textij!=''j){if(textij>='A'textij<='Z')bigelseif(textij>='a'textij<='z')smaelseif(textij>=''textij<='')numelseif(textij=='')spaelseoth}}printf("大写字母:dn",big)printf("小写字母:dn",sma)printf("数字:dn",num)printf("空格:dn",spa)printf("其它:dn",oth)}将个整数放到一维数组中输出该数组的最大值和最小值。#include<stdioh>voidmain(){inti,j,min,max,aprintf("请输入数据:n")for(i=i<=i){printf("ad=",i)scanf("d",ai)}for(i=i<=i){min=ifor(j=j<=j)if(amin>aj)amin=aj}for(i=i<=i){max=ifor(j=j<=j)if(amax<aj)amax=aj}printf("最大数为:dn",amax)printf("最小数为:dn",amin)}将个整数放到一维数组中输出该数组中的最大值它的下标然后将它和数组中的最前面的元素对换。#include<stdioh>voidmain(){inti,j,min,max,a,m,nprintf("请输入数据:n")for(i=i<=i){printf("ad=",i)scanf("d",ai)}for(i=i<=i){max=ifor(j=j<=j)if(amax<aj)amax=ajm=I,n=j}printf("最大数下标为:ddn",m,n)}将字符数组str种下标为偶数的元素赋给另一字符数组str并输出str和str。#include<stdioh>#include<stringh>#defineNvoidmain(){inti,ncharstrN,strNgets(str)for(n=n<Nn){i=*nstrn=stri}puts(str)puts(str)}编写一个程序将两个字符串连接起来不要使用strcat函数。#include<stdioh>voidmain(){charstr,strinti=,j=printf("请输入字符串:n")scanf("s",str)printf("请输入字符串:n")scanf("s",str)while(stri!='')iwhile(strj!='')stri=strjstri=''printf("合并之后的字符串为:sn",str)}编写一个程序将字符数组a中的全部字符复制到字符数组b中。不要使用strcpy函数。#include<stdioh>#include<stringh>voidmain(){chara,bintiprintf("请输入a:n")scanf("s",a)for(i=i<=strlen(a)i)bi=aiprintf("b:sn",b)}编写一个程序找出个字符串中的最大者将它输出。#include<stdioh>#include<stringh>voidmain(){charstringcharstrintifor(i=i<i)gets(stri)if(strcmp(str,str)>)strcpy(string,str)elsestrcpy(string,str)if(strcmp(str,string)>)strcpy(string,str)printf("最大的字符串是:nsn",string)}编写程序输入任意一个~之间的整数将他们转换成对应的英文单词。例如:转换成Monday,转换成Sunday。#include<stdioh>voidmain(){intaprintf("输入一个整数:n")scanf("d",a)if(a==)printf("Mondayn")elseif(a==)printf("Tuesdayn")elseif(a==)printf("Wendesdayn")elseif(a==)printf("Thursdayn")elseif(a==)printf("Fridayn")elseif(a==)printf("Saturdayn")elseif(a==)printf("Sundayn")elseprintf("错误n")}编写程序输入两个整数和、、*、之中的任意一个运算符输出计算结果。#include<stdioh>voidmain(){floata,bcharcprintf("请输入一个运算符:n")scanf("c",c)printf("请输入两个整数:n")scanf("d,d",a,b)switch(c){case'':printf("fn",ab)breakcase'':printf("fn",ab)breakcase'*':printf("fn",a*b)breakcase'':printf("fn",ab)breakdefault:printf("错误")}}输入年号计算这一年的月份的天数将结果输出。#include<stdioh>voidmain(){intyear,leapprintf("请输入年份:n")scanf("d",year)if(year==){if(year==){if(year==)leap=elseleap=}elseleap=}elseleap=if(leap)printf("d年的月有天",year)elseprintf("d年的月有天",year)}输入三角形的三边a,b,c判断a,b,c能否构成三角形若能计算面积。#include<stdioh>#include<mathh>voidmain(){floata,b,c,areadoublesprintf("Pleaseenterthreesidesofatriangle:n")scanf("f,f,f",a,b,c)s=(abc)area=sqrt(s*(sa)*(sb)*(sc))if(ab<c||bc<a||ac<b)printf("Theycan'tformatrianglen")elseprintf("Theareaofthetriangleis:fn",area)}#include<stdioh>#defineN#defineMvoidmain(){inti,j,k,m,n,flag,flag,aN,max,maxjprintf("输入行数nn<:")scanf("d",n)printf("输入列数mm<:")scanf("d",m)for(i=i<ni){printf("第d行n",i)for(j=j<mj)scanf("d",aij)}输入三角形的三边a,b,c判断a,b,c能否构成三角形何种类型的三角形:等腰、等边、直角、等腰直角、一般。#include<stdioh>#include<mathh>voidmain(){floata,b,cprintf("请输入三角形边长:n")scanf("fff",a,b,c)if((ab>=c)||(bc>=a)||(ca>=b))printf("不能够成三角形n")elseif((a*ab*b==c*c)||(b*bc*c==a*a)||(c*ca*a==b*b))if((a==b)||(b==c)||(c==a))printf("等腰直角三角形n")elseprintf("直角三角形n")elseif((a==b)(b==c))printf("等边三角形n")elseif((a==b)(b!=c)||(c==b)(b!=a)||(a==c)(c!=a))printf("等腰三角形n")elseprintf("一般三角形n")}输入奖金数a计算并输出税率、应缴税款和实得奖金数。(用if语句编程)#include<stdioh>voidmain(){intafloatr,t,sprintf("请输入奖金数:n")scanf("d",a)if(a<)r=elseif(a<)r=elseif(a<)r=elseif(a<)r=elser=t=a*rs=atprintf("税率:fn",r)printf("税款:fn",t)printf("实得奖金:fn",s)}输入奖金数a计算并输出税率、应缴税款和实得奖金数。(用switch语句编程)#include<stdioh>voidmain(){inta,efloatr,t,sprintf("请输入奖金数:n")scanf("d",a)e=aswitch(e){case:r=breakcase:r=breakcase:case:r=breakcase:case:case:case:case:case:r=breakdefault:r=break}t=a*rs=atprintf("税率:fn",r)printf("税款:fn",t)printf("实得奖金:fn",s)}输入学生的成绩利用计算机将学生的成绩划分出等级并输出:~:A级~:B级~:C级~:D级~:E级#include<stdioh>voidmain(){floatmprintf("输入学生成绩:n")scanf("f",m)if((m>)||(m<))printf("数据有误n")elseif(m>=)printf("A级n")elseif(m>=)printf("B级n")elseif(m>=)printf("C级n")elseif(m>=)printf("D级n")elseprintf("E级n")}编程序求方程aXbXc=的解输入a,b,c#include<stdioh>#include<mathh>voidmain(){floata,b,c,tdoublex,xprintf("请由高次到低次顺序输入系数:n")scanf("fff",a,b,c)t=b*b*a*cif(t<)printf("方程无实根n")if(t==){x=(ba)printf("方程有两个相等实根x=x=fn",x)}if(t>){x=(bsqrt(t))ax=(bsqrt(t))aprintf("方程有两个不等实根x=f,x=fn",x,x)}}编程序输入个整数按由小到大的顺序输出。#include<stdioh>#defineNvoidmain(){intaN,t,i,jprintf("输入四个整数:n")for(i=i<Ni)scanf("d",ai)printf("n")for(i=i<Ni){for(j=j<Nij)if(aj>aj){t=ajaj=ajaj=t}}printf("排序后:n")for(i=i<Ni)printf("dn",ai)}求满足…n<中最大的N并求其和编写程序实现。#include<stdioh>voidmain(){intn=,sum=while(sum<){nsum=n}printf("N:dn",n)printf("d<n",n)}把~之间的不能被整除的数输出。#include<stdioh>voidmain(){intafor(a=a<=a)if(a!=)printf("dt",a)}求Fibonacci数列前个数每行输出个将个Fibonacci数输出。#include<stdioh>longf(int)voidmain(){intn,i=for(n=n<n){printf("ldt",f(n))iif(i==)printf("n")}}longf(intm){if(m==)returnif(m==)returnelsereturnf(m)f(m)}按以下规律翻译密码:将每一个字母变成它后面的字母例如将A变成BB变成C…Z变成A非字母字符不变“!”作为电文结束标志。#include<stdioh>voidmain(){charchprintf("输入字符串:n")while((ch=getchar())!='!'){if((ch>='a'ch<='z')||(ch>='A'ch<='Z'))if(ch=='z')ch='a'elseif(ch=='Z')ch='A'elsech=chprintf("c",ch)}printf("n")}百元买百鸡问题:公鸡每只元母鸡每只元小鸡只一元问一百元买一百只鸡有几种买法。#include<stdioh>voidmain(){inta,b,c,n=for(a=a<=a)for(b=b<=b)for(c=c<=c)if(*a*bc==)nprintf("一共有d种n",n)}使用数组编程计算出前项fibonacci数列要求一行打印个数。#include<stdioh>longf(int)voidmain(){intn,i=for(n=n<n){printf("ldt",f(n))iif(i==)printf("n")}}longf(intm){if(m==)returnif(m==)returnelsereturnf(m)f(m)}编程序求出两个*矩阵的和并将原矩阵和求出的和矩阵按原矩阵的形式分别输出。#include<stdioh>voidmain(){inta,b,s,n,mprintf("输入数组A:n")for(n=n<n){printf("第d行n",n)for(m=m<m)scanf("d",an)}printf("输入数组B:n")for(n=n<n){printf("第d行n",n)for(m=m<m)scanf("d",bn)}for(n=n<n){for(m=m<m)sn=anbn}printf("原数组A:n")for(n=n<n){for(m=m<m)printf("d",an)printf("n")}printf("n")printf("原数组B:n")for(n=n<n){for(m=m<m)printf("d",bn)printf("n")}printf("n")printf("所得数组:n")for(n=n<n){for(m=m<m)printf("d",sn)printf("n")}}将一个*的矩阵转置并将原矩阵和求出的转置矩阵按原矩阵的形式分别输出。#include<stdioh>voidmain(){inta,b,s,n,mprintf("输入数组A:n")for(n=n<n){printf("第d行n",n)for(m=m<m)scanf("d",an)}printf("输入数组B:n")for(n=n<n){printf("第d行n",n)for(m=m<m)scanf("d",bn)}for(n=n<n){for(m=m<m)sn=anbn}printf("原数组A:n")for(n=n<n){for(m=m<m)printf("d",an)printf("n")}printf("n")printf("原数组B:n")for(n=n<n){for

用户评论(0)

0/200

精彩专题

上传我的资料

每篇奖励 +1积分

资料评分:

/10
仅支持在线阅读

意见
反馈

立即扫码关注

爱问共享资料微信公众号

返回
顶部

举报
资料