我的c语言程序.docx
《我的c语言程序.docx》由会员分享,可在线阅读,更多相关《我的c语言程序.docx(15页珍藏版)》请在冰豆网上搜索。
我的c语言程序
1.求a的开方
#include
intmain()
{
floatx0,x1,a;
scanf("%f",&a);
x0=a/2;
x1=(x0+a/x0)/2;
do
{x0=x1;//循环体中这个必须先执行
}
while(fabs(x0-x1)>=1e-5);
printf("%f\n",x0);
return0;
2.完数:
一个数等于他的因子之和(6=1+2+3)
输出1000之内所有完数和其因子
intn,i,s;
for(n=2;n<=1000;n++)
s=0;//循环体中这个必须先执行
for(i=1;iif((n%i)==0)s=s+i;if(s==n){printf("%ditsfactorsare",n);for(i=1;iif((n%i)==0)printf("%d",i);putchar('\n');}}return0;}3.近似求π的值,同时输出最后一位循环精度以及循环次数#includeintmain(){inti;doublej=1,t,s=0;for(i=1;1/j>=1e-8;i=i+2)//此精度可以改{j=i;//必须要这样做,要把i变为double型才能分之一if(i%4==1)t=1/j;elset=-1/j;s=s+t;}i=i/2+1;printf("π=%15.14f\n循环精度%15.14f\n循环次数%d\n",s*4,t,i)return0;}//我的这个程序(for语句)显然比书上的算法简单 4.输出100到200之间的所有素数#includeintmain(){intn,i,s;for(n=101;n<=200;n++)//数据范围可以更改{s=0;//每次必须将s赋值为0for(i=2;i{if(n%i==0)s=s+i;}if(s==0)printf("%d",n);}putchar('\n');return0;} 5.输入两个数,求其最大公约数与最小公倍数#includeintmain(){intm,n,i,t;scanf("%d,%d",&m,&n);if(m{t=m;m=n;n=t;}for(i=1;i<=n;i++){if((n%i==0)&&(m%i==0)){t=i;break;}}printf("最大公约数为%d\n",t);for(i=m;;i++){if((i%n==0)&&(i%m==0)){t=i;break;}}printf("最小公倍数为%d\n",t);return0;} 6.用二分法求方程的根#includeintmain(){doublexa,xb,fa,fb;for(xa=-10,xb=10;(xb-xa)>=1e-15;)//范围可以改变{fa=2*xa*xa*xa-4*xa*xa+3*xa-6;//方程可以改变fb=2*xb*xb*xb-4*xb*xb+2*xb-6;if((fa+fb)/2<=0)xa=(xa+xb)/2;elseif((fa+fb)/2>0)xb=(xa+xb)/2;}printf("方程的解为x=%f\n",xa);return0;} 6.几个数从小到大排序(用数组)选择法#includeintmain(){printf("请输入五个整数:\n");inta[5];//数的个数可以改inti,j,m;for(i=0;i<5;i++)scanf("%d",&a[i]);for(i=0;i<5;i++){for(j=i+1;j<5;j++){if(a[j]{m=a[j];a[j]=a[i];a[i]=m;}}}for(i=0;i<5;i++)printf("%d",a[i]);putchar('\n');return0;}//又比老师的程序简单冒泡法排序#includeintmain(){printf("请输入五个整数:\n");inta[5];inti,j,m,o;//定义o简化算法for(i=0;i<5;i++)scanf("%d",&a[i]);for(i=0;i<4;i++){o=0;for(j=0;j<4-i;j++){if(a[j]>a[j+1]){m=a[j];a[j]=a[j+1];a[j+1]=m;o++;}}if(o==0)break;}for(i=0;i<5;i++)printf("%d",a[i]);putchar('\n');return0;} 7.求矩阵的转制#includeintmain(){inta[2][3]={{1,2,3},{4,5,6}};intb[3][2],i,j;printf("arraya:\n");for(i=0;i<=1;i++){for(j=0;j<=2;j++){printf("%5d",a[i][j]);b[j][i]=a[i][j];}putchar('\n');}printf("arrayb:\n");for(i=0;i<=2;i++){for(j=0;j<=1;j++)printf("%5d",b[i][j]);putchar('\n');}return0;}//优化讲义上所得8.用筛选法求10000以内所有素数#include#includeintmain(){inti,j,n,a[10001];//范围可改a[1]=0;for(i=2;i<=sqrt(10000);i++)for(j=2;j*i<=10000;j++)a[j*i]=0;for(i=1,n=0;i<=10000;i++){if(a[i]!=0){printf("%6d",i);n++;}//用n计数,每十个换行if(n==10){putchar('\n');n=0;}}putchar('\n');return0;}9.输出杨辉三角#include#defineN10//符号常量N为输出杨辉三角的行数,可一改全改intmain()//上面的几个可改数字的程序也可以如此做,方便简单{inti,j,a[N+1][N+1];for(i=1;i<=N;i++){a[i][1]=1;for(j=1;j<=N;j++)a[i][j]=1;}for(i=3;i<=N;i++)for(j=2;ja[i][j]=a[i-1][j-1]+a[i-1][j];for(i=1;i<=N;i++){for(j=1;j<=i;j++)printf("%5d",a[i][j]);putchar('\n');putchar('\n');}return0;} 10.得意之作!打印2014年年历表。#includeintmain(){inti,j,k,n=2;//i月份,j天数,k每月前空格,n列数计数intm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//m数组记每月天数printf("2014年年历表\n\n");for(i=1;i<=12;i++){switch(i){case1:printf("一月\n");break;case2:printf("二月\n");break;case3:printf("三月\n");break;case4:printf("四月\n");break;case5:printf("五月\n");break;case6:printf("六月\n");break;case7:printf("七月\n");break;case8:printf("八月\n");break;case9:printf("九月\n");break;case10:printf("十月\n");break;case11:printf("十一月\n");break;case12:printf("十二月\n");break;}printf("\n一二三四五六日\n");for(k=1;k<=n;k++)printf("");for(j=1;j<=m[i];j++){printf("%3d",j);n++;if(n==7){putchar('\n');n=0;}}printf("\n\n");}return0;}11.hanoi移盘子问题,运用函数递归,难!!!#includevoidmove(charx,chary){printf("%c-->%c\n",x,y);}voidhanoi(intn,chara,charb,charc){if(n==1)move(a,c);else{hanoi(n-1,a,c,b);move(a,c);hanoi(n-1,b,a,c);}}intmain(){intm;printf("hanoi问题,将n个盘子从A借助B移动到C。\n请输入盘子数n:");scanf("%d",&m);printf("移动方法为:\n");hanoi(m,'A','B','C');} 12.函数求最大公约数与最小公倍数#includeintY(inta,intb)//最大公约数用递归函数的辗转相除法求得{intc,d,x;c=a/b,d=a%b;if(d==0)x=b;elsex=Y(b,d);returnx;}intB(inta,intb){return(a*b/Y(a,b));//两数乘积等于其最大公约数与最小公倍数相乘}intmain(){inta,b;printf("请输入两个整数:");scanf("%d%d",&a,&b);printf("最大公约数为%d\n最小公倍数为%d\n",Y(a,b),B(a,b));return0;} 13,用函数求方程的根,判别式的情况都要考虑#include#includevoidroot(doublea,doubleb,doublec);voidroot1(doublea,doubleb,doublec);voidroot2(doublea,doubleb,doublec);voidroot3();intmain(){doublea,b,c;scanf("%lf%lf%lf",&a,&b,&c);root(a,b,c);return0;}voidroot(doublea,doubleb,doublec){doublex=b*b-4*a*c;if(x>0)root1(a,b,c);elseif(x==0)root2(a,b,c);elseroot3();}voidroot1(doublea,doubleb,doublec){doublex=sqrt(b*b-4*a*c);printf("Therootsis%f,%f\n",(-b-x)/(2*a),(-b+x)/(2*a));}voidroot2(doublea,doubleb,doublec){printf("Therootis%f\n",-b/(2*a));}voidroot3(){printf("无实根\n");}//并不高级,但是对于练习函数的用法有很大帮助 14.算法:n个人围成一圈1,2,3报数,报到3退出,问最后剩下的人是原来的第几个?(经过耗费脑细胞的优化算法,终于得到最简单的程序!累死!)#include#definen10intmain(){inta[n+1],i,j;for(i=1;i<=n;i++)a[i]=1;for(i=1,j=0;j!=3*n;i++){if(i==n+1)i=1;if(a[i]!=0)j++;if(j%3==0)a[i]=0;}printf("%d\n",i-1);return0;}#include#definen10#definet3intmain(){inta[n+1],i,j;for(i=1;i<=n;i++)a[i]=1;for(i=1,j=0;j!=t*n;i++){if(i==n+1)i=1;if(a[i]!=0)j++;if((j%t==0)&&(a[i]!=0)){a[i]=0;printf("%3d\n",i);}}return0;} 15.关于四个学生五门成绩的一些处理(没什么的,只是它长,练习之用,留下以作纪念)。P29215题#includeintmain(){voidaverage1(int(*a)[5]);voidsearch1(int(*a)[5]);voidsearch2(int(*a)[5]);inta[4][5]={{77,88,99,44,55},{65,54,98,78,23},{85,95,96,94,100},{45,100,100,100,15}};for(int*p=*a,i=1;p<=*a+19;p++,i++){printf("%4d",*p);if(i%5==0)putchar('\n');}average1(a);search1(a);search2(a);return0;}intaverage(int*p){for(inti=1,s=0;i<=5;i++,p++)s=s+*p;return(s/5);}voidaverage1(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++)s=s+**a;printf("average=%d\n",s/5);}voidsearch1(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++){for(intj=0,t=0;j<5;j++)if(*(*a+j)<60)t++;if(t>1){printf("%dfail.\nher/hisscoresis:",i+1);for(intj=0;j<5;j++)printf("%d",*(*a+j));printf("\naveragescore:%d\n",average(*a));}}}voidsearch2(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++){for(intj=0,t1=0,t2=0;j<5;j++){if(*(*a+j)>=85)t1++;if(*(*a+j)>=95)t2++;}if(t1>1||t2>0)printf("%dwins\n",i+1);}}
if((n%i)==0)s=s+i;
if(s==n)
printf("%ditsfactorsare",n);
for(i=1;iif((n%i)==0)printf("%d",i);putchar('\n');}}return0;}3.近似求π的值,同时输出最后一位循环精度以及循环次数#includeintmain(){inti;doublej=1,t,s=0;for(i=1;1/j>=1e-8;i=i+2)//此精度可以改{j=i;//必须要这样做,要把i变为double型才能分之一if(i%4==1)t=1/j;elset=-1/j;s=s+t;}i=i/2+1;printf("π=%15.14f\n循环精度%15.14f\n循环次数%d\n",s*4,t,i)return0;}//我的这个程序(for语句)显然比书上的算法简单 4.输出100到200之间的所有素数#includeintmain(){intn,i,s;for(n=101;n<=200;n++)//数据范围可以更改{s=0;//每次必须将s赋值为0for(i=2;i{if(n%i==0)s=s+i;}if(s==0)printf("%d",n);}putchar('\n');return0;} 5.输入两个数,求其最大公约数与最小公倍数#includeintmain(){intm,n,i,t;scanf("%d,%d",&m,&n);if(m{t=m;m=n;n=t;}for(i=1;i<=n;i++){if((n%i==0)&&(m%i==0)){t=i;break;}}printf("最大公约数为%d\n",t);for(i=m;;i++){if((i%n==0)&&(i%m==0)){t=i;break;}}printf("最小公倍数为%d\n",t);return0;} 6.用二分法求方程的根#includeintmain(){doublexa,xb,fa,fb;for(xa=-10,xb=10;(xb-xa)>=1e-15;)//范围可以改变{fa=2*xa*xa*xa-4*xa*xa+3*xa-6;//方程可以改变fb=2*xb*xb*xb-4*xb*xb+2*xb-6;if((fa+fb)/2<=0)xa=(xa+xb)/2;elseif((fa+fb)/2>0)xb=(xa+xb)/2;}printf("方程的解为x=%f\n",xa);return0;} 6.几个数从小到大排序(用数组)选择法#includeintmain(){printf("请输入五个整数:\n");inta[5];//数的个数可以改inti,j,m;for(i=0;i<5;i++)scanf("%d",&a[i]);for(i=0;i<5;i++){for(j=i+1;j<5;j++){if(a[j]{m=a[j];a[j]=a[i];a[i]=m;}}}for(i=0;i<5;i++)printf("%d",a[i]);putchar('\n');return0;}//又比老师的程序简单冒泡法排序#includeintmain(){printf("请输入五个整数:\n");inta[5];inti,j,m,o;//定义o简化算法for(i=0;i<5;i++)scanf("%d",&a[i]);for(i=0;i<4;i++){o=0;for(j=0;j<4-i;j++){if(a[j]>a[j+1]){m=a[j];a[j]=a[j+1];a[j+1]=m;o++;}}if(o==0)break;}for(i=0;i<5;i++)printf("%d",a[i]);putchar('\n');return0;} 7.求矩阵的转制#includeintmain(){inta[2][3]={{1,2,3},{4,5,6}};intb[3][2],i,j;printf("arraya:\n");for(i=0;i<=1;i++){for(j=0;j<=2;j++){printf("%5d",a[i][j]);b[j][i]=a[i][j];}putchar('\n');}printf("arrayb:\n");for(i=0;i<=2;i++){for(j=0;j<=1;j++)printf("%5d",b[i][j]);putchar('\n');}return0;}//优化讲义上所得8.用筛选法求10000以内所有素数#include#includeintmain(){inti,j,n,a[10001];//范围可改a[1]=0;for(i=2;i<=sqrt(10000);i++)for(j=2;j*i<=10000;j++)a[j*i]=0;for(i=1,n=0;i<=10000;i++){if(a[i]!=0){printf("%6d",i);n++;}//用n计数,每十个换行if(n==10){putchar('\n');n=0;}}putchar('\n');return0;}9.输出杨辉三角#include#defineN10//符号常量N为输出杨辉三角的行数,可一改全改intmain()//上面的几个可改数字的程序也可以如此做,方便简单{inti,j,a[N+1][N+1];for(i=1;i<=N;i++){a[i][1]=1;for(j=1;j<=N;j++)a[i][j]=1;}for(i=3;i<=N;i++)for(j=2;ja[i][j]=a[i-1][j-1]+a[i-1][j];for(i=1;i<=N;i++){for(j=1;j<=i;j++)printf("%5d",a[i][j]);putchar('\n');putchar('\n');}return0;} 10.得意之作!打印2014年年历表。#includeintmain(){inti,j,k,n=2;//i月份,j天数,k每月前空格,n列数计数intm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//m数组记每月天数printf("2014年年历表\n\n");for(i=1;i<=12;i++){switch(i){case1:printf("一月\n");break;case2:printf("二月\n");break;case3:printf("三月\n");break;case4:printf("四月\n");break;case5:printf("五月\n");break;case6:printf("六月\n");break;case7:printf("七月\n");break;case8:printf("八月\n");break;case9:printf("九月\n");break;case10:printf("十月\n");break;case11:printf("十一月\n");break;case12:printf("十二月\n");break;}printf("\n一二三四五六日\n");for(k=1;k<=n;k++)printf("");for(j=1;j<=m[i];j++){printf("%3d",j);n++;if(n==7){putchar('\n');n=0;}}printf("\n\n");}return0;}11.hanoi移盘子问题,运用函数递归,难!!!#includevoidmove(charx,chary){printf("%c-->%c\n",x,y);}voidhanoi(intn,chara,charb,charc){if(n==1)move(a,c);else{hanoi(n-1,a,c,b);move(a,c);hanoi(n-1,b,a,c);}}intmain(){intm;printf("hanoi问题,将n个盘子从A借助B移动到C。\n请输入盘子数n:");scanf("%d",&m);printf("移动方法为:\n");hanoi(m,'A','B','C');} 12.函数求最大公约数与最小公倍数#includeintY(inta,intb)//最大公约数用递归函数的辗转相除法求得{intc,d,x;c=a/b,d=a%b;if(d==0)x=b;elsex=Y(b,d);returnx;}intB(inta,intb){return(a*b/Y(a,b));//两数乘积等于其最大公约数与最小公倍数相乘}intmain(){inta,b;printf("请输入两个整数:");scanf("%d%d",&a,&b);printf("最大公约数为%d\n最小公倍数为%d\n",Y(a,b),B(a,b));return0;} 13,用函数求方程的根,判别式的情况都要考虑#include#includevoidroot(doublea,doubleb,doublec);voidroot1(doublea,doubleb,doublec);voidroot2(doublea,doubleb,doublec);voidroot3();intmain(){doublea,b,c;scanf("%lf%lf%lf",&a,&b,&c);root(a,b,c);return0;}voidroot(doublea,doubleb,doublec){doublex=b*b-4*a*c;if(x>0)root1(a,b,c);elseif(x==0)root2(a,b,c);elseroot3();}voidroot1(doublea,doubleb,doublec){doublex=sqrt(b*b-4*a*c);printf("Therootsis%f,%f\n",(-b-x)/(2*a),(-b+x)/(2*a));}voidroot2(doublea,doubleb,doublec){printf("Therootis%f\n",-b/(2*a));}voidroot3(){printf("无实根\n");}//并不高级,但是对于练习函数的用法有很大帮助 14.算法:n个人围成一圈1,2,3报数,报到3退出,问最后剩下的人是原来的第几个?(经过耗费脑细胞的优化算法,终于得到最简单的程序!累死!)#include#definen10intmain(){inta[n+1],i,j;for(i=1;i<=n;i++)a[i]=1;for(i=1,j=0;j!=3*n;i++){if(i==n+1)i=1;if(a[i]!=0)j++;if(j%3==0)a[i]=0;}printf("%d\n",i-1);return0;}#include#definen10#definet3intmain(){inta[n+1],i,j;for(i=1;i<=n;i++)a[i]=1;for(i=1,j=0;j!=t*n;i++){if(i==n+1)i=1;if(a[i]!=0)j++;if((j%t==0)&&(a[i]!=0)){a[i]=0;printf("%3d\n",i);}}return0;} 15.关于四个学生五门成绩的一些处理(没什么的,只是它长,练习之用,留下以作纪念)。P29215题#includeintmain(){voidaverage1(int(*a)[5]);voidsearch1(int(*a)[5]);voidsearch2(int(*a)[5]);inta[4][5]={{77,88,99,44,55},{65,54,98,78,23},{85,95,96,94,100},{45,100,100,100,15}};for(int*p=*a,i=1;p<=*a+19;p++,i++){printf("%4d",*p);if(i%5==0)putchar('\n');}average1(a);search1(a);search2(a);return0;}intaverage(int*p){for(inti=1,s=0;i<=5;i++,p++)s=s+*p;return(s/5);}voidaverage1(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++)s=s+**a;printf("average=%d\n",s/5);}voidsearch1(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++){for(intj=0,t=0;j<5;j++)if(*(*a+j)<60)t++;if(t>1){printf("%dfail.\nher/hisscoresis:",i+1);for(intj=0;j<5;j++)printf("%d",*(*a+j));printf("\naveragescore:%d\n",average(*a));}}}voidsearch2(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++){for(intj=0,t1=0,t2=0;j<5;j++){if(*(*a+j)>=85)t1++;if(*(*a+j)>=95)t2++;}if(t1>1||t2>0)printf("%dwins\n",i+1);}}
if((n%i)==0)printf("%d",i);
putchar('\n');
3.近似求π的值,同时输出最后一位循环精度以及循环次数
inti;
doublej=1,t,s=0;
for(i=1;1/j>=1e-8;i=i+2)//此精度可以改
j=i;//必须要这样做,要把i变为double型才能分之一
if(i%4==1)t=1/j;
elset=-1/j;
s=s+t;
i=i/2+1;
printf("π=%15.14f\n循环精度%15.14f\n循环次数%d\n",s*4,t,i)
}//我的这个程序(for语句)显然比书上的算法简单
4.输出100到200之间的所有素数
for(n=101;n<=200;n++)//数据范围可以更改
s=0;//每次必须将s赋值为0
for(i=2;i{if(n%i==0)s=s+i;}if(s==0)printf("%d",n);}putchar('\n');return0;} 5.输入两个数,求其最大公约数与最小公倍数#includeintmain(){intm,n,i,t;scanf("%d,%d",&m,&n);if(m{t=m;m=n;n=t;}for(i=1;i<=n;i++){if((n%i==0)&&(m%i==0)){t=i;break;}}printf("最大公约数为%d\n",t);for(i=m;;i++){if((i%n==0)&&(i%m==0)){t=i;break;}}printf("最小公倍数为%d\n",t);return0;} 6.用二分法求方程的根#includeintmain(){doublexa,xb,fa,fb;for(xa=-10,xb=10;(xb-xa)>=1e-15;)//范围可以改变{fa=2*xa*xa*xa-4*xa*xa+3*xa-6;//方程可以改变fb=2*xb*xb*xb-4*xb*xb+2*xb-6;if((fa+fb)/2<=0)xa=(xa+xb)/2;elseif((fa+fb)/2>0)xb=(xa+xb)/2;}printf("方程的解为x=%f\n",xa);return0;} 6.几个数从小到大排序(用数组)选择法#includeintmain(){printf("请输入五个整数:\n");inta[5];//数的个数可以改inti,j,m;for(i=0;i<5;i++)scanf("%d",&a[i]);for(i=0;i<5;i++){for(j=i+1;j<5;j++){if(a[j]{m=a[j];a[j]=a[i];a[i]=m;}}}for(i=0;i<5;i++)printf("%d",a[i]);putchar('\n');return0;}//又比老师的程序简单冒泡法排序#includeintmain(){printf("请输入五个整数:\n");inta[5];inti,j,m,o;//定义o简化算法for(i=0;i<5;i++)scanf("%d",&a[i]);for(i=0;i<4;i++){o=0;for(j=0;j<4-i;j++){if(a[j]>a[j+1]){m=a[j];a[j]=a[j+1];a[j+1]=m;o++;}}if(o==0)break;}for(i=0;i<5;i++)printf("%d",a[i]);putchar('\n');return0;} 7.求矩阵的转制#includeintmain(){inta[2][3]={{1,2,3},{4,5,6}};intb[3][2],i,j;printf("arraya:\n");for(i=0;i<=1;i++){for(j=0;j<=2;j++){printf("%5d",a[i][j]);b[j][i]=a[i][j];}putchar('\n');}printf("arrayb:\n");for(i=0;i<=2;i++){for(j=0;j<=1;j++)printf("%5d",b[i][j]);putchar('\n');}return0;}//优化讲义上所得8.用筛选法求10000以内所有素数#include#includeintmain(){inti,j,n,a[10001];//范围可改a[1]=0;for(i=2;i<=sqrt(10000);i++)for(j=2;j*i<=10000;j++)a[j*i]=0;for(i=1,n=0;i<=10000;i++){if(a[i]!=0){printf("%6d",i);n++;}//用n计数,每十个换行if(n==10){putchar('\n');n=0;}}putchar('\n');return0;}9.输出杨辉三角#include#defineN10//符号常量N为输出杨辉三角的行数,可一改全改intmain()//上面的几个可改数字的程序也可以如此做,方便简单{inti,j,a[N+1][N+1];for(i=1;i<=N;i++){a[i][1]=1;for(j=1;j<=N;j++)a[i][j]=1;}for(i=3;i<=N;i++)for(j=2;ja[i][j]=a[i-1][j-1]+a[i-1][j];for(i=1;i<=N;i++){for(j=1;j<=i;j++)printf("%5d",a[i][j]);putchar('\n');putchar('\n');}return0;} 10.得意之作!打印2014年年历表。#includeintmain(){inti,j,k,n=2;//i月份,j天数,k每月前空格,n列数计数intm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//m数组记每月天数printf("2014年年历表\n\n");for(i=1;i<=12;i++){switch(i){case1:printf("一月\n");break;case2:printf("二月\n");break;case3:printf("三月\n");break;case4:printf("四月\n");break;case5:printf("五月\n");break;case6:printf("六月\n");break;case7:printf("七月\n");break;case8:printf("八月\n");break;case9:printf("九月\n");break;case10:printf("十月\n");break;case11:printf("十一月\n");break;case12:printf("十二月\n");break;}printf("\n一二三四五六日\n");for(k=1;k<=n;k++)printf("");for(j=1;j<=m[i];j++){printf("%3d",j);n++;if(n==7){putchar('\n');n=0;}}printf("\n\n");}return0;}11.hanoi移盘子问题,运用函数递归,难!!!#includevoidmove(charx,chary){printf("%c-->%c\n",x,y);}voidhanoi(intn,chara,charb,charc){if(n==1)move(a,c);else{hanoi(n-1,a,c,b);move(a,c);hanoi(n-1,b,a,c);}}intmain(){intm;printf("hanoi问题,将n个盘子从A借助B移动到C。\n请输入盘子数n:");scanf("%d",&m);printf("移动方法为:\n");hanoi(m,'A','B','C');} 12.函数求最大公约数与最小公倍数#includeintY(inta,intb)//最大公约数用递归函数的辗转相除法求得{intc,d,x;c=a/b,d=a%b;if(d==0)x=b;elsex=Y(b,d);returnx;}intB(inta,intb){return(a*b/Y(a,b));//两数乘积等于其最大公约数与最小公倍数相乘}intmain(){inta,b;printf("请输入两个整数:");scanf("%d%d",&a,&b);printf("最大公约数为%d\n最小公倍数为%d\n",Y(a,b),B(a,b));return0;} 13,用函数求方程的根,判别式的情况都要考虑#include#includevoidroot(doublea,doubleb,doublec);voidroot1(doublea,doubleb,doublec);voidroot2(doublea,doubleb,doublec);voidroot3();intmain(){doublea,b,c;scanf("%lf%lf%lf",&a,&b,&c);root(a,b,c);return0;}voidroot(doublea,doubleb,doublec){doublex=b*b-4*a*c;if(x>0)root1(a,b,c);elseif(x==0)root2(a,b,c);elseroot3();}voidroot1(doublea,doubleb,doublec){doublex=sqrt(b*b-4*a*c);printf("Therootsis%f,%f\n",(-b-x)/(2*a),(-b+x)/(2*a));}voidroot2(doublea,doubleb,doublec){printf("Therootis%f\n",-b/(2*a));}voidroot3(){printf("无实根\n");}//并不高级,但是对于练习函数的用法有很大帮助 14.算法:n个人围成一圈1,2,3报数,报到3退出,问最后剩下的人是原来的第几个?(经过耗费脑细胞的优化算法,终于得到最简单的程序!累死!)#include#definen10intmain(){inta[n+1],i,j;for(i=1;i<=n;i++)a[i]=1;for(i=1,j=0;j!=3*n;i++){if(i==n+1)i=1;if(a[i]!=0)j++;if(j%3==0)a[i]=0;}printf("%d\n",i-1);return0;}#include#definen10#definet3intmain(){inta[n+1],i,j;for(i=1;i<=n;i++)a[i]=1;for(i=1,j=0;j!=t*n;i++){if(i==n+1)i=1;if(a[i]!=0)j++;if((j%t==0)&&(a[i]!=0)){a[i]=0;printf("%3d\n",i);}}return0;} 15.关于四个学生五门成绩的一些处理(没什么的,只是它长,练习之用,留下以作纪念)。P29215题#includeintmain(){voidaverage1(int(*a)[5]);voidsearch1(int(*a)[5]);voidsearch2(int(*a)[5]);inta[4][5]={{77,88,99,44,55},{65,54,98,78,23},{85,95,96,94,100},{45,100,100,100,15}};for(int*p=*a,i=1;p<=*a+19;p++,i++){printf("%4d",*p);if(i%5==0)putchar('\n');}average1(a);search1(a);search2(a);return0;}intaverage(int*p){for(inti=1,s=0;i<=5;i++,p++)s=s+*p;return(s/5);}voidaverage1(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++)s=s+**a;printf("average=%d\n",s/5);}voidsearch1(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++){for(intj=0,t=0;j<5;j++)if(*(*a+j)<60)t++;if(t>1){printf("%dfail.\nher/hisscoresis:",i+1);for(intj=0;j<5;j++)printf("%d",*(*a+j));printf("\naveragescore:%d\n",average(*a));}}}voidsearch2(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++){for(intj=0,t1=0,t2=0;j<5;j++){if(*(*a+j)>=85)t1++;if(*(*a+j)>=95)t2++;}if(t1>1||t2>0)printf("%dwins\n",i+1);}}
if(n%i==0)s=s+i;
if(s==0)printf("%d",n);
5.输入两个数,求其最大公约数与最小公倍数
intm,n,i,t;
scanf("%d,%d",&m,&n);
if(m{t=m;m=n;n=t;}for(i=1;i<=n;i++){if((n%i==0)&&(m%i==0)){t=i;break;}}printf("最大公约数为%d\n",t);for(i=m;;i++){if((i%n==0)&&(i%m==0)){t=i;break;}}printf("最小公倍数为%d\n",t);return0;} 6.用二分法求方程的根#includeintmain(){doublexa,xb,fa,fb;for(xa=-10,xb=10;(xb-xa)>=1e-15;)//范围可以改变{fa=2*xa*xa*xa-4*xa*xa+3*xa-6;//方程可以改变fb=2*xb*xb*xb-4*xb*xb+2*xb-6;if((fa+fb)/2<=0)xa=(xa+xb)/2;elseif((fa+fb)/2>0)xb=(xa+xb)/2;}printf("方程的解为x=%f\n",xa);return0;} 6.几个数从小到大排序(用数组)选择法#includeintmain(){printf("请输入五个整数:\n");inta[5];//数的个数可以改inti,j,m;for(i=0;i<5;i++)scanf("%d",&a[i]);for(i=0;i<5;i++){for(j=i+1;j<5;j++){if(a[j]{m=a[j];a[j]=a[i];a[i]=m;}}}for(i=0;i<5;i++)printf("%d",a[i]);putchar('\n');return0;}//又比老师的程序简单冒泡法排序#includeintmain(){printf("请输入五个整数:\n");inta[5];inti,j,m,o;//定义o简化算法for(i=0;i<5;i++)scanf("%d",&a[i]);for(i=0;i<4;i++){o=0;for(j=0;j<4-i;j++){if(a[j]>a[j+1]){m=a[j];a[j]=a[j+1];a[j+1]=m;o++;}}if(o==0)break;}for(i=0;i<5;i++)printf("%d",a[i]);putchar('\n');return0;} 7.求矩阵的转制#includeintmain(){inta[2][3]={{1,2,3},{4,5,6}};intb[3][2],i,j;printf("arraya:\n");for(i=0;i<=1;i++){for(j=0;j<=2;j++){printf("%5d",a[i][j]);b[j][i]=a[i][j];}putchar('\n');}printf("arrayb:\n");for(i=0;i<=2;i++){for(j=0;j<=1;j++)printf("%5d",b[i][j]);putchar('\n');}return0;}//优化讲义上所得8.用筛选法求10000以内所有素数#include#includeintmain(){inti,j,n,a[10001];//范围可改a[1]=0;for(i=2;i<=sqrt(10000);i++)for(j=2;j*i<=10000;j++)a[j*i]=0;for(i=1,n=0;i<=10000;i++){if(a[i]!=0){printf("%6d",i);n++;}//用n计数,每十个换行if(n==10){putchar('\n');n=0;}}putchar('\n');return0;}9.输出杨辉三角#include#defineN10//符号常量N为输出杨辉三角的行数,可一改全改intmain()//上面的几个可改数字的程序也可以如此做,方便简单{inti,j,a[N+1][N+1];for(i=1;i<=N;i++){a[i][1]=1;for(j=1;j<=N;j++)a[i][j]=1;}for(i=3;i<=N;i++)for(j=2;ja[i][j]=a[i-1][j-1]+a[i-1][j];for(i=1;i<=N;i++){for(j=1;j<=i;j++)printf("%5d",a[i][j]);putchar('\n');putchar('\n');}return0;} 10.得意之作!打印2014年年历表。#includeintmain(){inti,j,k,n=2;//i月份,j天数,k每月前空格,n列数计数intm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//m数组记每月天数printf("2014年年历表\n\n");for(i=1;i<=12;i++){switch(i){case1:printf("一月\n");break;case2:printf("二月\n");break;case3:printf("三月\n");break;case4:printf("四月\n");break;case5:printf("五月\n");break;case6:printf("六月\n");break;case7:printf("七月\n");break;case8:printf("八月\n");break;case9:printf("九月\n");break;case10:printf("十月\n");break;case11:printf("十一月\n");break;case12:printf("十二月\n");break;}printf("\n一二三四五六日\n");for(k=1;k<=n;k++)printf("");for(j=1;j<=m[i];j++){printf("%3d",j);n++;if(n==7){putchar('\n');n=0;}}printf("\n\n");}return0;}11.hanoi移盘子问题,运用函数递归,难!!!#includevoidmove(charx,chary){printf("%c-->%c\n",x,y);}voidhanoi(intn,chara,charb,charc){if(n==1)move(a,c);else{hanoi(n-1,a,c,b);move(a,c);hanoi(n-1,b,a,c);}}intmain(){intm;printf("hanoi问题,将n个盘子从A借助B移动到C。\n请输入盘子数n:");scanf("%d",&m);printf("移动方法为:\n");hanoi(m,'A','B','C');} 12.函数求最大公约数与最小公倍数#includeintY(inta,intb)//最大公约数用递归函数的辗转相除法求得{intc,d,x;c=a/b,d=a%b;if(d==0)x=b;elsex=Y(b,d);returnx;}intB(inta,intb){return(a*b/Y(a,b));//两数乘积等于其最大公约数与最小公倍数相乘}intmain(){inta,b;printf("请输入两个整数:");scanf("%d%d",&a,&b);printf("最大公约数为%d\n最小公倍数为%d\n",Y(a,b),B(a,b));return0;} 13,用函数求方程的根,判别式的情况都要考虑#include#includevoidroot(doublea,doubleb,doublec);voidroot1(doublea,doubleb,doublec);voidroot2(doublea,doubleb,doublec);voidroot3();intmain(){doublea,b,c;scanf("%lf%lf%lf",&a,&b,&c);root(a,b,c);return0;}voidroot(doublea,doubleb,doublec){doublex=b*b-4*a*c;if(x>0)root1(a,b,c);elseif(x==0)root2(a,b,c);elseroot3();}voidroot1(doublea,doubleb,doublec){doublex=sqrt(b*b-4*a*c);printf("Therootsis%f,%f\n",(-b-x)/(2*a),(-b+x)/(2*a));}voidroot2(doublea,doubleb,doublec){printf("Therootis%f\n",-b/(2*a));}voidroot3(){printf("无实根\n");}//并不高级,但是对于练习函数的用法有很大帮助 14.算法:n个人围成一圈1,2,3报数,报到3退出,问最后剩下的人是原来的第几个?(经过耗费脑细胞的优化算法,终于得到最简单的程序!累死!)#include#definen10intmain(){inta[n+1],i,j;for(i=1;i<=n;i++)a[i]=1;for(i=1,j=0;j!=3*n;i++){if(i==n+1)i=1;if(a[i]!=0)j++;if(j%3==0)a[i]=0;}printf("%d\n",i-1);return0;}#include#definen10#definet3intmain(){inta[n+1],i,j;for(i=1;i<=n;i++)a[i]=1;for(i=1,j=0;j!=t*n;i++){if(i==n+1)i=1;if(a[i]!=0)j++;if((j%t==0)&&(a[i]!=0)){a[i]=0;printf("%3d\n",i);}}return0;} 15.关于四个学生五门成绩的一些处理(没什么的,只是它长,练习之用,留下以作纪念)。P29215题#includeintmain(){voidaverage1(int(*a)[5]);voidsearch1(int(*a)[5]);voidsearch2(int(*a)[5]);inta[4][5]={{77,88,99,44,55},{65,54,98,78,23},{85,95,96,94,100},{45,100,100,100,15}};for(int*p=*a,i=1;p<=*a+19;p++,i++){printf("%4d",*p);if(i%5==0)putchar('\n');}average1(a);search1(a);search2(a);return0;}intaverage(int*p){for(inti=1,s=0;i<=5;i++,p++)s=s+*p;return(s/5);}voidaverage1(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++)s=s+**a;printf("average=%d\n",s/5);}voidsearch1(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++){for(intj=0,t=0;j<5;j++)if(*(*a+j)<60)t++;if(t>1){printf("%dfail.\nher/hisscoresis:",i+1);for(intj=0;j<5;j++)printf("%d",*(*a+j));printf("\naveragescore:%d\n",average(*a));}}}voidsearch2(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++){for(intj=0,t1=0,t2=0;j<5;j++){if(*(*a+j)>=85)t1++;if(*(*a+j)>=95)t2++;}if(t1>1||t2>0)printf("%dwins\n",i+1);}}
{t=m;m=n;n=t;}
for(i=1;i<=n;i++)
{if((n%i==0)&&(m%i==0)){t=i;break;}}
printf("最大公约数为%d\n",t);
for(i=m;;i++)
if((i%n==0)&&(i%m==0))
{t=i;break;}
printf("最小公倍数为%d\n",t);
6.用二分法求方程的根
doublexa,xb,fa,fb;
for(xa=-10,xb=10;(xb-xa)>=1e-15;)//范围可以改变
fa=2*xa*xa*xa-4*xa*xa+3*xa-6;//方程可以改变
fb=2*xb*xb*xb-4*xb*xb+2*xb-6;
if((fa+fb)/2<=0)xa=(xa+xb)/2;
elseif((fa+fb)/2>0)xb=(xa+xb)/2;
printf("方程的解为x=%f\n",xa);
6.几个数从小到大排序(用数组)选择法
printf("请输入五个整数:
\n");
inta[5];//数的个数可以改
inti,j,m;
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(j=i+1;j<5;j++)
if(a[j]{m=a[j];a[j]=a[i];a[i]=m;}}}for(i=0;i<5;i++)printf("%d",a[i]);putchar('\n');return0;}//又比老师的程序简单冒泡法排序#includeintmain(){printf("请输入五个整数:\n");inta[5];inti,j,m,o;//定义o简化算法for(i=0;i<5;i++)scanf("%d",&a[i]);for(i=0;i<4;i++){o=0;for(j=0;j<4-i;j++){if(a[j]>a[j+1]){m=a[j];a[j]=a[j+1];a[j+1]=m;o++;}}if(o==0)break;}for(i=0;i<5;i++)printf("%d",a[i]);putchar('\n');return0;} 7.求矩阵的转制#includeintmain(){inta[2][3]={{1,2,3},{4,5,6}};intb[3][2],i,j;printf("arraya:\n");for(i=0;i<=1;i++){for(j=0;j<=2;j++){printf("%5d",a[i][j]);b[j][i]=a[i][j];}putchar('\n');}printf("arrayb:\n");for(i=0;i<=2;i++){for(j=0;j<=1;j++)printf("%5d",b[i][j]);putchar('\n');}return0;}//优化讲义上所得8.用筛选法求10000以内所有素数#include#includeintmain(){inti,j,n,a[10001];//范围可改a[1]=0;for(i=2;i<=sqrt(10000);i++)for(j=2;j*i<=10000;j++)a[j*i]=0;for(i=1,n=0;i<=10000;i++){if(a[i]!=0){printf("%6d",i);n++;}//用n计数,每十个换行if(n==10){putchar('\n');n=0;}}putchar('\n');return0;}9.输出杨辉三角#include#defineN10//符号常量N为输出杨辉三角的行数,可一改全改intmain()//上面的几个可改数字的程序也可以如此做,方便简单{inti,j,a[N+1][N+1];for(i=1;i<=N;i++){a[i][1]=1;for(j=1;j<=N;j++)a[i][j]=1;}for(i=3;i<=N;i++)for(j=2;ja[i][j]=a[i-1][j-1]+a[i-1][j];for(i=1;i<=N;i++){for(j=1;j<=i;j++)printf("%5d",a[i][j]);putchar('\n');putchar('\n');}return0;} 10.得意之作!打印2014年年历表。#includeintmain(){inti,j,k,n=2;//i月份,j天数,k每月前空格,n列数计数intm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//m数组记每月天数printf("2014年年历表\n\n");for(i=1;i<=12;i++){switch(i){case1:printf("一月\n");break;case2:printf("二月\n");break;case3:printf("三月\n");break;case4:printf("四月\n");break;case5:printf("五月\n");break;case6:printf("六月\n");break;case7:printf("七月\n");break;case8:printf("八月\n");break;case9:printf("九月\n");break;case10:printf("十月\n");break;case11:printf("十一月\n");break;case12:printf("十二月\n");break;}printf("\n一二三四五六日\n");for(k=1;k<=n;k++)printf("");for(j=1;j<=m[i];j++){printf("%3d",j);n++;if(n==7){putchar('\n');n=0;}}printf("\n\n");}return0;}11.hanoi移盘子问题,运用函数递归,难!!!#includevoidmove(charx,chary){printf("%c-->%c\n",x,y);}voidhanoi(intn,chara,charb,charc){if(n==1)move(a,c);else{hanoi(n-1,a,c,b);move(a,c);hanoi(n-1,b,a,c);}}intmain(){intm;printf("hanoi问题,将n个盘子从A借助B移动到C。\n请输入盘子数n:");scanf("%d",&m);printf("移动方法为:\n");hanoi(m,'A','B','C');} 12.函数求最大公约数与最小公倍数#includeintY(inta,intb)//最大公约数用递归函数的辗转相除法求得{intc,d,x;c=a/b,d=a%b;if(d==0)x=b;elsex=Y(b,d);returnx;}intB(inta,intb){return(a*b/Y(a,b));//两数乘积等于其最大公约数与最小公倍数相乘}intmain(){inta,b;printf("请输入两个整数:");scanf("%d%d",&a,&b);printf("最大公约数为%d\n最小公倍数为%d\n",Y(a,b),B(a,b));return0;} 13,用函数求方程的根,判别式的情况都要考虑#include#includevoidroot(doublea,doubleb,doublec);voidroot1(doublea,doubleb,doublec);voidroot2(doublea,doubleb,doublec);voidroot3();intmain(){doublea,b,c;scanf("%lf%lf%lf",&a,&b,&c);root(a,b,c);return0;}voidroot(doublea,doubleb,doublec){doublex=b*b-4*a*c;if(x>0)root1(a,b,c);elseif(x==0)root2(a,b,c);elseroot3();}voidroot1(doublea,doubleb,doublec){doublex=sqrt(b*b-4*a*c);printf("Therootsis%f,%f\n",(-b-x)/(2*a),(-b+x)/(2*a));}voidroot2(doublea,doubleb,doublec){printf("Therootis%f\n",-b/(2*a));}voidroot3(){printf("无实根\n");}//并不高级,但是对于练习函数的用法有很大帮助 14.算法:n个人围成一圈1,2,3报数,报到3退出,问最后剩下的人是原来的第几个?(经过耗费脑细胞的优化算法,终于得到最简单的程序!累死!)#include#definen10intmain(){inta[n+1],i,j;for(i=1;i<=n;i++)a[i]=1;for(i=1,j=0;j!=3*n;i++){if(i==n+1)i=1;if(a[i]!=0)j++;if(j%3==0)a[i]=0;}printf("%d\n",i-1);return0;}#include#definen10#definet3intmain(){inta[n+1],i,j;for(i=1;i<=n;i++)a[i]=1;for(i=1,j=0;j!=t*n;i++){if(i==n+1)i=1;if(a[i]!=0)j++;if((j%t==0)&&(a[i]!=0)){a[i]=0;printf("%3d\n",i);}}return0;} 15.关于四个学生五门成绩的一些处理(没什么的,只是它长,练习之用,留下以作纪念)。P29215题#includeintmain(){voidaverage1(int(*a)[5]);voidsearch1(int(*a)[5]);voidsearch2(int(*a)[5]);inta[4][5]={{77,88,99,44,55},{65,54,98,78,23},{85,95,96,94,100},{45,100,100,100,15}};for(int*p=*a,i=1;p<=*a+19;p++,i++){printf("%4d",*p);if(i%5==0)putchar('\n');}average1(a);search1(a);search2(a);return0;}intaverage(int*p){for(inti=1,s=0;i<=5;i++,p++)s=s+*p;return(s/5);}voidaverage1(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++)s=s+**a;printf("average=%d\n",s/5);}voidsearch1(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++){for(intj=0,t=0;j<5;j++)if(*(*a+j)<60)t++;if(t>1){printf("%dfail.\nher/hisscoresis:",i+1);for(intj=0;j<5;j++)printf("%d",*(*a+j));printf("\naveragescore:%d\n",average(*a));}}}voidsearch2(int(*a)[5]){for(inti=1,s=0;i<=4;i++,a++){for(intj=0,t1=0,t2=0;j<5;j++){if(*(*a+j)>=85)t1++;if(*(*a+j)>=95)t2++;}if(t1>1||t2>0)printf("%dwins\n",i+1);}}
{m=a[j];a[j]=a[i];a[i]=m;}
printf("%d",a[i]);
}//又比老师的程序简单
冒泡法排序
inta[5];
inti,j,m,o;//定义o简化算法
for(i=0;i<4;i++)
o=0;
for(j=0;j<4-i;j++)
if(a[j]>a[j+1])
{m=a[j];a[j]=a[j+1];a[j+1]=m;o++;}
if(o==0)break;
7.求矩阵的转制
inta[2][3]={{1,2,3},{4,5,6}};
intb[3][2],i,j;
printf("arraya:
for(i=0;i<=1;i++)
for(j=0;j<=2;j++)
{printf("%5d",a[i][j]);
b[j][i]=a[i][j];
printf("arrayb:
for(i=0;i<=2;i++)
for(j=0;j<=1;j++)
printf("%5d",b[i][j]);
}//优化讲义上所得
8.用筛选法求10000以内所有素数
inti,j,n,a[10001];//范围可改
a[1]=0;
for(i=2;i<=sqrt(10000);i++)
for(j=2;j*i<=10000;j++)a[j*i]=0;
for(i=1,n=0;i<=10000;i++)
if(a[i]!
=0){printf("%6d",i);n++;}//用n计数,每十个换行
if(n==10){putchar('\n');n=0;}
9.输出杨辉三角
#defineN10//符号常量N为输出杨辉三角的行数,可一改全改
intmain()//上面的几个可改数字的程序也可以如此做,方便简单
inti,j,a[N+1][N+1];
for(i=1;i<=N;i++)
a[i][1]=1;
for(j=1;j<=N;j++)
a[i][j]=1;
for(i=3;i<=N;i++)
for(j=2;j
a[i][j]=a[i-1][j-1]+a[i-1][j];
for(j=1;j<=i;j++)
printf("%5d",a[i][j]);
10.得意之作!
打印2014年年历表。
inti,j,k,n=2;//i月份,j天数,k每月前空格,n列数计数
intm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//m数组记每月天数
printf("2014年年历表\n\n");
for(i=1;i<=12;i++)
switch(i)
case1:
printf("一月\n");break;
case2:
printf("二月\n");break;
case3:
printf("三月\n");break;
case4:
printf("四月\n");break;
case5:
printf("五月\n");break;
case6:
printf("六月\n");break;
case7:
printf("七月\n");break;
case8:
printf("八月\n");break;
case9:
printf("九月\n");break;
case10:
printf("十月\n");break;
case11:
printf("十一月\n");break;
case12:
printf("十二月\n");break;
printf("\n一二三四五六日\n");
for(k=1;k<=n;k++)printf("");
for(j=1;j<=m[i];j++)
printf("%3d",j);
n++;
if(n==7)
{putchar('\n');n=0;}
printf("\n\n");
11.hanoi移盘子问题,运用函数递归,难!
!
voidmove(charx,chary)
printf("%c-->%c\n",x,y);
voidhanoi(intn,chara,charb,charc)
if(n==1)move(a,c);
else
hanoi(n-1,a,c,b);
move(a,c);
hanoi(n-1,b,a,c);
intm;
printf("hanoi问题,将n个盘子从A借助B移动到C。
\n请输入盘子数n:
");
scanf("%d",&m);
printf("移动方法为:
hanoi(m,'A','B','C');
12.函数求最大公约数与最小公倍数
intY(inta,intb)//最大公约数用递归函数的辗转相除法求得
intc,d,x;
c=a/b,d=a%b;
if(d==0)x=b;
elsex=Y(b,d);
returnx;
intB(inta,intb)
return(a*b/Y(a,b));//两数乘积等于其最大公约数与最小公倍数相乘
inta,b;
printf("请输入两个整数:
scanf("%d%d",&a,&b);
printf("最大公约数为%d\n最小公倍数为%d\n",Y(a,b),B(a,b));
13,用函数求方程的根,判别式的情况都要考虑
voidroot(doublea,doubleb,doublec);
voidroot1(doublea,doubleb,doublec);
voidroot2(doublea,doubleb,doublec);
voidroot3();
doublea,b,c;
scanf("%lf%lf%lf",&a,&b,&c);
root(a,b,c);
voidroot(doublea,doubleb,doublec)
doublex=b*b-4*a*c;
if(x>0)root1(a,b,c);
elseif(x==0)root2(a,b,c);
elseroot3();
voidroot1(doublea,doubleb,doublec)
doublex=sqrt(b*b-4*a*c);
printf("Therootsis%f,%f\n",(-b-x)/(2*a),(-b+x)/(2*a));
voidroot2(doublea,doubleb,doublec)
printf("Therootis%f\n",-b/(2*a));
voidroot3()
printf("无实根\n");
}//并不高级,但是对于练习函数的用法有很大帮助
14.算法:
n个人围成一圈1,2,3报数,报到3退出,问最后剩下的人是原来的第几个?
(经过耗费脑细胞的优化算法,终于得到最简单的程序!
累死!
)
#definen10
inta[n+1],i,j;
for(i=1;i<=n;i++)a[i]=1;
for(i=1,j=0;j!
=3*n;i++)
if(i==n+1)i=1;
=0)j++;
if(j%3==0)a[i]=0;
printf("%d\n",i-1);
#definet3
=t*n;i++)
if((j%t==0)&&(a[i]!
=0)){a[i]=0;printf("%3d\n",i);}
15.关于四个学生五门成绩的一些处理(没什么的,只是它长,练习之用,留下以作纪念)。
P29215题
voidaverage1(int(*a)[5]);
voidsearch1(int(*a)[5]);
voidsearch2(int(*a)[5]);
inta[4][5]={{77,88,99,44,55},{65,54,98,78,23},{85,95,96,94,100},{45,100,100,100,15}};
for(int*p=*a,i=1;p<=*a+19;p++,i++)
printf("%4d",*p);
if(i%5==0)putchar('\n');
average1(a);
search1(a);
search2(a);
intaverage(int*p)
for(inti=1,s=0;i<=5;i++,p++)s=s+*p;
return(s/5);
voidaverage1(int(*a)[5])
for(inti=1,s=0;i<=4;i++,a++)s=s+**a;
printf("average=%d\n",s/5);
voidsearch1(int(*a)[5])
for(inti=1,s=0;i<=4;i++,a++)
for(intj=0,t=0;j<5;j++)if(*(*a+j)<60)t++;
if(t>1)
printf("%dfail.\nher/hisscoresis:
",i+1);
for(intj=0;j<5;j++)printf("%d",*(*a+j));
printf("\naveragescore:
%d\n",average(*a));
voidsearch2(int(*a)[5])
for(intj=0,t1=0,t2=0;j<5;j++)
if(*(*a+j)>=85)t1++;
if(*(*a+j)>=95)t2++;
if(t1>1||t2>0)printf("%dwins\n",i+1);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1