面试笔试题目(参考)

1、使用冒泡算法实现对一维数组str中十个元素进行排序(从小到大)

#include
 
  
void swap(char *a,char *b)
{
char c;
c=*a;
*a=*b;
*b=c;
}
 
  
int main()
{
    char str[10] = {2,5,45,23,1,34,67,89,10,50};
    int i,j;
    for(i=0;i<10;i++)
    {
        for(j=i+1;j<10;j++)
        {
        if(str[i]>str[j])
        {
            swap(&str[i],&str[j]);
        }
    }
    //for(i=0;i<10;i++)
    printf("%d\n",str[i]);
    }
    return 0;
}
 
  

2、利用while / for / do...while循环语句,计算1-1/2+1/3-1/4+...+1/99-1/100的值

1)for循环

#include
 
  
int main()
{
    double i=0,t=0,sum=0,sign=-1;
    for(i=1;i<=100;i++)
    {
        sign = -sing;
        t=sign/i;
        sum=sum+t;
    }
    printf("sum = %lf\n",sum);
    return 0;
}
 
   
  

2)while循环

#include 
 
  
int main()
{
    double i=1,t=0,sum=0,sign=-1;
    while(i<=100)
    {
        sign = -sign;
        t=sign/i;
        sum = sum+t;
        i++;
    }
    printf("sum = %lf\n",sum);
    return 0;
}

3)do...while循环

#include
 
  
int main()
{
    double i=1,t=0,sum=0,sign=-1;
    do
    {
        sign = -sign;
        t=sign/i;
        sum=sum+t;
        i++;
    }
    while(i<=100)
    printf("sum = %lf\n",sum);
    
    return 0;
}
 
   
  

你可能感兴趣的:(面试笔试题目(参考))