10個經典的C語言面試基礎算法及代碼
算法是一個程序和軟件的靈魂,下面小編為大家整理了10個經典的`C語言面試基礎算法及代碼,希望能幫到大家!
1、計算Fibonacci數列
Fibonacci數列又稱斐波那契數列,又稱黃金分割數列,指的是這樣一個數列:1、1、2、3、5、8、13、21。
C語言實現的代碼如下:
/* Displaying Fibonacci sequence up to nth term where n is entered by user. */
#include
int main()
{
int count, n, t1=0, t2=1, display=0;
printf("Enter number of terms: ");
scanf("%d",&n);
printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */
count=2; /* count=2 because first two terms are already displayed. */
while (count<n)
{
display=t1+t2;
t1=t2;
t2=display;
++count;
printf("%d+",display);
}
return 0;
}
結果輸出:
Enter number of terms: 10
Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+
也可以使用下面的源代碼:
/* Displaying Fibonacci series up to certain number entered by user. */
#include
int main()
{
int t1=0, t2=1, display=0, num;
printf("Enter an integer: ");
scanf("%d",&num);
printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */
display=t1+t2;
while(display<num)
{
printf("%d+",display);
t1=t2;
t2=display;
display=t1+t2;
}
return 0;
}
結果輸出:
Enter an integer: 200
Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+55+89+144+
2、迴文檢查
源代碼:
/* C program to check whether a number is palindrome or not */
#include
int main()
{
int n, reverse=0, rem,temp;
printf("Enter an integer: ");
scanf("%d", &n);
temp=n;
while(temp!=0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10;
}
/* Checking if number entered by user and it's reverse number is equal. */
if(reverse==n)
printf("%d is a palindrome.",n);
else
printf("%d is not a palindrome.",n);
return 0;
}
結果輸出:
Enter an integer: 12321
12321 is a palindrome.
3、質數檢查
注:1既不是質數也不是合數。
源代碼:
/* C program to check whether a number is prime or not. */
#include
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
結果輸出:
Enter a positive integer: 29
29 is a prime number.
4、打印金字塔和三角形
使用 * 建立三角形
*
* *
* * *
* * * *
* * * * *
源代碼:
#include
int main()
{
int i,j,rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;++i)
{
for(j=1;j<=i;++j)
{
printf("* ");
}
printf("");
}
return 0;
}
如下圖所示使用數字打印半金字塔。
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
源代碼:
#include
int main()
{
int i,j,rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;++i)
{
for(j=1;j<=i;++j)
{
printf("%d ",j);
}
printf("");
}
return 0;
}
用 * 打印半金字塔
* * * * *
* * * *
* * *
* *
*
源代碼:
#include
int main()
{
int i,j,rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=rows;i>=1;--i)
{
for(j=1;j<=i;++j)
{
printf("* ");
}
printf("");
}
return 0;
}
用 * 打印金字塔
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
源代碼:
#include
int main()
{
int i,space,rows,k=0;
printf("Enter the number of rows: ");
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("");
}
return 0;
}
用 * 打印倒金字塔
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
源代碼:
#include
int main()
{
int rows,i,j,space;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=rows;i>=1;--i)
{
for(space=0;space<rows-i;++space)
printf(" ");
for(j=i;j<=2*i-1;++j)
printf("* ");
for(j=0;j<i-1;++j)
printf("* ");
printf("");
}
return 0;
}
-
C語言中文件操作詳解及實例代碼
在文件操作中除了打開操作以及讀寫操作,還有幾種比較常見的操作。以下是本站小編搜索整理的關於這些操作中涉及到的函數,,需要的朋友可以參考本文!想了解更多相關信息請持續關注我們應屆畢業生考試網!一.移動位置指針的函數rewind函數和fseek函數,這兩個函數的原...
-
C語言轉義字符和格式控制符參考
導語:字符型常量所表示的值是字符型變量所能包含的值。我們可以用ASCII表達式來表示一個字符型常量,或者用單引號內加反斜槓表示轉義字符。下面是C語言轉義字符和格式控制符參考,歡迎學習:轉義字符參考:a:蜂鳴,響鈴:回退:向後退一格f:換頁:換行,光標到下行行首:回車,光...
-
C語言中的整數(short,int,long)
整數是編程中常用的一種數據,C語言使用int來定義整數(int是integer的簡寫)。歡迎大家閲讀!更多相關信息請關注相關欄目!現代操作系統中int一般佔用4個字節內存,共計32位(Bit)。如果不考慮正負數,當所有的位都為1時它的值最大,為232-1=4,294,967,295≈43億,這是一個很大的...
-
二級C語言考試複習五要點
綜觀近幾年全國計算機等級二級基礎部分和C語言程序設計的題目,筆試中大多數考題是與大綱要求的基本內容一致的,難度不高,但內容十分廣泛,應牢固掌握。以下是小編為您帶來的二級C語言考試複習五要點,感謝您的閲讀!一、瞭解試卷,胸中有數綜觀近幾年全國計算機等級二級...