嵌入式题目,C语言

版权声明:Yangliwei 版权所有 https://blog.csdn.net/qq_32674347/article/details/84562155

练习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;

扫描二维码关注公众号,回复: 4422131 查看本文章

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;
}

 

猜你喜欢

转载自blog.csdn.net/qq_32674347/article/details/84562155