10个经典的C语言面试基础算法及代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

10个经典的C语言面试基础算法及代码算法是一个程序和软件的灵魂,作为一名优秀的程序员,只有对一些基础的算法有着全面的掌握,才会在设计程序和编写代码的过程中显得得心应手。本文是近百个C语言算法系列的第二篇,包括了经典的Fibonacci数列、简易计算器、回文检查、质数检查等算法。也许他们能在你的毕业设计或者面试中派上用场。1、计算Fibonacci数列Fibonacci数列又称斐波那契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21。C语言实现的代码如下:/*DisplayingFibonaccisequenceuptonthtermwherenisenteredbyuser.*/#includestdio.hintmain(){intcount,n,t1=0,t2=1,display=0;printf(Enternumberofterms:);scanf(%d,&n);printf(FibonacciSeries:%d+%d+,t1,t2);/*Displayingfirsttwoterms*/count=2;/*count=2becausefirsttwotermsarealreadydisplayed.*/while(countn){display=t1+t2;t1=t2;t2=display;++count;printf(%d+,display);}return0;}结果输出:Enternumberofterms:10FibonacciSeries:0+1+1+2+3+5+8+13+21+34+也可以使用下面的源代码:/*DisplayingFibonacciseriesuptocertainnumberenteredbyuser.*/#includestdio.hintmain(){intt1=0,t2=1,display=0,num;printf(Enteraninteger:);scanf(%d,&num);printf(FibonacciSeries:%d+%d+,t1,t2);/*Displayingfirsttwoterms*/display=t1+t2;while(displaynum){printf(%d+,display);t1=t2;t2=display;display=t1+t2;}return0;}结果输出:Enteraninteger:200FibonacciSeries:0+1+1+2+3+5+8+13+21+34+55+89+144+2、回文检查源代码:/*Cprogramtocheckwhetheranumberispalindromeornot*/#includestdio.hintmain(){intn,reverse=0,rem,temp;printf(Enteraninteger:);scanf(%d,&n);temp=n;while(temp!=0){rem=temp%10;reverse=reverse*10+rem;temp/=10;}/*Checkingifnumberenteredbyuserandit'sreversenumberisequal.*/if(reverse==n)printf(%disapalindrome.,n);elseprintf(%disnotapalindrome.,n);return0;}结果输出:Enteraninteger:1232112321isapalindrome.3、质数检查注:1既不是质数也不是合数。源代码:/*Cprogramtocheckwhetheranumberisprimeornot.*/#includestdio.hintmain(){intn,i,flag=0;printf(Enterapositiveinteger:);scanf(%d,&n);for(i=2;i=n/2;++i){if(n%i==0){flag=1;break;}}if(flag==0)printf(%disaprimenumber.,n);elseprintf(%disnotaprimenumber.,n);return0;}结果输出:Enterapositiveinteger:2929isaprimenumber.4、打印金字塔和三角形使用*建立三角形***************源代码:#includestdio.hintmain(){inti,j,rows;printf(Enterthenumberofrows:);scanf(%d,&rows);for(i=1;i=rows;++i){for(j=1;j=i;++j){printf(*);}printf(\n);}return0;}如下图所示使用数字打印半金字塔。112123123412345源代码:#includestdio.hintmain(){inti,j,rows;printf(Enterthenumberofrows:);scanf(%d,&rows);for(i=1;i=rows;++i){for(j=1;j=i;++j){printf(%d,j);}printf(\n);}return0;}用*打印半金字塔***************源代码:#includestdio.hintmain(){inti,j,rows;printf(Enterthenumberofrows:);scanf(%d,&rows);for(i=rows;i=1;--i){for(j=1;j=i;++j){printf(*);}printf(\n);}return0;}用*打印金字塔*************************源代码:#includestdio.hintmain(){inti,space,rows,k=0;printf(Enterthenumberofrows:);scanf(%d,&rows);for(i=1;i=rows;++i){for(space=1;space=rows-i;++space){printf();}while(k!=2*i-1){printf(*);++k;}k=0;printf(\n);}return0;}用*打印倒金字塔*************************源代码:#includestdio.hintmain(){introws,i,j,space;printf(Enternumberofrows:);scanf(%d,&rows);for(i=rows;i=1;--i){for(space=0;spacerows-i;++space)printf();for(j=i;j=2*i-1;++j)printf(*);for(j=0;ji-1;++j)printf(*);printf(\n);}return0;}5、简单的加减乘除计算器源代码:/*Sourcecodetocreateasimplecalculatorforaddition,subtraction,multiplicationanddivisionusingswitch...casestatementinCprogramming.*/#includestdio.hintmain(){charo;floatnum1,num2;printf(Enteroperatoreither+or-or*ordivide:);scanf(%c,&o);printf(Entertwooperands:);scanf(%f%f,&num1,&num2);switch(o){case'+':printf(%.1f+%.1f=%.1f,num1,num2,num1+num2);break;case'-':printf(%.1f-%.1f=%.1f,num1,num2,num1-num2);break;case'*':printf(%.1f*%.1f=%.1f,num1,num2,num1*num2);break;case'/':printf(%.1f/%.1f=%.1f,num1,num2,num1/num2);break;default:/*Ifoperatorisotherthan+,-,*or/,errormessageisshown*/printf(Error!operatorisnotcorrect);break;}return0;}结果输出:Enteroperatoreither+or-or*ordivide:-Entertwooperands:3.48.43.4-8.4=-5.06、检查一个数能不能表示成两个质数之和源代码:#includestdio.hintprime(intn);intmain(){intn,i,flag=0;printf(Enterapositiveinteger:);scanf(%d,&n);for(i=2;i=n/2;++i){if(prime(i)!=0){if(prime(n-i)!=0){printf(%d=%d+%d\n,n,i,n-i);flag=1;}}}if(flag==0)printf(%dcan'tbeexpressedassumoftwoprimenumbers.,n);return0;}intprime(intn)/*Functiontocheckprimenumber*/{inti,flag=1;for(i=2;i=n/2;++i)if(n%i==0)flag=0;returnflag;}结果输出:Enterapositiveinteger:3434=3+3134=5+2934=11+2334=17+177、用递归的方式颠倒字符串源代码:/*Exampletoreverseasentenceenteredbyuserwithoutusingstrings.*/#includestdio.hvoidReverse();intmain(){printf(Enterasentence:);Reverse();return0;}voidReverse(){charc;scanf(%c,&c);if(c!='\n'){Reverse();printf(%c,c);}}结果输出:Enterasentence:margorpemosewaawesomeprogram8、实现二进制与十进制之间的相互转换/*Cprogrammingsourcecodetoconverteitherbinarytodecimalordecimaltobinaryaccordingtodataenteredbyuser.*/#includestdio.h#includemath.hintbinary_decimal(intn);intdecimal_binary(intn);intmain(){intn;charc;printf(Instructions:\n);printf(1.Enteralphabet'd'toconvertbinarytodecimal.\n);printf(2.Enteralphabet'b'toconvertdecimaltobinary.\n);scanf(%c,&c);if(c=='d'||c=='D'){printf(Enterabinarynumber:);scanf(%d,&n);printf(%dinbinary=%dindecimal,n,binary_decimal(n));}if(c=='b'||c=='B'){printf(Enteradecimalnumber:);scanf(%d,&n);printf(%dindecimal=%dinbinary,n,decimal_binary(n));}return0;}intdecimal_binary(intn)/*Functiontoconvertd

1 / 23
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功