优秀的c语言代码
⑴ 收集神奇的或功能强悍的而且短小的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;
}