版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
PAGEPAGE110說明:1.程序編號(hào)的前2位為所在章節(jié),第3位數(shù)字為序號(hào)。2.可直接復(fù)制程序文本,“粘貼”到VC++6.0當(dāng)前文件,編譯運(yùn)行。3.該文本在排版時(shí)最好不改變字體,以免改變某些符號(hào)的屬性。第1章算法與程序設(shè)計(jì)概述//橫豎折對(duì)稱方陣,c131#include<stdio.h>//調(diào)用2個(gè)頭文件#include<math.h>voidmain(){inti,j,m,n,a[30][30];//定義數(shù)據(jù)結(jié)構(gòu)printf("請(qǐng)確定方陣階數(shù)(奇數(shù))n:");scanf("%d",&n);if(n%2==0){printf("請(qǐng)輸入奇數(shù)!");return;}m=(n+1)/2;for(i=1;i<=n;i++)for(j=1;j<=n;j++){ if(i+j<=n+1&&i<=j||i+j>=n+1&&i>=j) a[i][j]=m-abs(m-j);//方陣上、下部元素賦值if(i+j<n+1&&i>j||i+j>n+1&&i<j) a[i][j]=m-abs(m-i);//方陣左、右部元素賦值}printf("%d階對(duì)稱方陣為:\n",n);for(i=1;i<=n;i++){for(j=1;j<=n;j++)//輸出對(duì)稱方陣printf("%3d",a[i][j]);printf("\n");}}//斜折對(duì)稱方陣,c132#include<math.h>#include<stdio.h>voidmain(){inti,j,m,n,a[30][30];printf("請(qǐng)確定方陣階數(shù)(奇數(shù))n:");scanf("%d",&n);if(n%2==0){printf("請(qǐng)輸入奇數(shù)!");return;}m=(n+1)/2;for(i=1;i<=n;i++)for(j=1;j<=n;j++){if(i<=m&&j<=m||i>m&&j>m)a[i][j]=abs(i-j);//方陣左上部與右下部元素賦值 if(i<=m&&j>m||i>m&&j<=m)a[i][j]=abs(i+j-n-1);//方陣右上部與左下部元素賦值}printf("%d階對(duì)稱方陣為:\n",n);for(i=1;i<=n;i++){for(j=1;j<=n;j++)//輸出對(duì)稱方陣printf("%3d",a[i][j]);printf("\n");}}//求整數(shù)a,b的最大公約數(shù)(a,b),c133#include<stdio.h>voidmain(){longa,b,c,r;printf("請(qǐng)輸入整數(shù)a,b:");scanf("%ld,%ld",&a,&b); //輸入整數(shù)a,bprintf("(%ld,%ld)",a,b);if(a<b){c=a;a=b;b=c;} //交換a,b,確保a>br=a%b;while(r!=0){a=b;b=r; //實(shí)施"輾轉(zhuǎn)相除"r=a%b;}printf("=%ld\n",b); //輸出求解結(jié)果}//求n個(gè)整數(shù)的最大公約數(shù),c134#include<stdio.h>voidmain(){intk,n;longa,b,c,r,m[100];printf("請(qǐng)輸入整數(shù)個(gè)數(shù)n:"); //輸入原始數(shù)據(jù)scanf("%d",&n);printf("請(qǐng)依次輸入%d個(gè)整數(shù):",n);for(k=0;k<=n-1;k++){printf("\n請(qǐng)輸入第%d個(gè)整數(shù):",k+1);?scanf("%ld",&m[k]);}b=m[0];for(k=1;k<=n-1;k++) //控制應(yīng)用n?1次歐幾里德算法{a=m[k];if(a<b){c=a;a=b;b=c;} //交換a,b,確保a>br=a%b;while(r!=0){a=b;b=r; //實(shí)施"輾轉(zhuǎn)相除"r=a%b;}}printf("(%ld",m[0]); //輸出求解結(jié)果for(k=1;k<=n-1;k++)printf(",%ld",m[k]);printf(")=%ld\n",b);}//實(shí)現(xiàn)歐幾里德算法的函數(shù),c135longgcd(longa,longb){longc,r;if(a<b){c=a;a=b;b=c;}//交換a,b,確保a>br=a%b;while(r!=0){a=b;b=r;//實(shí)施"輾轉(zhuǎn)相除"r=a%b;}returnb;}//求n個(gè)整數(shù)的最大公約數(shù),c136#include<stdio.h>voidmain(){intk,n;longx,y,m[100];printf("請(qǐng)輸入整數(shù)個(gè)數(shù)n:");scanf("%d",&n);printf("請(qǐng)依次輸入%d個(gè)整數(shù):",n);for(k=0;k<=n-1;k++) {printf("\n請(qǐng)輸入第%d個(gè)整數(shù):",k+1);scanf("%ld",&m[k]);}x=m[0];for(k=1;k<=n-1;k++){y=m[k];x=gcd(x,y);}printf("(%ld",m[0]);for(k=1;k<=n-1;k++) printf(",%ld",m[k]);printf(")=%ld\n",x);}第2章枚舉//含數(shù)字m且不能被m整除的n位整數(shù)的個(gè)數(shù)統(tǒng)計(jì)與求和,c221#include<stdio.h>voidmain(){intc,j,m,n,f[10];longd,k,g1,g2,s1,s2,t;printf("請(qǐng)輸入一位整數(shù)m,n:");scanf("%d,%d",&m,&n);t=1;for(k=1;k<=n-1;k++) t=t*10;//求最小的n位整數(shù)tg1=0;s1=0;g2=0;s2=0;for(k=t;k<=10*t-1;k++)//枚舉每一個(gè)n位數(shù){d=k;for(j=0;j<=9;j++)f[j]=0;for(j=1;j<=n;j++) {c=d%10;f[c]+=1;//統(tǒng)計(jì)n位整數(shù)k中各數(shù)字的個(gè)數(shù) d=d/10; } if(f[m]>0&&k%m>0)//k含數(shù)字m且不能被m整除 {g1++;s1+=k;}if(f[m]==2&&k%m>0)//k恰含2個(gè)數(shù)字m且不能被m整除 {g2++;s2+=k;}}printf("含數(shù)字%d且不能被%d整除的%d位整數(shù)的個(gè)數(shù)g1=%ld\n",m,m,n,g1);printf("這些整數(shù)的和為s1=%ld\n",s1);printf("恰含2個(gè)數(shù)字%d且不能被%d整除的%d位整數(shù)個(gè)數(shù)g2=%ld\n",m,m,n,g2);printf("這些整數(shù)的和為s2=%ld\n",s2);}//求分母在[a,b]的最簡真分?jǐn)?shù)的個(gè)數(shù)及其和,c222#include<stdio.h>#include<math.h>voidmain(){inta,b,n,i,j,t,u;doubles;printf("最簡真分?jǐn)?shù)分母在[a,b]內(nèi),請(qǐng)確定a,b:");scanf("%d,%d",&a,&b);//輸入?yún)^(qū)間的上下限n=0;s=0;for(j=a;j<=b;j++)//枚舉分母for(i=1;i<=j-1;i++)//枚舉分子{for(t=0,u=2;u<=i;u++)//枚舉因數(shù)if(j%u==0&&i%u==0){t=1;break;//分子分母有公因數(shù)舍去}if(t==0){n++;//統(tǒng)計(jì)最簡真分?jǐn)?shù)個(gè)數(shù)s+=(double)i/j;//求最簡真分?jǐn)?shù)和}}printf("最簡真分?jǐn)?shù)個(gè)數(shù)n=%d\n",n);printf("其和s=%.5f\n",s);}//解PELL方程:x^2-ny^2=1,c231#include<math.h>#include<stdio.h>voidmain(){doublea,m,n,x,y;printf("解PELL方程:x^2-ny^2=1.\n");printf("請(qǐng)輸入非平方整數(shù)n:");scanf("%lf",&n);m=floor(sqrt(n+1));if(m*m==n){printf("n為平方數(shù),方程無正整數(shù)解!\n");return;}y=1;while(y<=10000000){y++;a=n*y*y;x=floor(sqrt(a+1));if(x*x==a+1)//檢測(cè)是否滿足方程{printf("方程x^2-%.0fy^2=1的基本解為:\n",n);printf("x=%.0f,y=%.0f\n",x,y);break;}}if(y>10000000)printf("未求出該方程的基本解!");}//基于最小的枚舉求精解超越方程,c232#include<stdio.h>#include<math.h>doublefny(doublex)//自定義函數(shù)fny,用來定義方程式{return2*pow(x,2)*pow(sin(x),7)+3*pow(x,0.5)*cos(x)-exp(x)/5;}voidmain(){intk,t;doublea,b,c,x,x1,y,mi;printf("求方程在[a,b]中的一個(gè)解,請(qǐng)確定a,b:");scanf("%lf,%lf",&a,&b);for(t=0,x=a;x<=b;x+=0.1)//按步長0.1初步掃描if(fny(x)*fny(b)<=0)//調(diào)用自定義函數(shù)fny(){t=1;break;}if(t==0){printf("無解!");return;}c=0.1;k=1;mi=100.0; while(k<=8)//設(shè)置8次求精循環(huán) {for(x=a;x<=b;x+=c) {y=fny(x); if(fabs(y)<mi)//比較求取最小值mi {mi=fabs(y);x1=x;} } c=c/10;a=x1-5*c;b=x1+5*c;//縮小循環(huán)步長求精 k++; }printf("所求方程的一個(gè)解為x=%.8f\n",x1);//輸出所求解}//符號(hào)判定枚舉求精解超越方程,c233#include<stdio.h>#include<math.h>doublefny(doublex)//自定義函數(shù)fny,用來定義方程式{return2*pow(x,2)*pow(sin(x),7)+3*pow(x,0.5)*cos(x)-exp(x)/5;}voidmain(){inti,t=0;doublea,b,x,x1,x2,c;printf("求方程在[a,b]中的一個(gè)解,請(qǐng)確定a,b:");scanf("%lf,%lf",&a,&b);for(x=a;x<=b;x+=0.1)//初步掃描if(fny(x)*fny(b)<=0)//調(diào)用自定義函數(shù)fny(){x1=x;x2=b;t=1;break;}if(t==0){printf("無解!");return;}c=0.01;for(i=2;i<=9;i++)//逐位求精{for(x=x1;x<=x2;x+=c)if(fny(x)*fny(x2)>0)//如果變?yōu)橥?hào),縮小循環(huán)范圍{x2=x;x1=x-c;break;}//調(diào)整循環(huán)的初值x1與終值x2c=c/10;//縮小循環(huán)步長求精}x=(x1+x2)/2;printf("所求方程的一個(gè)解為x=%.8f",x);//輸出解,小數(shù)點(diǎn)后6位}//解分?jǐn)?shù)不等式,c241#include<stdio.h>#include<math.h>voidmain(){longc,d,i,m1,m2;doubles;printf("請(qǐng)輸入正整數(shù)m1,m2(m1<m2):");scanf("%ld,%ld",&m1,&m2);i=0;s=0;while(s<=m1){i=i+1;s=s+sqrt(i)/(i+1);}c=i;do{i=i+1;s=s+sqrt(i)/(i+1);}while(s<=m2);d=i-1;printf("滿足不等式的正整數(shù)n為:%ld≤n≤%ld\n",c,d);}//解不等式:d<1+1/2-1/3+1/4+1/5-1/6+...±1/n,c242#include<stdio.h>voidmain(){longd,n,k;doubles;printf("請(qǐng)輸入正整數(shù)d:");scanf("%d",&d);printf("%d<1+1/2-1/3+1/4+1/5-1/6+…+-1/n的解:\n",d);n=1;s=0;while(1){s=s+1.0/n+1.0/(n+1)-1.0/(n+2);if(s>d)break; n=n+3;}printf("n>=%ld\n",n+1);//得一個(gè)區(qū)間解k=1;s=0;while(k<=n){if(k%3>0)s=s+1.0/k;elses=s-1.0/k;if(s>d)//得到離散解 printf("n=%ld\n",k);k++;}}//解不等式:d<1+1/2-1/3+1/4+1/5-1/6+...±1/n,c243#include<stdio.h>voidmain(){longd,n,k;doubles;printf("請(qǐng)輸入正整數(shù)d:");scanf("%d",&d);printf("%d<1+1/2-1/3+1/4+1/5-1/6+…+-1/n的解:\n",d);n=3;s=3.0/2;while(1){s=s-1.0/n+1.0/(n+1)+1.0/(n+2);if(s>d)break; n=n+3;}printf("n=%ld\n",n+2); //打印第一個(gè)離散解n+2k=n+2; while(1){if((s=s-1.0/(++k))>d)break;if((s=s+1.0/(++k))>d)break;s=s+1.0/(++k); printf("n=%ld\n",k); //打印離散解k}printf("n>=%ld\n",k); //打印區(qū)間解}//基于素?cái)?shù)的分?jǐn)?shù)和,c251#include<stdio.h>#include<math.h>voidmain(){intt,j,n,k,k1,k2,a[3000];doubles,s2,smax,mi;printf("請(qǐng)輸入整數(shù)n:");scanf("%d",&n);for(k=1;k<=n+1;k++)a[k]=0;for(k=2;k<=n+1;k++){for(t=0,j=3;j<=sqrt(2*k-1);j+=2)if((2*k-1)%j==0){t=1;break;}if(t==0)a[k]=1;//標(biāo)記第k個(gè)奇數(shù)2k-1為素?cái)?shù) }s=0;smax=0;mi=10;for(k=1;k<=n;k++){if(a[k]+a[k+1]==1)//判斷a[k]與a[k+1]中有一個(gè)素?cái)?shù)s+=(double)(2*k-1)/(2*k+1);//實(shí)施加 elses-=(double)(2*k-1)/(2*k+1);//否則,實(shí)施減 if(s>smax){smax=s;k1=k;}//比較求最大值smax if(fabs(s)<mi){mi=fabs(s);k2=k;s2=s;}//絕對(duì)值比較求最接近0點(diǎn) }printf("s(%d)=%.5f\n",n,s);printf("當(dāng)k=%d時(shí)s有最大值:%.5f\n",k1,smax);printf("當(dāng)k=%d時(shí)s=%.5f最接近0.\n",k2,s2);}//求[x,y]范圍內(nèi)整數(shù)的因數(shù)比最大值,c252#include<stdio.h>#include<math.h>voidmain(){doublea,s,a1,s1,b,k,t,x,y,max=0;printf("求區(qū)間[x,y]中整數(shù)的因數(shù)比最大值.");printf("請(qǐng)輸入整數(shù)x,y:");scanf("%lf,%lf",&x,&y);for(a=x;a<=y;a++)//枚舉區(qū)間內(nèi)的所有整數(shù)a{s=1;b=sqrt(a);for(k=2;k<=b;k++)//試商尋求a的因數(shù)kif(fmod(a,k)==0)s=s+k+a/k;//k與a/k是a的因數(shù),求和if(a==b*b)s=s-b;//如果a=b^2,去掉重復(fù)因數(shù)bt=s/a; if(max<t) {max=t;a1=a;s1=s;}}printf("整數(shù)%.0f的因數(shù)比最大:%.4f\n",a1,max);printf("%.0f的因數(shù)和為:\n",a1);printf("%.0f=1",s1);//輸出其因數(shù)和式for(k=2;k<=a1/2;k++)if(fmod(a1,k)==0)printf("+%.0f",k);}//雙和數(shù)組探索,c261#include<stdio.h>#include<math.h>voidmain(){inta,b,c,d,e,f,x,s;for(s=11;s<=100;s++){printf("s=%d:\n",s);x=0;for(a=1;a<=(s-3)/3;a++)for(b=a+1;b<=(s-a-1)/2;b++)for(d=a+1;d<=(s-3)/3;d++)for(e=d+1;e<=(s-d-1)/2;e++){c=s-a-b;f=s-d-e;if(a*b*c*(e*f+f*d+d*e)!=d*e*f*(b*c+c*a+a*b))continue;//排除倒數(shù)和不相等x++;printf("%3d:(%2d,%2d,%2d)",x,a,b,c);printf("(%2d,%2d,%2d)\n",d,e,f);}if(x==0)printf("無解!\n");}}//2x+3y按項(xiàng)的大小枚舉設(shè)計(jì),c262#include<stdio.h>voidmain(){intn,t,k,i,h,j,a[30000];printf("請(qǐng)輸入n:");scanf("%d",&n);a[1]=1;a[2]=2;t=2;k=2;while(t<n){k++;h=0;//枚舉k是否為A集項(xiàng)for(i=2;i<=t;i++){for(j=1;j<=i-1;j++)if(k==2*a[j]+3*a[i]||k==2*a[i]+3*a[j]){h=1;t++;a[t]=k;//若k為A集項(xiàng),給a[t]賦值break;}if(h==1)break;}}printf("a(%d)=%d",n,a[n]);}//逆序乘積式,c271#include<math.h>#include<stdio.h>voidmain(){inta,b,a1,b1,i,j,n,t,c[5];printf("逆序式:DE*FG=ED*GF\n");n=0;for(a=10;a<=98;a++)//枚舉二位數(shù)for(b=a+1;b<=99;b++){c[1]=a/10;c[2]=a%10;c[3]=b/10;c[4]=b%10;t=0;for(i=1;i<=3;i++)for(j=i+1;j<=4;j++)if(c[i]==c[j]){t=1;break;}if(t==1)continue;//存在相同數(shù)字時(shí)返回a1=c[2]*10+c[1];//產(chǎn)生逆序數(shù)b1=c[4]*10+c[3];if(a*b==a1*b1&&c[1]<c[2]&&c[1]<c[3]&&c[1]<c[4]){n=n+1;printf("%2d:%2d*%2d=%2d*%2d",n,a,b,a1,b1); if(n%2==0)printf("\n");}}}//完美綜合運(yùn)算式(1),c272#include<stdio.h>#include<math.h>voidmain(){doublea,b,c,d,e,z;intj,k,t,n,f[10];printf("□^□+□□/□□-□□*□=0\n");n=0;for(a=2;a<=9;a++)for(b=2;b<=9;b++)for(c=12;c<50;c++)for(z=2*c;z<=98;z=z+c)//各數(shù)實(shí)施枚舉,確保z為c的倍數(shù)for(d=12;d<=98;d++)for(e=2;e<=9;e++){if(pow(a,b)+z/c!=d*e)continue;//檢驗(yàn)等式是否成立t=0; f[0]=0;f[1]=a;f[2]=b;f[3]=e;//9個(gè)數(shù)字賦給f數(shù)組 f[4]=fmod(c,10);f[5]=floor(c/10); f[6]=fmod(d,10);f[7]=floor(d/10); f[8]=fmod(z,10);f[9]=floor(z/10);for(k=0;k<=8;k++) for(j=k+1;j<=9;j++)if(f[k]==f[j]){t=1;break;}//檢驗(yàn)數(shù)字是否有重復(fù)if(t==0){n++;//輸出一個(gè)解printf("%2d:%.0f^%.0f+%.0f/%.0f",n,a,b,z,c);printf("-%.0f*%.0f=0\n",d,e);}}}//完美綜合運(yùn)算式設(shè)計(jì)(2),c273#include<stdio.h>#include<math.h>voidmain(){inta,b,c,d,e,k,t,n,x,y,z,m[7],f[10];printf("□^□+□□/□□-□□*□=0\n");n=0;for(a=2;a<=9;a++)for(b=2;b<=9;b++)for(c=12;c<=98;c++)for(d=12;d<=98;d++)//實(shí)施枚舉for(e=2;e<=9;e++){for(t=1,k=1;k<=b;k++)t=t*a;//計(jì)算乘方a^bz=(d*e-t)*c; if(z<10||z>98)continue;m[1]=a;m[2]=b;m[3]=c;m[4]=d;m[5]=e;m[6]=z;for(x=0;x<=9;x++)f[x]=0;for(k=1;k<=6;k++){y=m[k];while(y>0){x=y%10;f[x]=f[x]+1;y=y/10;//分離數(shù)字f數(shù)組統(tǒng)計(jì)}}for(t=0,x=1;x<=9;x++)if(f[x]!=1){t=1;break;}//檢驗(yàn)數(shù)字1-9各出現(xiàn)一次if(t==0)//輸出一個(gè)解{n++;printf("%2d:%d^%d+%d/%d",n,a,b,z,c);printf("-%d*%d=0\n",d,e);}}}//指定幻和的三階素?cái)?shù)幻方,c281#include<math.h>#include<stdio.h>voidmain(){intc,d,j,k,n,t,t1,t2,s,w,x,y,z,m;inta[3000];printf("請(qǐng)確定素?cái)?shù)幻方的幻和s:");scanf("%d",&s);if(s%3>0){printf("不存在幻和為%d的素?cái)?shù)幻方!\n",s);return;}n=s/3;m=0;c=3;d=2*n-3;//確定幻和為s時(shí)素?cái)?shù)的最大最小區(qū)間for(k=c;k<=d;k++)a[k]=0;for(k=c;k<=d;k+=2){for(t=0,j=3;j<=sqrt(k);j+=2)if(k%j==0){t=1;break;}if(t==0)a[k]=1;//[c,d]中的奇數(shù)k為素?cái)?shù),標(biāo)注1}if(a[n]==0){printf("不存在幻和為%d的素?cái)?shù)幻方!\n",s);return;}for(y=2;y<=n-3;y+=2)for(x=y+2;x<=n-3;x+=2){z=x-y;w=x+y;if(x==2*y||n-w<c||n+w>d) continue;//控制幻方的素?cái)?shù)范圍t1=a[n-w]*a[n+w]*a[n-z]*a[n+z]; t2=a[n-x]*a[n+x]*a[n-y]*a[n+y];if(t1*t2==0)continue;//控制其余8個(gè)均為素?cái)?shù) m++;printf("NO%d:\n",m);//統(tǒng)計(jì)并輸出三階素?cái)?shù)幻方printf("%5d%5d%5d\n",n-x,n+w,n-y);printf("%5d%5d%5d\n",n+z,n,n-z);printf("%5d%5d%5d\n",n+y,n-w,n+x);}printf("\n共%d個(gè)素?cái)?shù)幻方.\n",m);}//和積三角形,c282#include<stdio.h>voidmain(){intk,j,t,s,s1,n,b[9];longs2;printf("請(qǐng)輸入s:");scanf("%d",&s); n=0;for(b[1]=1;b[1]<=(s-21)/2;b[1]++)for(b[7]=b[1]+1;b[7]<=s-28;b[7]++)for(b[4]=1;b[4]<=s-28;b[4]++){if((s+b[1]+b[4]+b[7])%3!=0)continue;s1=(s+b[1]+b[4]+b[7])/3;for(b[3]=(s1-b[1]-b[4])/2+1;b[3]<s1-b[1]-b[4];b[3]++)for(b[5]=(s1-b[4]-b[7])/2+1;b[5]<s1-b[4]-b[7];b[5]++) {b[2]=s1-b[1]-b[4]-b[3];b[6]=s1-b[4]-b[7]-b[5]; b[8]=s1-b[1]-b[7]; t=0; for(k=1;k<=7;k++) for(j=k+1;j<=8;j++) if(b[k]==b[j]){t=1;k=7;break;}if(t==1)continue; s2=b[1]*b[2]*b[3]*b[4]; if(b[4]*b[5]*b[6]*b[7]!=s2||b[1]*b[8]*b[7]!=s2)continue; n++;printf("%3d:%2d",n,b[1]); for(k=2;k<=8;k++) printf(",%2d",b[k]); printf("s1=%d,s2=%ld\n",s1,s2); }}printf("共%d個(gè)解。\n",n);}第3章遞推//擺動(dòng)數(shù)列,c321#include<stdio.h>voidmain(){inti,n,max,a[10000];printf("請(qǐng)確定項(xiàng)數(shù)n:");scanf("%d",&n);a[1]=1;max=0;for(i=2;i<=n;i++){if(i%2==0)a[i]=a[i/2]+1;elsea[i]=a[(i-1)/2]+a[(i+1)/2];if(a[i]>max)max=a[i];}printf("a(%d)=%d\n",n,a[n]);printf("擺動(dòng)數(shù)列前%d項(xiàng)中最大項(xiàng)有:",n);for(i=2;i<=n;i++)if(a[i]==max)printf("a(%d)=",i);printf("%d\n",max);}//分?jǐn)?shù)遞推數(shù)列,c322#include<stdio.h>voidmain(){intn,i,k,t,j,kmax;longc[3001],d[3001];printf("請(qǐng)輸入整數(shù)n(1--3000):");//鍵盤輸入確定整數(shù)nscanf("%d",&n);c[1]=1;d[1]=2;c[2]=3;d[2]=5;kmax=1;//數(shù)組與最大項(xiàng)序號(hào)賦初值for(i=3;i<=n;i++){for(k=c[i-1]+1;k<d[i-1];k++){t=0;//k枚舉探求第i項(xiàng)分子cfor(j=1;j<i-1;j++)if(k==d[j]){t=1;break;}if(t==0){c[i]=k;d[i]=k+i;//第i項(xiàng)分子c,分母d賦值break;}}if(c[i]*d[kmax]>c[kmax]*d[i])kmax=i;//比較得最大項(xiàng)的序號(hào)kmax}printf("數(shù)列第%d項(xiàng)為:%ld/%ld。\n",n,c[n],d[n]);printf("數(shù)列前%d項(xiàng)中最大項(xiàng)為",n);for(i=1;i<=n;i++)//檢查可能有多個(gè)最大項(xiàng)if(c[i]*d[kmax]==c[kmax]*d[i])printf("第%d項(xiàng):%ld/%ld。\n",i,c[i],d[i]);}//雙冪序列求解,c331#include<stdio.h>voidmain(){intk,n,t,p2,p3;doublea,b,s,f[100];printf("求數(shù)列的第n項(xiàng)與前n項(xiàng)和,請(qǐng)輸入n:");scanf("%d",&n);f[1]=1;p2=0;p3=0;a=2;b=3;s=1;for(k=2;k<=n;k++){if(a<b){f[k]=a;a=a*2; //用2的冪給f[k]賦值t=2;p2++; //t=2表示2的冪,p2為指數(shù)}else{f[k]=b;b=b*3; //用3的冪給f[k]賦值t=3;p3++; //t=3表示3的冪,p3為指數(shù)}s+=f[k];}printf("數(shù)列的第%d項(xiàng)為:%.0f",n,f[n]);if(t==2) //對(duì)輸出項(xiàng)進(jìn)行標(biāo)注printf("(2^%d)\n",p2);elseprintf("(3^%d)\n",p3);printf("數(shù)列的前%d項(xiàng)之和為:%.0f\n",n,s);}//冪序列2^x*3^y枚舉求解,c332#include<stdio.h>voidmain(){intk,m;longa,j,n,f[1000];printf("計(jì)算不大于n的項(xiàng)數(shù),請(qǐng)指定n:");scanf("%ld",&n);printf("輸出序列的第m項(xiàng),請(qǐng)指定m:");scanf("%d",&m);f[1]=1;f[2]=2;k=2;for(a=3;a<=n;a++){j=a;while(j%2==0)j=j/2;//反復(fù)用2試商while(j%3==0)j=j/3;//反復(fù)用3試商if(j==1){k++;f[k]=a;}//用a給f[k]賦值}printf("冪序列中不大于%ld的項(xiàng)數(shù)為:%d\n",n,k);if(m<=k)printf("從小到大排序的第%d項(xiàng)為:%ld\n",m,f[m]);elseprintf("所輸序號(hào)m大于序列的項(xiàng)數(shù)!\n");}//遞推排序求解,c333#include<stdio.h>voidmain(){inti,j,h,k,m,u,c[100];doubled,n,t,f[1000];printf("計(jì)算小于n的項(xiàng)數(shù),請(qǐng)指定n:");scanf("%lf",&n);printf("輸出序列的第m項(xiàng),請(qǐng)指定m:");scanf("%d",&m);k=1;t=1.0;i=1;c[0]=1;f[1]=1.0;while(1){u=0; for(j=0;j<=i-1;j++){h=c[i-1]+j; if(f[h]*2<n&&f[h]>0)//第i行各項(xiàng)為前一行各項(xiàng)乘2{k++;f[k]=f[h]*2;u=1; if(j==0)c[i]=k;//該行的第1項(xiàng)的項(xiàng)數(shù)值賦給c(i)} elsebreak; }t=t*3;//最后一項(xiàng)為3的冪if(t<n&&t>0){k++;f[k]=t;} //用t給f[k]賦值 if(u==0)break;i++;}for(i=1;i<k;i++)//逐項(xiàng)比較排序 for(j=i+1;j<=k;j++) if(f[i]>f[j]) {d=f[i];f[i]=f[j];f[j]=d;} printf("冪序列中小于%f的項(xiàng)數(shù)為:%d\n",n,k);if(m<=k)printf("從小到大排序的第%d項(xiàng)為:%.0f\n",m,f[m]);elseprintf("所輸入的m大于序列的項(xiàng)數(shù)!\n");}//求3^i*2^j<n的項(xiàng)數(shù)及從小到大第m項(xiàng),c334#include<stdio.h>voidmain(){inti,m,u,p;doublen,h,f[1000],q[100];printf("計(jì)算小于n的項(xiàng)數(shù),請(qǐng)指定n:");scanf("%lf",&n);printf("輸出序列的第m項(xiàng),請(qǐng)指定m:");scanf("%d",&m);u=1;f[u]=1.0;p=0;q[p]=1.0;while(1){if(q[p]<=f[u]){p++;q[p]=3*q[p-1];}h=q[p];//遞推3的冪,q[p]=3^pfor(i=0;i<p;i++){if(q[i]<=f[u])q[i]*=2;//冪積q[i]=2^j*3^i,j=1,2,3,… if(q[i]<h)h=q[i];} if(h>n)break;u++;f[u]=h;}printf("冪序列中小于%.0f的項(xiàng)數(shù)為:%d\n",n,u);if(m<=u)printf("從小到大排序的第%d項(xiàng)為:%.0f\n",m,f[m]);elseprintf("所輸入的m大于序列的項(xiàng)數(shù)!\n");}//楊輝三角,c341#include<stdio.h>voidmain(){intn,i,j,k,a[20][20];printf("請(qǐng)輸入行數(shù)n:");scanf("%d",&n);for(i=1;i<=n;i++){a[i][1]=1;a[i][i]=1;}//確定初始條件for(i=3;i<=n;i++)for(j=2;j<=i-1;j++)//遞推實(shí)施a[i][j]=a[i-1][j-1]+a[i-1][j];for(i=1;i<=n;i++)//控制輸出n行{for(k=1;k<=40-3*i;k++)printf("");//控制輸出第i行的前導(dǎo)空格for(j=1;j<=i;j++)printf("%6d",a[i][j]);//控制輸出第i行的i個(gè)元素printf("\n");}}//應(yīng)用變量迭代求解,c342#include<stdio.h>voidmain(){intm,n,cnm,k;printf("請(qǐng)輸入行數(shù)n:");scanf("%d",&n);for(k=1;k<=40;k++)printf("");printf("%6d\n",1);//輸出第1行的"1"for(m=1;m<=n-1;m++){for(k=1;k<=40-3*m;k++)printf("");cnm=1;printf("%6d",cnm);//輸出每行開始的"1"for(k=1;k<=m;k++){cnm=cnm*(m-k+1)/k;//計(jì)算第m行的第k個(gè)數(shù)printf("%6d",cnm);}printf("\n");}}//兩種折疊方陣,c343#include<stdio.h>voidmain(){inti,m,n,t,x,y,z[20][20];printf("1:間斷型2:回轉(zhuǎn)型,請(qǐng)選擇:");scanf("%d",&t);printf("m階折疊方陣,請(qǐng)確定m:");scanf("%d",&m);n=1;z[1][1]=n;//n與數(shù)組z賦初值for(i=2;i<=m;i++)//方陣共m層if(t==1||t==2&&i%2==0){x=1;y=i;n++;//間斷型,回轉(zhuǎn)型的偶數(shù)層賦值z(mì)[x][y]=n;while(x<i)z[++x][y]=++n;while(y>1)z[x][--y]=++n;}else//回轉(zhuǎn)型的奇數(shù)層賦值{x=i;y=1;n++;z[x][y]=n;while(y<i)z[x][++y]=++n;while(x>1)z[--x][y]=++n; }if(t==1)printf("%d階間斷折疊方陣:\n",m);elseprintf("%d階回轉(zhuǎn)折疊方陣:\n",m);for(x=1;x<=m;x++)//打印m階折疊方陣{for(y=1;y<=m;y++)printf("%4d",z[x][y]);printf("\n");}}//整數(shù)s劃分展示,c351#include<stdio.h>voidmain(){ints,i,j,k,t,u;staticinta[21][800][21];printf("inputs(s<=20):");scanf("%d",&s);a[2][1][1]=1;a[2][1][2]=1;a[2][2][1]=2;u=2;for(k=3;k<=s;k++){for(j=1;j<=u;j++){a[k][j][1]=1;for(t=2;t<=k;t++) //實(shí)施在k-1所有劃分式前加1操作a[k][j][t]=a[k-1][j][t-1]; }for(i=u,j=1;j<=u;j++)if(a[k-1][j][1]<a[k-1][j][2]) //若k-1劃分式第1項(xiàng)小于第2項(xiàng){i++; //第1項(xiàng)加1為k的第i個(gè)劃分式的第1項(xiàng)a[k][i][1]=a[k-1][j][1]+1;for(t=2;t<=k-1;t++)a[k][i][t]=a[k-1][j][t];}i++;a[k][i][1]=k; //k的最后一個(gè)劃分式為:k=ku=i;}for(j=1;j<=u;j++) //輸出s的所有劃分式{printf("%3d:%d=%d",j,s,a[s][j][1]);i=2;while(a[s][j][i]>0){printf("+%d",a[s][j][i]);i++;}printf("\n");}}//整數(shù)s劃分優(yōu)化遞推設(shè)計(jì),c352#include<stdio.h>voidmain(){ints,i,j,k,t,u;staticinta[1600][25];printf("inputs(s<=24):");scanf("%d",&s);a[1][1]=1;a[1][2]=1;a[2][1]=2;u=2;for(k=3;k<=s;k++){for(j=1;j<=u;j++){i=k-1;for(t=i;t>=1;t--) //實(shí)施在k-1所有劃分式前加1操作a[j][t+1]=a[j][t];a[j][1]=1;}for(t=u,j=1;j<=u;j++)if(a[j][2]<a[j][3]) //若k-1劃分式第1項(xiàng)小于第2項(xiàng){t++;a[t][1]=a[j][2]+1; //第1項(xiàng)加1i=3;while(a[j][i]>0){a[t][i-1]=a[j][i];i++;}}t++;a[t][1]=k; //最后一個(gè)劃分式為:k=ku=t;}for(j=1;j<=u;j++) //輸出所有u個(gè)劃分式{printf("%3d:%d=%d",j,s,a[j][1]);i=2;while(a[j][i]>0){printf("+%d",a[j][i]);i++;}printf("\n");}}//迭代求解5個(gè)水手分椰子,c361#include<math.h>#include<stdio.h>voidmain(){inti;doublek,x,y;i=1;k=1.0;y=k;while(i<=5){i++;y=(4*y-1)/5;//遞推求后一個(gè)水手時(shí)的椰子yif(y!=floor(y)) {k=k+1.0;y=k;i=1;}//若y不是整數(shù),k增1從頭重試 }x=5*k+1;printf("%d個(gè)水手分椰子,原有椰子至少有:%6.0f個(gè).\n",5,x);}//遞推求解5個(gè)水手分椰子,c362#include<math.h>#include<stdio.h>voidmain(){inti;doublek,x,y[7];i=1;k=1.0;y[1]=k;while(i<=5){i++;y[i]=(4*y[i-1]-1)/5;//遞推求后一個(gè)水手時(shí)的椰子y(i)if(y[i]!=floor(y[i])) {k=k+1.0;y[1]=k;i=1;}//若y(i)不是整數(shù),k增1重試 }x=5*y[1]+1;printf("%d個(gè)水手分椰子,原有椰子至少有:%6.0f個(gè).\n",5,x);}//5個(gè)水手分椰子,從后往前遞推,c363#include<math.h>#include<stdio.h>voidmain(){inti;doublek,x,y[7];i=6;k=3.0;y[6]=k;while(i>1){i--;y[i]=(y[i+1]*5+1)/4;//遞推求前一個(gè)水手時(shí)的椰子if(y[i]!=floor(y[i])){k=k+4.0;y[6]=k;i=6;}//若y(i)不是整數(shù),k增4重試}x=5*y[1]+1;printf("原有椰子至少為:%5.0f個(gè).\n",x);for(i=1;i<=5;i++){printf("第%2d個(gè)水手面臨椰子:%5.0f=5*%5.0f+1個(gè),",i,5*y[i]+1,y[i]);printf("藏%5.0f個(gè).\n",y[i]);}printf("最后一起分時(shí)有椰子:%5.0f=5*%5.0f+1個(gè).",5*y[6]+1,y[6]);printf("每人分得%5.0f個(gè).\n",y[6]);}//n個(gè)水手分椰子,每次給猴子m個(gè)椰子,c364#include<math.h>#include<stdio.h>voidmain(){inti,m,n;doublek,x,y[20];printf("請(qǐng)輸入人數(shù)n(1<n<9):");scanf("%d",&n);printf("請(qǐng)輸入每次所剩椰子數(shù)m(0<m<n):");scanf("%d",&m);i=n+1;k=n-m-1;y[n+1]=k;while(i>1){i--;y[i]=(y[i+1]*n+m)/(n-1);//遞推求前一個(gè)水手時(shí)的椰子if(y[i]!=floor(y[i])){k=k+n-1;y[n+1]=k;i=n+1;}//若y(i)不是整數(shù),k增n-1重試}x=n*y[1]+m;printf("原有椰子至少為:%8.0f個(gè).\n",x);for(i=1;i<=n;i++){printf("第%2d個(gè)水手面臨椰子:",i);printf("%8.0f=%d*%8.0f+%d個(gè),",n*y[i]+m,n,y[i],m);printf("藏%8.0f個(gè).\n",y[i]);}printf("最后一起分時(shí)有椰子:");printf("%8.0f=%d*%8.0f+%d個(gè).",n*y[n+1]+m,n,y[n+1],m);printf("每人分得%8.0f個(gè).\n",y[n+1]);}//猴子爬山n級(jí),一步跨1級(jí)或3級(jí)臺(tái)階,c371#include<stdio.h>voidmain(){intk,n;longf[1000];printf("請(qǐng)輸入臺(tái)階總數(shù)n:");scanf("%d",&n);f[1]=1;f[2]=1;f[3]=2;//數(shù)組元素賦初值for(k=4;k<=n;k++)f[k]=f[k-1]+f[k-3];//按遞推關(guān)系實(shí)施遞推printf("s=%ld",f[n]);}//分級(jí)遞推,c372#include<stdio.h>voidmain(){inti,j,k,m,n,t,x[10];?longf[200];?printf("請(qǐng)輸入總臺(tái)階數(shù):");?scanf("%d",&n);//輸入臺(tái)階數(shù)?printf("一次有幾種跳法:");?scanf("%d",&m);?printf("請(qǐng)從小到大輸入一步跳幾級(jí)。\n");?for(i=1;i<=m;i++) //輸入m個(gè)一步跳級(jí)數(shù){printf("第%d個(gè)一步可跳級(jí)數(shù):",i);?scanf("%d",&x[i]);}?for(i=1;i<=x[1]-1;i++)f[i]=0; //確定初始條件?x[m+1]=n;f[x[1]]=1;?for(k=1;k<=m;k++)for(t=x[k]+1;t<=x[k+1];t++){f[t]=0;for(j=1;j<=k;j++) //按公式累加實(shí)現(xiàn)分級(jí)f[t]=f[t]+f[t-x[j]];if(t==x[k+1]) //t=x(k+1)時(shí)增1f[t]=f[t]+1;}?printf("共有不同的跳法種數(shù)為:");?printf("%d(%d",n,x[1]); //按指定格式輸出結(jié)果?for(i=2;i<=m;i++)printf(",%d",x[i]);?printf(")=%ld.\n",f[n]-1);?}第4章遞歸//輸出阿克曼函數(shù)的(m≤3,n≤10)的值,C411inta(intm,intn){if(m==0)returnn+1;elseif(n==0)returna(m-1,1);elsereturna(m-1,a(m,n-1));}#include<stdio.h>voidmain(){intm,n;printf("a(m,n)");for(n=0;n<=10;n++) printf("n=%1d",n);printf("\n");for(m=0;m<=3;m++){printf("m=%d",m); for(n=0;n<=10;n++)printf("%5d",a(m,n));printf("\n");}printf("\n");}//購票排隊(duì),c421longf(intj,inti){longy;if(i==0)y=1;elseif(j<i)y=0;//確定初始條件elsey=f(j-1,i)+f(j,i-1);//實(shí)施遞歸return(y);}#include<stdio.h>voidmain(){intm,n;printf("inputm,n:");scanf("%d,%d",&m,&n);printf("f(%d,%d)=%ld.\n",m,n,f(m,n));}//購票排隊(duì),c422#include<stdio.h>voidmain(){intm,n,i,j;longf[100][100];printf("inputm,n:");scanf("%d,%d",&m,&n);for(j=1;j<=m;j++)f[j][0]=1;for(j=0;j<=m;j++)//確定初始條件for(i=j+1;i<=n;i++)f[j][i]=0;for(i=1;i<=n;i++)for(j=i;j<=m;j++)f[j][i]=f[j-1][i]+f[j][i-1];//實(shí)施遞推printf("f(%d,%d)=%ld.\n",m,n,f[m][n]);}//漢諾塔n盤移動(dòng)次數(shù),c431#include<stdio.h>voidmain(){doubleg(intm);intn;printf("請(qǐng)輸入盤片數(shù)n:");scanf("%d",&n);if(n<=40)printf("%d盤的移動(dòng)次數(shù)為:%.0f\n",n,g(n));elseprintf("%d盤的移動(dòng)次數(shù)為:%.4e\n",n,g(n));}//求移動(dòng)次數(shù)的遞歸函數(shù)doubleg(intm){doubles;if(m==1)//確定初始條件s=1;else s=2*g(m-1)+1;returns;}//展示漢諾塔移動(dòng)過程的遞歸設(shè)計(jì),c432#include<stdio.h>intk=0;voidmv(charx,chary)//輸出函數(shù){printf("%c-->%c",x,y);k++;//統(tǒng)計(jì)移動(dòng)次數(shù)if(k%5==0)printf("\n");}voidhn(intm,chara,charb,charc)//遞歸函數(shù){if(m==1)mv(a,c);else{hn(m-1,a,c,b);mv(a,c);hn(m-1,b,a,c);}}voidmain()//主函數(shù){intn;printf("\ninputn:");scanf("%d",&n);hn(n,'A','B','C');printf("\nk=%d\n",k);//輸出移動(dòng)次數(shù)}//雙轉(zhuǎn)向旋轉(zhuǎn)方陣遞歸設(shè)計(jì),c441#include<stdio.h>intn,a[20][20]={0};voidmain(){inth,v,b,p,s,d;printf("請(qǐng)選擇方陣階數(shù)n:");scanf("%d",&n);printf("請(qǐng)選擇轉(zhuǎn)向,順轉(zhuǎn)1,逆轉(zhuǎn)2:");scanf("%d",&p);b=1;s=n;d=1;voidt(intb,ints,intd);//遞歸函數(shù)說明t(b,s,d);if(p==1)//按要求輸出旋轉(zhuǎn)方陣printf("%d階順轉(zhuǎn)方陣:\n",n);elseprintf("%d階逆轉(zhuǎn)方陣:\n",n);for(h=1;h<=n;h++){for(v=1;v<=n;v++) if(p==1) printf("%3d",a[h][v]); elseprintf("%3d",a[v][h]);printf("\n");}return;}voidt(intb,ints,intd)//定義遞歸函數(shù){intj,h=b,v=b;if(s==0)return;//s=0,1時(shí)為遞歸出口if(s==1){a[b][b]=d;return;}for(j=1;j<s;j++)//一圈的上行從左至右遞增{a[h][v]=d;v++;d++;}for(j=1;j<s;j++)//一圈的右列從上至下遞增{a[h][v]=d;h++;d++;}for(j=1;j<s;j++)//一圈的下行從右至左遞增{a[h][v]=d;v--;d++;}for(j=1;j<s;j++)//一圈的左行從下至上遞增{a[h][v]=d;h--;d++;}t(b+1,s-2,d);//調(diào)用內(nèi)圈遞歸函數(shù)}//m×n順轉(zhuǎn)矩陣遞歸設(shè)計(jì),c442#include<stdio.h>intm,n,a[20][20]={0};voidmain(){inth,v,b,s,d;printf("數(shù)陣為m行n列,請(qǐng)確定m,n:");scanf("%d,%d",&m,&n);s=m>n?n:m;b=1;d=1;voidt(intb,ints,intd);//遞歸函數(shù)說明t(b,s,d);//調(diào)用遞歸函數(shù)printf("%d×%d順轉(zhuǎn)矩陣:\n",m,n);for(h=1;h<=m;h++){for(v=1;v<=n;v++) printf("%3d",a[h][v]);printf("\n");}return;}voidt(intb,ints,intd)//定義遞歸函數(shù){intj,h=b,v=b;if(s<=0)return;//遞歸出口if(s=
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年度外墻仿石漆節(jié)能環(huán)保產(chǎn)品推廣合同2篇
- 2024版合同主體變更及風(fēng)險(xiǎn)評(píng)估協(xié)議3篇
- 2024年服裝店門面店面裝修合同3篇
- 2024版公司與快遞鳥深度合作服務(wù)合同3篇
- 2024版施工單位與施工質(zhì)量管理人員勞動(dòng)合同模板3篇
- 2024年校園道路照明合同3篇
- 2024年度體育場(chǎng)館EPC總承包施工合同3篇
- 2024年度股權(quán)轉(zhuǎn)讓合同:股東間關(guān)于股權(quán)轉(zhuǎn)讓的價(jià)格、條件和程序規(guī)定2篇
- 2024年度土建瓦工勞務(wù)分包合同模板:綠色建筑與智慧工地3篇
- 2024年度電力設(shè)備安裝合同:變壓器安裝工程
- 家居風(fēng)格分類說明PPT講座
- 高標(biāo)準(zhǔn)農(nóng)田施工合同
- GB/T 35833-2018廚房油污清潔劑
- GB/T 27800-2011靜密封橡膠制品使用壽命的快速預(yù)測(cè)方法
- 醫(yī)學(xué)人工智能原理及實(shí)踐
- 中學(xué)生法制教育主題班會(huì)課件《預(yù)防未成年人犯罪》
- 雙氧水工藝培訓(xùn)課件
- GB∕T 41550-2022 畜禽屠宰用脫毛劑使用規(guī)范
- 國家開放大學(xué)《公共政策概論》形考任務(wù)1參考答案
- 部編版六年級(jí)語文上冊(cè)第23課《月光曲》導(dǎo)學(xué)案
- 中醫(yī)基礎(chǔ)理論題庫4
評(píng)論
0/150
提交評(píng)論