//模拟扑克牌;
//思路:首先通过语句char b[13]定义扑克的花色,然后通过两个for循环生成一副新牌,最后srand生成随机数实现随机数打乱牌的顺序。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
int i,j;
char ch1,ch2;
char s[104];
char a[]="hdsc";
char b[13]={'A','K','Q','J','10','9','8','7','6','5','4','3','2'};
for(i=0;i<4;i++)
for(j=0;j<13;j++)
{
s[2*j+26*i]=a[i];
s[2*j+26*i+1]=b[j];
}
srand(time(0));
for(i=0;i<52;i++)
{
j=rand()%(52-i);
ch1=s[2*j];
s[2*j]=s[102-2*i];
s[102-2*i]=ch1;
ch2=s[2*j+1];
s[2*j+1]=s[103-2*i];
s[103-2*i]=ch2;
}
printf("\n\n");
for(i=0,j=0;i<104;i+=2,j++)
{
if(!(j%13))
printf("\n\n\n");
printf(" %c%c",s[i],s[i+1]);
}
printf("\n\n");
return;
}
//成绩统计
//将学生的信息存储在数组中,然后利用循环计算出数组中存储的学生平均成绩,并找出成绩高于平均分的学生的信息;
#include<stdio.h>
#define N 2
main()
{
int x,num[N+1];
float st[N+1],ave,sum=0;
printf("请输入N个人的学号与成绩;\n");
printf("学号 成绩\n");
for(x=1;x<=N;x++)
scanf("%d%f",&num[x],&st[x]);
for(x=1;x<=N;x++)
sum=sum+st[x];
ave=sum/N;
printf("aerage is :%f\n",ave);
for(x=1;x<=N;x++)
if(st[x]>=ave)
printf("%d\t%f\n",num[x],st[x]);
}
//矩阵交换;
//有一个3*4的矩阵,找出每一行中最大的值并与第一列交换;
#include<stdio.h>
main()
{
int x,y,t,p;
int a[3][4]={{9,8,7,11},{4,6,2,1},{5,3,19,10}};
printf("原来的数组为:\n");
for(x=0;x<=2;x++)
{
for(y=0;y<=3;y++)
printf("%d\t",a[x][y]);
printf("\n");
}
for(x=0;x<=2;x++)
{
p=0;
for(y=0;y<=3;y++)
if(a[x][y]>a[x][p])
p=y;
t=a[x][0];
a[x][0]=a[x][p];
a[x][p]=t;
}
printf("交换后的数组为:\n");
for(x=0;x<=2;x++)
{
for(y=0;y<=3;y++)
printf("%d\t",a[x][y]);
printf("\n");
}
}
//字符排序;
//输入3个字符串,要求按照字母由小到大的顺序输出这3个字符串;
#include<stdio.h>
#include<string.h>
main()
{
char a[3][18],s[18];
printf("请输入3个字符串:\n");
gets(a[0]);
gets(a[1]);
gets(a[2]);
if(strcmp(a[0],a[1])>0)
{
strcpy(s,a[0]);
strcpy(a[0],a[1]);
strcpy(a[1],s);
}
if(strcmp(a[0],a[2])>0)
{
strcpy(s,a[0]);
strcpy(a[0],a[2]);
strcpy(a[2],s);
}
if(strcmp(a[1],a[2])>0)
{
strcpy(s,a[1]);
strcpy(a[1],a[2]);
strcpy(a[2],s);
}
printf("排序结果为:\n");
printf("\n%s\n%s\n%s\n",a[0],a[1],a[2]);
}
//杨辉三角;
#include<stdio.h>
//int x,y;
int c(int x,int y)
{
int z;
if((y==1)||(y==x+1))
return(1);
z=c(x-1,y-1)+c(x-1,y);
return (z);
}
main()
{
int i,j,n=13;
printf("N= ");
while(n>12)
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=0;j<12-i;j++)
printf(" ");
for(j=1;j<i+2;j++)
printf("%6d",c(i,j));
printf("\n");
}
}
//学生成绩检查;
#include<stdio.h>
#include<windows.h>
void search(float (*p)[4],int m)
{
int i,j,flag;
for(i=0;i<m;i++)
{
flag=0;
for(j=0;j<4;j++)
{
if(*(*(p+i)+j)<60)
flag=1;
if(flag==1)
{
printf("Np.%d is flunked ,score are :\n",i+1);
for(j=0;j<4;j++)
printf("%5.1f",*(*(p+i)+j));
printf("\n");
}
}
}
}
main()
{
void search(float (*p)[4],int m);
static float score[5][4]={{65,80,78,90},{98,89,100,81},{92,56,78,70},{78,63,80,70},{64,55,70,81}};
system("cls");
search(score,5);
}
//清屏函数:sysem("cls");
#include<stdio.h>
#include<windows.h>
void main()
{
int n;
printf("hahahaha!!");
for(int i=0;i<4;i++)
{
scanf("%d",&n);
system("cls");
}
printf("youyouyou!!");
printf("\n\n");
// getch();
}
//奇偶函数调用;
//输入n为偶数时,调用1/2+1/4+...+1/n;n为奇数时,调用1/1+1/3+...+1/n;
#include<stdio.h>
#include<windows.h>
float peven(int n)
{
int i;
float s=1;
for(i=2;i<=n;i+=2)
s+=1/(float)i;
return s;
}
float podd(int n)
{
int i;
float s=0;
for(i=2;i<=n;i+=2)
s+=1/(float)i;
return s;
}
void main()
{
float sum;
int n;
while(1)
{
scanf("%d",&n);
system("cls");
if(n>1)
break;
}
if(n%2==0)
{
printf("Even=");
sum=peven(n);
}
else
{
printf("odd=");
sum=podd(n);
}
printf("%f\n",sum);
}
//汉诺塔问题;
#include<stdio.h>
#include<windows.h>
hanoi(int n,char x,char y,char z)
{
if(n==1)
printf("%c->%c\n",x,z);
else
{
hanoi(n-1,x,z,y);
printf("%c->%c\n",x,z);
hanoi(n-1,y,x,z);
}
}
void main()
{
int m;
printf("input the number of diskes:\n");
scanf("%d",&m);
system("cls");
printf("The step to moving %3d diskes:\n",m);
hanoi(m,'a','b','c');
}
//指针函数;
#include<stdio.h>
int * GetDate(int wk,int dy);
main()
{
int wk,dy;
do
{
printf("Enter week(1-5)day(1-7)\n");
scanf("%d%d",&wk,&dy);
} while(wk<1||wk>5||dy<1||dy>7);
printf("%d\n",*GetDate(wk,dy));
}
int * GetDate(int wk,int dy)
{
static int calendar[5][7]= { {1,2,3,4,5,6,7}, {8,9,10,11,12,13,14}, {15,16,17,18,19,20,21}, {22,23,24,25,26,27,28}, {29,30,31,-1} };
printf("calendar[5][7]:\n");
for(int i=0;i<5;i++)
for(int j=0;j<7;j++)
{
printf("%4d",calendar[i][j]);
if(j==6)
printf("\n");
}
return &calendar[wk-1][dy-1];
}
#include<stdio.h>
void (*funcp)();
void FileFunc(),EditFunc();
main()
{
funcp=FileFunc;
(*funcp)();
funcp=EditFunc;
(*funcp)();
}
void FileFunc()
{
printf("FileFunc\n");
}
void EditFunc()
{
printf("EditFunc\n");
}
//求最小值;
#include<stdio.h>
int min1(int x,int y)
{
int z;
z=x<y?x:y;
return z;
}
int min2(int x,int y,int z)
{
int s;
s=min1(x,y);
s=min1(s,z);
return s;
}
main()
{
int a,b,c,m;
printf("\n please input three number:\n");
scanf("%d%d%d",&a,&b,&c);
m=min2(a,b,c);
printf("\nthe min is %d\n",m);
}