嵌入式題目,C語言
練習1
程式設計題:
29
#include <stdio.h>
main()
{ long int m9=9,sum=9;
int zi,n1=1,c9=1;
scanf("%d",&zi);
while(n1!=0)
{ if(!(sum%zi))
n1=0;
else
{m9=m9*10;
sum=sum+m9;
c9++;
}
}
printf("%ld,can be divided by %d \"9\"",sum,c9);
}
30
#include <stdio.h>
void main(void)
{
int fun(int);
int i;
i=5;
printf("@#@# %d \n\n",fun(i));
}
int fun(int j)
{
int sum;
if(j==0)
sum=1;
else
sum=j*fun(j-1);
return sum;
}
練習2
#include <stdio.h>
#define size 10
int g[size]={0,1,2,3,4,5,6,7,8,9};
int search(int x)
{
int low=0;
int high=size-1;
int mid;
while(low<=high) {
mid=(low+high)/2;
if(g[mid]==x)
return mid;
else if(g[mid]<x)
low=mid+1;
else if(g[mid]>x)
high=mid-1;
}
return -1;
}
void main()
{
int result=0;
result=search(6);
printf("%d",result);
}
37.
#include <stdio.h>
int cal_bit(char a)
{
int i,j=0;
for(i=0;i<8;i++) {
if((a&0x01)==0x01)
j++;
a=a>>1;
}
return j;
}
int main()
{
int a= 0 ;
a = cal_bit('g');
printf("%d", a );
return 0;
}