OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2304|回复: 1

C语言算法经典87-94

[复制链接]

65

主题

71

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
357
金钱
357
注册时间
2017-7-27
在线时间
24 小时
发表于 2017-10-12 10:53:34 | 显示全部楼层 |阅读模式
【程序87
题目:回答结果(结构体变量传递)
1.程序分析:
2.程序源代码:
#include "stdio.h"
struct student
{ int x;
char c;
} a;
main()
{a.x=3;
a.c='a';
f(a);
printf("%d,%c",a.x,a.c);
}
f(struct student b)
{
b.x=20;
b.c='y';
}
==============================================================
【程序88嵌入式要妖气呜呜吧久零纪要
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
1.程序分析:
2.程序源代码:
main()
{int i,a,n=1;
while(n<=7)
{ do {
scanf("%d",&a);
}while(a<1||a>50);
for(i=1;i<=a;i++)
printf("*");
printf("\n");
n++;}
getch();
}
==============================================================
【程序89
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:
每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
1.程序分析:
2.程序源代码:
main()
{int a,i,aa[4],t;
scanf("%d",&a);
aa[0]=a ;
aa[1]=a 0/10;
aa[2]=a 00/100;
aa[3]=a/1000;
for(i=0;i<=3;i++)
{aa+=5;
aa%=10;
}
for(i=0;i<=3/2;i++)
{t=aa;
aa=aa[3-i];
aa[3-i]=t;
}
for(i=3;i>=0;i--)
printf("%d",aa);
}
==============================================================
【程序90
题目:专升本一题,读结果。
1.程序分析:
2.程序源代码:
#include "stdio.h"
#define M 5
main()
{int a[M]={1,2,3,4,5};
int i,j,t;
i=0;j=M-1;
while(i {t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
i++;j--;
}
for(i=0;i printf("%d",*(a+i));
}
【程序91
题目:时间函数举例1
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "time.h"
void main()
{ time_t lt; /*define a longint time varible*/
lt=time(NULL);/*system time and date*/
printf(ctime(<)); /*english format output*/
printf(asctime(localtime(<)));/*tranfer to tm*/
printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/
}
==============================================================
【程序92
题目:时间函数举例2
1.程序分析:
2.程序源代码:
/*calculate time*/
#include "time.h"
#include "stdio.h"
main()
{ time_t start,end;
int i;
start=time(NULL);
for(i=0;i<3000;i++)
{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
end=time(NULL);
printf("\1: The different is %6.3f\n",difftime(end,start));
}
==============================================================
【程序93
题目:时间函数举例3
1.程序分析:
2.程序源代码:
/*calculate time*/
#include "time.h"
#include "stdio.h"
main()
{ clock_t start,end;
int i;
double var;
start=clock();
for(i=0;i<10000;i++)
{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
end=clock();
printf("\1: The different is %6.3f\n",(double)(end-start));
}
==============================================================
【程序94
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)
1.程序分析:
2.程序源代码:
#include "time.h"
#include "stdlib.h"
#include "stdio.h"
main()
{char c;
clock_t start,end;
time_t a,b;
double var;
int i,guess;
srand(time(NULL));
printf("do you want to play it.('y' or 'n') \n");
loop:
while((c=getchar())=='y')
{
i=rand() 0;
printf("\nplease input number you guess:\n");
start=clock();
a=time(NULL);
scanf("%d",&guess);
while(guess!=i)
{if(guess>i)
{printf("please input a little smaller.\n");
scanf("%d",&guess);}
else
{printf("please input a little bigger.\n");
scanf("%d",&guess);}
}
end=clock();
b=time(NULL);
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
if(var<15)
printf("\1\1 You are very clever! \1\1\n\n");
else if(var<25)
printf("\1\1 you are normal! \1\1\n\n");
else
printf("\1\1 you are stupid! \1\1\n\n");
printf("\1\1 Congradulations \1\1\n\n");
printf("The number you guess is %d",i);
}
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
if((c=getch())=='y')
goto loop;
}
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

558

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
164846
金钱
164846
注册时间
2010-12-1
在线时间
2098 小时
发表于 2017-10-13 00:52:06 | 显示全部楼层
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-4-20 17:03

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表