c语言程序代码例子大全准确(c语言 代码)
c语言必背100代码有哪些?
/*输出9*9口诀。共9行9列,i控制行,j控制列。*/
#include "stdio.h"
main()
{int i,j,result;
for (i=1;i10;i++)
{ for(j=1;j10;j++)
{
result=i*j;
printf("%d*%d=%-3d",i,j,result);/*-3d表示左对齐,占3位*/
}
printf("\n");/*每一行后换行*/
}
}
扩展资料:
C语言的字符串其实就是以'\0'字符结尾的char型数组,使用字符型并不需要引用库,但是使用字符串就需要C标准库里面的一些用于对字符串进行操作的函数。它们不同于字符数组。使用这些函数需要引用头文件string.h。
文件输入/输出
在C语言中,输入和输出是经由标准库中的一组函数来实现的。在ANSI C中,这些函数被定义在头文件stdio.h;中。
标准输入/输出
有三个标准输入/输出是标准I/O库预先定义的:
stdin标准输入
stdout标准输出
stderr输入输出错误
参考资料来源:百度百科-c语言
C语言简单例子
1 c语言是面向过程的语言,可以理解为一个函数调用另一个函数的语言,其中程序的入口是main函数
2 示例
#includestdio.h //包含要用到的函数的头文件,如printf
int?max(int?a,?int?b);//函数声明
int?main(){//程序入口函数
int?a?=?12,?b?=?9;
int?m?=?max(a,?b);//函数调用
printf("max=%d\n",?m);
getchar();
return?0;
}
int?max(int?a,?int?b){//函数定义
return?a??b???a?:?b;
}
3 运行结果
c语言例子 100行以上
#include "stdafx.h"
#include "iostream.h"
#define M 2000
#define N 8
void magic(int a[M][M], int);
int main(int argc, char* argv[])
{
static int a[M][M];
int d = 12;
int n = 8;
while(1){
while(1)
{
cout"请输入方阵的阶数, 阶数必须能被4整除:";
cinn;
if(n%4 != 0 )cout"笨蛋,看清楚题目!\n"endl;
else if(n64)coutn"! 这么大, 想累死我啊, 不给算了!\n"endl;
else break;
}
//Init
for(int i=1; i=n; i++)
{
for(int j=1; j=n; j++)
{
a[i][j]=d;
d++;
// d++;
}
}
magic(a, n);
//Print dimension and sum for rows
for( i=1; i=n; i++)
{
int sum=0;
for(int j=1; j=n; j++)
{
couta[i][j]"\t";
sum+=a[i][j];
}
cout" | "sum"\n\n";
}
//Print sum of columns
for(i = 1; i=n; i++)cout"--\t";
cout"\n";
for(i=1; i=n; i++)
{
int sum = 0;
for(int j = 1; j=n; j++) sum += a[j][i];
coutsum"\t";
}
cout"\n\n";
char c;
cout"Continue?(y/n)";
cin c;
if(c=='n'|| c=='N')break;
}
return 0;
}
void exchg(int a, int b)
{
int t;
t = a;
a = b;
b = t;
}
void magic(int a[M][M], int n) // a:= 矩阵 n:= 实际阶数
{
int baseBlock_x=0;
int baseBlock_y=0;
int MaxBlock = n/4;
if(MaxBlock%2==0)
{
for(int bx = 0; bxMaxBlock/2; bx++)
for(int by=0; byMaxBlock; by++)
{
for(int c = 1; c = 4; c++)
{
exchg(a[bx*4+c][by*4+c], a[n+1-bx*4-c][n+1-by*4-c]);
exchg(a[bx*4+c][by*4+5-c], a[n+1-bx*4-c][n+1-by*4-5+c]);
}
}
}
else
{
for(int bx = 0; bxMaxBlock/2; bx++)
{
for(int by=0; byMaxBlock; by++)
{
for(int c = 1; c = 4; c++)
{
exchg(a[bx*4+c][by*4+c], a[n+1-bx*4-c][n+1-by*4-c]);
exchg(a[bx*4+c][by*4+5-c], a[n+1-bx*4-c][n+1-by*4-5+c]);
}
}
}
bx = MaxBlock/2;
for(int by=0; byMaxBlock; by++)
{
for(int c = 1; c = 2; c++)
{
exchg(a[bx*4+c][by*4+c], a[n+1-bx*4-c][n+1-by*4-c]);
exchg(a[bx*4+c][by*4+5-c], a[n+1-bx*4-c][n-by*4-4+c]);
}
}
}
}
求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; in - 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; iNUM; i++) {
printf("请输入第(%d)个数:",i+1);
scanf("%d",arr[i]);
}
printf("\n输入如下排列:\n");
for(i=0; iNUM; i++) {
printf("%4d",arr[i]);
}/*
for(i=0; iNUM; i++) {
for(j=i+1; jNUM; 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; iNUM; i++) {
printf("%4d",arr[i]);
}*/
printf("\n从大到小如下排列:\n");
for(i=NUM-1; i=0; i--) {
printf("%4d",arr[i]);
}
return 0;
}
经典C语言程序例子
题目01:在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,空格用来分隔不同的单词。
直接编译,程序执行结果如下图所示:
题目02:编写一个int string_len(char *s),返回字符串s的字符长度(不包括\0)。
直接编译,程序执行结果如下图所示:
扩展资料:
C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。
尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。