C语言经典算法题

Posted aiguangyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言经典算法题相关的知识,希望对你有一定的参考价值。

1. 采用递归求解的方法,用倒序输出字符串系统,用户输入字符串,程序运行后,将倒序输出。

#include "stdio.h"
void reverse(){
  char c ;
  if((c=getchar())!='\\n'){
    reverse();
  };
  printf("%c",c);
};

int main(){
  printf("请输入一个字符串");
  reverse();
  printf("\\n");
  return 0;
}

2. 输入一行字符串,按如下规则加密:如果是英文字母则大写变小写、小写变大写,对非英文字符保持不变。

#include "stdio.h"
int main(){
  char ch;
  printf("请输入一个字符串:");
  while ((ch=getchar())!='\\n'){
    if(ch>='A'&&ch<='Z'){
      ch=ch-'A'+'a';
    };
    if(ch>='a'&&ch<='z'){
      ch=ch-'a'+'A';
    };
    putchar(ch);
  };
  return 0;
}

3. 设计一个学生的学习系统。例如:某学习小组有4位同学,学习五门课程求每个同学的平均分。

#include "stdio.h"

// 定义学生的结构体
struct  student{
  char name[20];
  float score1;
  float score2;
  float score3;
  float score4;
  float score5;
};

int main(){
  struct student students[4]={
    {"赵琳", 80, 82, 91, 68, 77},
    {"张强", 78, 83, 82, 72, 80},
    {"张帅", 73, 58, 62, 60, 75},
    {"李莉", 83, 87, 89, 79, 81}
  };
  for(int i=0;i<4;i++){
    struct student person = students[i];
    float avg = (person.score1+person.score2+person.score3+person.score4+person.score5)/5;
    printf("第%d个学生%s的平均成绩为%f\\n",i+1,person.name,avg);
    printf("\\n");
  }
  return 0;
}

4. 试写加密程序,对字符按如下规则加密:如果英文字母则大写变小写、小写变大写,并且a=>c、b=>d 、···、x=>z、y=>a、z=>b,对非英文字母保持不变。

#include "stdio.h"

char encode(char c){

  // 字符换位
  if(c=='y' || c=='Y' || c=='z' || c=='Z'){
    c-=24;
  }else if(c>='a'&& c<='z'|| c>='A'&&c<='Z'){
    c+=2;
  };

  // 转大写
  if(c>='a'&&c<='z'){
    c-=32;
  // 转小写
  }else if(c>='A' && c<='Z'){
    c+=32;
  };

  return c;

};

int main(){
  char c;
  printf("请输入一个字符串:");
  while ((c=getchar())!='\\n'){
    printf("%c",encode(c));
  };
  printf("\\n");
  return 0;
}


 5. 键盘输入N个战士的身高,用比较法将其升序排序。

#include "stdio.h"

void main(){
  int a[10];
  printf("请输入10个数字,程序会自动进行排序,输入一个回车表示确认");
  for(int i=0;i<10;i++){
    scanf("%d",&a[i]);
  };
  for(int i=0;i<10;i++){
    // 只和后面的比
    for(int j=1;j<10-i;j++){
      if(a[i]>a[i+j]){
        int temp = a[i];
        a[i]=a[i+j];
        a[i+j]=temp;
      }
    }
  };
  printf("由小到大的排序结果是\\n");
  for(int k=0;k<10;k++){
    // 注意用逗号来隔开
    printf("%d,",a[k]);
  }
}

6. 编写一个C程序,分解出6378的每一个位数。

#include "stdio.h"

void main() {
  int  x= 6378;
  int a,b,c,d;
  a = x % 10000 / 1000;
  b = x % 1000 / 100;
  c = x % 100 / 10;
  d = x % 10;
  printf("千位:%d\\n", a);
  printf("百位:%d\\n", b);
  printf("十位:%d\\n", c);
  printf("个位:%d\\n", d);
}

上面这个程序的逻辑比较直观,还有一种更经典的解法如下。

#include "stdio.h"
void main(){
  int n = 6378;
  while(n){
    printf("%d",n%10);
    n/=10;
    printf("\\n");
  }	
}

7. 用数组的方法输出斐波那契数列 1,2,3,5,8,13,21,34,55,89,.......写C程序,输出该数列的前N项。

#include "stdio.h"
#include "stdlib.h"
void main(){
  int arr[100]={1,1};
  printf("请输入数值N,N的值小于等于100,程序将列出前N项斐波那契数值:");
  int n;
  scanf("%d",&n);
  
  if(n>100){
    printf("输入的数值不能大于100");
    exit(1);
  };

  for(int i=2;i<n;i++){
    arr[i]=arr[i-1]+arr[i-2];
  };
  printf("N项斐波那契序列的值为:\\n");
  for(int a=0;a<n;a++){
    printf("%d\\n",arr[a]);
  }

}

8. 迭代法求某正数n的平方根。已知求平方根的迭代公式为:x1=(x2+n/x2)/2

#include "math.h"
#include "stdio.h"
#include "stdlib.h"

void main(){
  printf("请输入N的值,将求出N的平方根:");
  double n,x1,x2;
  scanf("%lf",&n);
  x2=1.0;
  while(true){
    x1 = x2;
    x2 = (x1 +n/x1)/2.0;
    if(fabs(x1-x2)<0.0001){
      printf("%.3f",x2);
      break;
    }
  }
}

以上是关于C语言经典算法题的主要内容,如果未能解决你的问题,请参考以下文章

经典C语言面试算法题

C语言经典算法题

C语言经典编程题(上)

C语言经典算法题

有啥经典的c语言算法书推荐一下吗

有哪位大虾帮忙做下我的C语言的题阿