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

上传人:b****3 文档编号:4644575 上传时间:2023-05-07 格式:DOCX 页数:20 大小:242.79KB
下载 相关 举报
10个经典的C语言面试基础算法及代码.docx_第1页
第1页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第2页
第2页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第3页
第3页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第4页
第4页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第5页
第5页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第6页
第6页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第7页
第7页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第8页
第8页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第9页
第9页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第10页
第10页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第11页
第11页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第12页
第12页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第13页
第13页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第14页
第14页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第15页
第15页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第16页
第16页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第17页
第17页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第18页
第18页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第19页
第19页 / 共20页
10个经典的C语言面试基础算法及代码.docx_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

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

《10个经典的C语言面试基础算法及代码.docx》由会员分享,可在线阅读,更多相关《10个经典的C语言面试基础算法及代码.docx(20页珍藏版)》请在冰点文库上搜索。

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

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

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

本文是码农网原创整理,转载请看清文末的转载要求,谢谢合作!

算法是一个程序和软件的灵魂,作为一名优秀的程序员,只有对一些基础的算法有着全面的掌握,才会在设计程序和编写代码的过程中显得得心应手。

本文是近百个C语言算法系列的第二篇,包括了经典的Fibonacci数列、简易计算器、回文检查、质数检查等算法。

也许他们能在你的毕业设计或者面试中派上用场。

1、计算Fibonacci数列

Fibonacci数列又称斐波那契数列,又称黄金分割数列,指的是这样一个数列:

1、1、2、3、5、8、13、21。

C语言实现的代码如下:

/*DisplayingFibonaccisequenceuptonthtermwherenisenteredbyuser.*/

#include

intmain()

{

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(count

{

display=t1+t2;

t1=t2;

t2=display;

++count;

printf("%d+",display);

}

return0;

}

结果输出:

Enternumberofterms:

10

FibonacciSeries:

0+1+1+2+3+5+8+13+21+34+

也可以使用下面的源代码:

/*DisplayingFibonacciseriesuptocertainnumberenteredbyuser.*/

#include

intmain()

{

intt1=0,t2=1,display=0,num;

printf("Enteraninteger:

");

scanf("%d",&num);

printf("FibonacciSeries:

%d+%d+",t1,t2);/*Displayingfirsttwoterms*/

display=t1+t2;

while(display

{

printf("%d+",display);

t1=t2;

t2=display;

display=t1+t2;

}

return0;

}

结果输出:

Enteraninteger:

200

FibonacciSeries:

0+1+1+2+3+5+8+13+21+34+55+89+144+

2、回文检查

源代码:

/*Cprogramtocheckwhetheranumberispalindromeornot*/

#include

intmain()

{

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);

else

printf("%disnotapalindrome.",n);

return0;

}

结果输出:

Enteraninteger:

12321

12321isapalindrome.

3、质数检查

注:

1既不是质数也不是合数。

源代码:

/*Cprogramtocheckwhetheranumberisprimeornot.*/

#include

intmain()

{

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);

else

printf("%disnotaprimenumber.",n);

return0;

}

结果输出:

Enterapositiveinteger:

29

29isaprimenumber.

4、打印金字塔和三角形

使用 * 建立三角形

*

**

***

****

*****

源代码:

#include

intmain()

{

inti,j,rows;

printf("Enterthenumberofrows:

");

scanf("%d",&rows);

for(i=1;i<=rows;++i)

{

for(j=1;j<=i;++j)

{

printf("*");

}

printf("\n");

}

return0;

}

如下图所示使用数字打印半金字塔。

1

12

123

1234

12345

源代码:

#include

intmain()

{

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;

}

用*打印半金字塔

*****

****

***

**

*

源代码:

#include

intmain()

{

inti,j,rows;

printf("Enterthenumberofrows:

");

scanf("%d",&rows);

for(i=rows;i>=1;--i)

{

for(j=1;j<=i;++j)

{

printf("*");

}

printf("\n");

}

return0;

}

用*打印金字塔

*

***

*****

*******

*********

源代码:

#include

intmain()

{

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;

}

用*打印倒金字塔

*********

*******

*****

***

*

源代码:

#include

intmain()

{

introws,i,j,space;

printf("Enternumberofrows:

");

scanf("%d",&rows);

for(i=rows;i>=1;--i)

{

for(space=0;space

printf("");

for(j=i;j<=2*i-1;++j)

printf("*");

for(j=0;j

printf("*");

printf("\n");

}

return0;

}

5、简单的加减乘除计算器

源代码:

/*Sourcecodetocreateasimplecalculatorforaddition,subtraction,multiplicationanddivisionusingswitch...casestatementinCprogramming.*/

#include

intmain()

{

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.4

8.4

3.4-8.4=-5.0

6、检查一个数能不能表示成两个质数之和

源代码:

#include

intprime(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:

34

34=3+31

34=5+29

34=11+23

34=17+17

7、用递归的方式颠倒字符串

源代码:

/*Exampletoreverseasentenceenteredbyuserwithoutusingstrings.*/

#include

voidReverse();

intmain()

{

printf("Enterasentence:

");

Reverse();

return0;

}

voidReverse()

{

charc;

scanf("%c",&c);

if(c!

='\n')

{

Reverse();

printf("%c",c);

}

}

结果输出:

Enterasentence:

margorpemosewa

awesomeprogram

8、实现二进制与十进制之间的相互转换

/*Cprogrammingsourcecodetoconverteitherbinarytodecimalordecimaltobinaryaccordingtodataenteredbyuser.*/

#include

#include

intbinary_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)/*Functiontoconvertdecimaltobinary.*/

{

intrem,i=1,binary=0;

while(n!

=0)

{

rem=n%2;

n/=2;

binary+=rem*i;

i*=10;

}

returnbinary;

}

intbinary_decimal(intn)/*Functiontoconvertbinarytodecimal.*/

{

intdecimal=0,i=0,rem;

while(n!

=0)

{

rem=n%10;

n/=10;

decimal+=rem*pow(2,i);

++i;

}

returndecimal;

}

结果输出:

9、使用多维数组实现两个矩阵的相加

源代码:

#include

intmain(){

intr,c,a[100][100],b[100][100],sum[100][100],i,j;

printf("Enternumberofrows(between1and100):

");

scanf("%d",&r);

printf("Enternumberofcolumns(between1and100):

");

scanf("%d",&c);

printf("\nEnterelementsof1stmatrix:

\n");

/*Storingelementsoffirstmatrixenteredbyuser.*/

for(i=0;i

for(j=0;j

{

printf("Enterelementa%d%d:

",i+1,j+1);

scanf("%d",&a[i][j]);

}

/*Storingelementsofsecondmatrixenteredbyuser.*/

printf("Enterelementsof2ndmatrix:

\n");

for(i=0;i

for(j=0;j

{

printf("Enterelementa%d%d:

",i+1,j+1);

scanf("%d",&b[i][j]);

}

/*AddingTwomatrices*/

for(i=0;i

for(j=0;j

sum[i][j]=a[i][j]+b[i][j];

/*Displayingtheresultantsummatrix.*/

printf("\nSumoftwomatrixis:

\n\n");

for(i=0;i

for(j=0;j

{

printf("%d",sum[i][j]);

if(j==c-1)

printf("\n\n");

}

return0;

}

结果输出:

10、矩阵转置

源代码:

#include

intmain()

{

inta[10][10],trans[10][10],r,c,i,j;

printf("Enterrowsandcolumnofmatrix:

");

scanf("%d%d",&r,&c);

/*Storingelementofmatrixenteredbyuserinarraya[][].*/

printf("\nEnterelementsofmatrix:

\n");

for(i=0;i

for(j=0;j

{

printf("Enterelementsa%d%d:

",i+1,j+1);

scanf("%d",&a[i][j]);

}

/*Displayingthematrixa[][]*/

printf("\nEnteredMatrix:

\n");

for(i=0;i

for(j=0;j

{

printf("%d",a[i][j]);

if(j==c-1)

printf("\n\n");

}

/*Findingtransposeofmatrixa[][]andstoringitinarraytrans[][].*/

for(i=0;i

for(j=0;j

{

trans[j][i]=a[i][j];

}

/*Displayingthetranspose,i.e,Displayingarraytrans[][].*/

printf("\nTransposeofMatrix:

\n");

for(i=0;i

for(j=0;j

{

printf("%d",trans[i][j]);

if(j==r-1)

printf("\n\n");

}

return0;

}

结果输出:

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2