當前位置:首頁 » 編程語言 » 優秀的c語言代碼

優秀的c語言代碼

發布時間: 2022-07-10 20:50:44

⑴ 收集神奇的或功能強悍的而且短小的c語言代碼

如下C語言代碼:

//求兩數最大公約數代碼。
#include<stdio.h>
intgcd(inta,intb)//求最大公約數函數。
{
if(a%b==0)returnb;
elsereturngcd(b,a%b);//輾轉相除法。
}
voidmain()
{
inta,b;
scanf("%d%d",&a,&b);//輸入兩個數。
printf("%d ",gcd(a,b));//輸出這兩個數的最大公約數。
}

給出的代碼是求兩數最大公約數的C語言代碼。

⑵ 求幾個比較有趣,簡單的C語言源代碼 小白自己敲著練一下手感

最簡單的模擬計時器:

#include<stdio.h>

#include<conio.h>

#include<windows.h>

int m=0,s=0,ms=0; //m是分 s是秒 ms是毫秒

//以下是5個自編函數

void csh( ); //初始化界面

void yinc(int x,int y); //隱藏游標的函數(y值設為0就會隱藏)

void jishi( ); //計時器運行(每100毫秒變化一次)

void Color (short x, short y); //設定顏色的函數(y設為0就是黑底)

void gtxy (int x, int y); //控制游標位置的函數

int main( ) //主函數

{ csh( );

getch( );

while(1)

{ jishi( );

Sleep(100); //間隔100毫秒

if( kbhit( ) )break; //有鍵按下就退出循環

}

return 0;

}

void csh( ) //初始化界面

{Color(14,0); //設定淡黃字配黑底

printf(「 計時器」);

Color(10,0); //設定淡綠字配黑底

printf(" ┌───────────┐");

printf(" │ │");

printf(" └───────────┘");

gtxy(10,4); //游標到屏幕第10列4行處輸出

Color(7,0); //恢復白字黑底

printf(" 00:00:00 ");

yinc(1,0 ); //隱藏游標(yinc代表隱藏)

return;

}

void jishi( ) //計時器運行

{ms+=1;

if(ms==10){s+=1;ms=0;}

if(s==60){m+=1;s=0;}

gtxy(10,4);

Color(9,0); //設定淡藍字配黑底

if(m>9) printf(" %d:",m);

else printf(" 0%d:",m);

Color(14,0); //設定淡黃字配黑底

if(s>9) printf("%d:",s);

else printf("0%d:",s);

Color(12,0); //設定淡紅字配黑底

printf("0%d",ms);

}

void gtxy (int x, int y) //控制游標位置的函數

{ COORD pos;

pos.X = x;

pos.Y = y;

SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );

}

void Color (short ForeColor= 7, short BackGroundColor= 0) //設定顏色的函數

{ HANDLE handle = GetStdHandle ( STD_OUTPUT_HANDLE );

SetConsoleTextAttribute ( handle, ForeColor + BackGroundColor * 0x10 );

}

void yinc(int x,int y) //隱藏游標的設置(gb代表游標)

{ CONSOLE_CURSOR_INFO gb={x,y}; //x為1-100,y為0就隱藏游標

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &gb);

}

⑶ 求C語言的一些代碼!

#include<iostream.h>

void print(int a_h[]) {
for (int i=0; i<9; i++)
cout<<a_h[i]<<' ';
cout<<endl;
}

void swap(int &a, int &b) {
int t = a;
a = b;
b = t;
}//交換

void reverse(int a[], int i, int j) {
while (i<j) {
swap(a[i++],a[j--]);
}
}//顛倒

T(int a[]) {
int b, c, d;
b = 1000 * a[0] + 100 * a[1] + 10 * a[2] + a[3];
c = a[4];
d = 1000 * a[5] + 100 * a[6] + 10 * a[7] + a[8];
if(b * c == d) return 1;
else return 0;
}

void main() {
int a_i[9] = {1,2,3,4,5,6,7,8,9}, i, j, n = 0;
cout<<" a b c d e f g h i\n";
while (1) {
if (T(a_i)) {
cout<<++n<<':';
print(a_i);
}
for (i=7; i>=0; i--)
if (a_i[i] < a_i[i+1]) break;
if (i==-1) break;
for (j=8; j>i; j--)
if (a_i[j] > a_i[i]) break;
swap(a_i[i],a_i[j]);
reverse(a_i,i+1,8);
}
}

⑷ 求簡單C語言程序代碼!

輸入2個正整數m和n,求其最大公約數和最小公倍數

#include

#include

int main()

int m,n,p,q,s,r;

printf("請輸入兩個正整數;m,n ");

scanf("%d,%d",&m,&n);

#include<stdio.h>

main()

int a,b,t=0;

scanf("%d %d",&a,&b);

if (a<b)

printf("%d %d %d %d %d",(a+b),(a-b),(a/b),(a*b),(a%b));

}

主要特點

C語言是一種結構化語言,它有著清晰的層次,可按照模塊的方式對程序進行編寫,十分有利於程序的調試,且c語言的處理和表現能力都非常的強大,依靠非常全面的運算符和多樣的數據類型,可以輕易完成各種數據結構的構建,通過指針類型更可對內存直接定址以及對硬體進行直接操作,因此既能夠用於開發系統程序,也可用於開發應用軟體。

以上內容參考:網路-c語言

⑸ 最簡單的C語言代碼

最簡單的C語言代就是輸出「helloWord」,通常是作為初學編程語言時的第一個程序代碼。具體代碼如下:

#include <stdio.h>

int main(){

printf("Hello, World! ");

return 0;

}

(5)優秀的c語言代碼擴展閱讀:

1、程序的第一行#include <stdio.h>是預處理器指令,告訴 C 編譯器在實際編譯之前要包含 stdio.h 文件。

2、下一行intmain()是主函數,程序從這里開始執行。

3、下一行printf(...)是C中另一個可用的函數,會在屏幕上顯示消息"Hello,World!"。

4、下一行return0;終止main()函數,並返回值0。

⑹ 有什麼好玩的c語言代碼

哈哈~ C語言再配上easyx插件,可以開發很多好玩的游戲哦^_^這篇博文網頁鏈接基於easyx實現的小藍鯨跑酷游戲,有詳細開發文檔和源碼,也可以在這個網站上面搜索看看,還有其他很多好玩的程序源碼和開發文檔,一個干貨滿滿的博客

⑺ 顯示優秀(90~100)的人數及百分比的c語言程序代碼

#include<stdio.h>

voidmain()
{
inti,j=0,n;
inta[100]={0};
printf("請輸入總人數:");
scanf("%d",&n);
printf("請輸入所有人的分數:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]>=90&&a[i]<=100)
j++;
}
printf("總人數為:%d 優秀的人數為:%d 比例為:%f ",n,j,(float)j/(float)n);

}

⑻ 哪裡能看到優秀的c語言代碼

開源的論壇上面有很多,國外的github,國內的開源中國都很多的。尤其如果用Linux系統,可以直接用幫助命令看到海量的C代碼,操作系統的和C語言標准庫的都有,比Windows要方便很多。

⑼ 求50行簡單C語言程序代碼,基礎的就好

#include <stdio.h>
#include <stdlib.h>

#define NUM 10
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//冒泡排序演算法
//基本思想:比較相鄰的兩個數,如果前者比後者大,則進行交換。每一輪排序結束,選出一個未排序中最大的數放到數組後面。
void bubbleSort(int *arr, int n) {
int i,j;
for (i = 0; i<n - 1; i++)
for (j = 0; j < n - i - 1; j++) {
//如果前面的數比後面大,進行交換
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
//最差時間復雜度為O(n^2),平均時間復雜度為O(n^2)。穩定性:穩定。輔助空間O(1)。
//升級版冒泡排序法:通過從低到高選出最大的數放到後面,再從高到低選出最小的數放到前面,
//如此反復,直到左邊界和右邊界重合。當數組中有已排序好的數時,這種排序比傳統冒泡排序性能稍好。
//升級版冒泡排序演算法
void bubbleSort_1(int *arr, int n) {
//設置數組左右邊界
int left = 0, right = n - 1;
//當左右邊界未重合時,進行排序
while (left<=right) {
int i,j;
//從左到右遍歷選出最大的數放到數組右邊
for (i =left; i < right; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
right--;
//從右到左遍歷選出最小的數放到數組左邊
for (j = right; j> left; j--) {
if (arr[j + 1] < arr[j]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
left++;
}
}
int main(int argc, char *argv[]) {
int arr[NUM],i,j,temp;
printf("請輸入10個數:\n");
for(i=0; i<NUM; i++) {
printf("請輸入第(%d)個數:",i+1);
scanf("%d",&arr[i]);
}
printf("\n輸入如下排列:\n");
for(i=0; i<NUM; i++) {
printf("%4d",arr[i]);
}/*
for(i=0; i<NUM; i++) {
for(j=i+1; j<NUM; j++) {
if(arr[i]>arr[j]) {
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}*/
bubbleSort_1(arr,NUM);
/*printf("\n從小到大如下排列:\n");
for(i=0; i<NUM; i++) {
printf("%4d",arr[i]);
}*/
printf("\n從大到小如下排列:\n");
for(i=NUM-1; i>=0; i--) {
printf("%4d",arr[i]);
}
return 0;
}

熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:432
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:557
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:743
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:537
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:146
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:234
java駝峰 發布:2025-02-02 09:13:26 瀏覽:651
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:538
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726