当前位置:首页 » 编程语言 » c语言实例六
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言实例六

发布时间: 2022-03-12 09:10:37

A. c语言编程实例

int countCattles(int cattles, int years){
return years<=0?cattles: countCattles(cattles*2,years-4);
}
===
就可以了
printf("20年后有%d只\n ", countCattles(1,20);

B. C语言程序设计实例

#include<stdio.h>
void main()
{
int a = 1,b = 2;
int c;
c = a + b;
printf("%d\n",c);
}

输出结果:3

C. 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整除:";
cin>>n;
if(n%4 != 0 )cout<<"笨蛋,看清楚题目!\n"<<endl;
else if(n>64)cout<<n<<"! 这么大, 想累死我啊, 不给算了!\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++)
{
cout<<a[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];
cout<<sum<<"\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; bx<MaxBlock/2; bx++)
for(int by=0; by<MaxBlock; 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; bx<MaxBlock/2; bx++)
{
for(int by=0; by<MaxBlock; 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; by<MaxBlock; 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]);
}
}

}

}

D. C语言实例教程

C语言是基础。C语言实例教程几乎也不是你想象的直接可以写出记事本、播放器等windows窗口程序。如果要非要写的话,你得先学完了C,再学学windows编程,调用windows api 来完成。现在一般用vc++。
感觉你就没有学进去。
不过,说实话,这可真不是几个月的功夫……你至少要学习:c语言教程,数据结构与算法,还有c++的面向对象的思想与实现方法……太多太多

E. c语言程序经典实例

#include <stdio.h>

void printk(void)
{
printf(" ");
}
void printx(void)
{
printf("*");
}
void main()
{
int n,i,j;
printf("Please input n:");
scanf("%d",&n);
for (i = 0;i <= 2*n;i++)
{
if (i <= n)
{
for (j = 0;j < n-i;j++)
{
printk();
}
for(j = 0;j < 2*i+1;j++)
{
printx();
}
printf("\n");

}
if (i > n)
{
for (j = 0;j <= i-n-1;j++)
{
printk();
}
for (j = 0;j < 2*(2*n-i)+1;j++)
{
printx();
}
printf("\n");
}
}

}

F. c语言编程实例

#include <stdio.h>
int main(void)
{
printf("hello world!");

return 0;

}

G. c语言实例

//用绘图函数即可。

#include<graphics.h>
void main()
{
int driver,mode;
driver=DETECT;
mode=0;
initgraph(&driver,&mode,"");
setcolor(15);
line(66,66,88,88);
lineto(100,100);
linerel(36,64);
getch();
restorecrtmode();
}

H. C语言实例

#include
<stdio.h>
#define
N
50
//
排队人数(可任意更改)
#define
CAL
3
//凡报3的人出列(可任意更改)
//下面是排队编号函数:从h
开始的n个人依次编号1到n
void
stdline(int
*h,int
n)
{
int
i;
for(i=1;i<n+1;i++)
*(h+i-1)=i;
}
/*下面函数表示从指针h处开始的人数为boy个人排队,从1报数,每报到call的人出列*/
void
outline(int
*h,int
boy,int
call)
{
int
*p,
chu,
callnum;
/*说明:
p
工作指针,表示从头依次指向每个元素,点名
chu
计数器,记录出列的人数
callnum
计数器,记录点名次序
*/
chu=0;
callnum=0;//各计数器清零
p=h;
//开始时,工作指针指向数组首
printf("出列顺序是:\n");
while(chu<boy)
{
if(*p!=0)
callnum++;
//每次加报数
if(callnum==call)
//如果某一个人报到出列数call...
{
printf("%5d",*p);
//打印编号,表示出列
chu++;
//出列人数加1
if(chu==boy)//如果全部出列....
{
*h=*p;
//把最后一个出列人的编号记入地址开始处
return;
//结束
}
if(chu%10==0)printf("\n");//每输出10个换行
callnum=0;
//出列后,重新报数
*p=0;
//出列后,将其编号赋零,以示区别
}
p++;
//工作指针移向下一个人,即下一个数组元素
if(p>h+boy-1)p=h;/*如果移到最后一个元素的后面,则让指向地址开头继续报数*/
}
}
void
main()
{
int
a[N];
//用数组模拟队列,每个元素代表一个人
stdline(a,N);//编号
outline(a,N,CAL);//计算并打印出列顺序
printf("\n最后留下来的是
%d
号\n",*a);/*在函数中,已经把最后一个人的编号写入了数组首地址处,
这里输出就可以了*/
}

I. c语言程序设计实例

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void main()
{
int num,x,j=1; //x 为随机数,num为你要输入的数,j为猜的次数
srand((float)time(0));
x=rand()%100;//产生100以内的随机数
printf("请输入你猜想的数字 num:\n");
scanf("%d",&num);
loop: if(num>x) {
printf("你输的数大了!,请重新输入");
scanf("%d",&num);
j++;
goto loop;
}
else if(num<x)
{
printf("你输的数小了!请重新输入");
scanf("%d",&num);
j++;
goto loop;
}
else printf("恭喜你,第 %d次 猜对了!!,这个数是%d\n",j,num);
}

J. C语言实例

到网络上去找了