当前位置:   article > 正文

嵌入式面试题 C/C++ 经典面试算法题总结_c++ 面试算法题

c++ 面试算法题

1.打印杨辉三角

  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3
  4. 4 int main()
  5. 5 {
  6. 6 int x;
  7. 7 int a[100][100];
  8. 8 printf("输入行数\n");
  9. 9 scanf("%d",&x);
  10. 10 for(int i = 0;i<x;i++)
  11. 11 {
  12. 12 for(int j = 0;j<x;j++)
  13. 13 {
  14. 14 a[i][j] = 0;
  15. 15 }
  16. 16 }
  17. 17
  18. 18 for(int i = 0;i<x;i++)
  19. 19 {
  20. 20 a[i][0] = 1;
  21. 21 }
  22. 22
  23. 23 for(int i = 1;i<x;i++)
  24. 24 {
  25. 25 for(int j = 1;j<=i;j++)
  26. 26 {
  27. 27 a[i][j] = a[i-1][j] + a[i-1][j-1];
  28. 28 }
  29. 29 }
  30. 30
  31. 31 for(int i = 0;i<x;i++)
  32. 32 {
  33. 33 for(int j = 0;j<=i;j++)
  34. 34 {
  35. 35 printf("%d ",a[i][j]);
  36. 36 }
  37. 37 printf("\n");
  38. 38 }
  39. 39
  40. 40 return 0;
  41. 41 }

2.斐波那契数列 

  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3
  4. 4 int func(int n)
  5. 5 {
  6. 6 if(0 == n) return 0;
  7. 7 if(1 == n) return 1;
  8. 8 else
  9. 9 return func(n-2)+func(n-1);
  10. 10 }
  11. 11
  12. 12 int main()
  13. 13 {
  14. 14 int n;
  15. 15 scanf("%d",&n);
  16. 16 printf("%d\n",func(n));
  17. 17
  18. 18 return 0;
  19. 19 }

3.请使用递归算法编写求N的阶乘函数

  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3
  4. 4 int func(int n)
  5. 5 {
  6. 6 if(1 == n) return 1;
  7. 7 return n * func(n-1);
  8. 8 }
  9. 9
  10. 10 int main()
  11. 11 {
  12. 12 int n;
  13. 13 scanf("%d",&n);
  14. 14 printf("%d\n",func(n));
  15. 15
  16. 16 return 0;
  17. 17 }

4.输入两个正整数 m 和 n,求其最大公约数和最小公倍数

  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3
  4. 4 int main()
  5. 5 {
  6. 6 int x,y,z,j;
  7. 7 scanf("%d%d",&x,&y);
  8. 8 if(x>y)
  9. 9 {
  10. 10 z = x;
  11. 11 }else{
  12. 12 z = y;
  13. 13 }
  14. 14
  15. 15 for(int i = z;i>0;i--)
  16. 16 {
  17. 17 j = i;
  18. 18 if(0 == x%i && 0 == y%i)
  19. 19 {
  20. 20 break;
  21. 21 }
  22. 22 }
  23. 23 printf("最大公约数为:%d\n",j);
  24. 24 printf("最小公倍数为:%d\n",(x*y)/j);
  25. 25
  26. 26 return 0;
  27. 27 }

5.判断从101到200间有多少个素数,并输出

  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3
  4. 4 int main()
  5. 5 {
  6. 6 int i,j;
  7. 7 for(i = 101;i<=200;i++)
  8. 8 {
  9. 9 for(j = 2;j<i;j++)
  10. 10 {
  11. 11 if(0 == i%j)
  12. 12 {
  13. 13 break;
  14. 14 }else{
  15. 15 if(j == i-1)
  16. 16 {
  17. 17 printf("%d\n",i);
  18. 18 }
  19. 19 }
  20. 20 }
  21. 21 }
  22. 22
  23. 23 return 0;
  24. 24 }

6.写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写

  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3
  4. 4 int main()
  5. 5 {
  6. 6 int len,cout = 0;
  7. 7 char a[] = "ABCDEFGAa";
  8. 8 char b = 'a';
  9. 9 len = strlen(a);
  10. 10 for(int i = 0;i<len;i++)
  11. 11 {
  12. 12 if(a[i] == b || a[i]-32 == b || a[i]+32 == b)
  13. 13 {
  14. 14 cout++;
  15. 15 }
  16. 16 }
  17. 17 printf("%d\n",cout);
  18. 18
  19. 19 return 0;
  20. 20 }

 7.打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。

例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。

  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3
  4. 4 int main()
  5. 5 {
  6. 6 int i,a,b,c,cout = 0;
  7. 7 printf("打印100-999水仙花个数\n");
  8. 8 for(i = 100;i<999;i++)
  9. 9 {
  10. 10 a = i/100;
  11. 11 b = i/10 %10;
  12. 12 c = i%10;
  13. 13 if(i == (a*a*a)+(b*b*b)+(c*c*c))
  14. 14 {
  15. 15 cout++;
  16. 16 printf("%d ",i);
  17. 17 }
  18. 18 }
  19. 19 printf("水仙花个数为:%d\n",cout);
  20. 20
  21. 21 return 0;
  22. 22 }

8.输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3
  4. 4 int main()
  5. 5 {
  6. 6 int i = 0,b = 0,c = 0,d = 0,e = 0;
  7. 7 char a[] = "ASsfDGG& adS A18";
  8. 8 while(a[i] != '\0')
  9. 9 {
  10. 10 if(a[i] >= '1' && a[i] <= '9')
  11. 11 {
  12. 12 b++;
  13. 13 }
  14. 14 else if(a[i] >= 'a' && a[i] <= 'z' || a[i] >= 'A' && a[i] <= 'Z')
  15. 15 {
  16. 16 c++;
  17. 17 }
  18. 18 else if(a[i] == ' ')
  19. 19 {
  20. 20 d++;
  21. 21 }
  22. 22 else
  23. 23 {
  24. 24 e++;
  25. 25 }
  26. 26 i++;
  27. 27 }
  28. 28 printf("数字的个数为:%d,字母的个数为:%d,空格的个数为:%d,其他符号的个数为:%d",b,c,d,e);
  29. 29
  30. 30 return 0;
  31. 31 }

9.输出9*9口诀。

  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3
  4. 4 int main()
  5. 5 {
  6. 6 int i,j,num;
  7. 7 printf("输出9*9乘法口诀\n");
  8. 8 for(i = 1;i<=9;i++)
  9. 9 {
  10. 10 for(j = 1;j<=i;j++)
  11. 11 {
  12. 12 num = i * j;
  13. 13 printf("%d * %d = %d ",i,j,num);
  14. 14 }
  15. 15 printf("\n");
  16. 16 }
  17. 17
  18. 18 return 0;
  19. 19 }

10.用*打印菱形图案

  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3
  4. 4 int main()
  5. 5 {
  6. 6 int i,j,k;
  7. 7 for(i = 1;i<=4;i++)
  8. 8 {
  9. 9 for(j = 0;j<4-i;j++)
  10. 10 {
  11. 11 printf(" ");
  12. 12 }
  13. 13 for(k = 0;k<(2*i)-1;k++)
  14. 14 {
  15. 15 printf("*");
  16. 16 }
  17. 17 printf("\n");
  18. 18 }
  19. 19
  20. 20 for(i = 1;i<=3;i++)
  21. 21 {
  22. 22 for(j = 0;j<i;j++)
  23. 23 {
  24. 24 printf(" ");
  25. 25 }
  26. 26 for(k = 0;k<7-(2*i);k++)
  27. 27 {
  28. 28 printf("*");
  29. 29 }
  30. 30 printf("\n");
  31. 31 }
  32. 32
  33. 33 return 0;
  34. 34 }

 11.题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?有多少个。

  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3
  4. 4 int main()
  5. 5 {
  6. 6 int a[] = {1,2,3,4};
  7. 7 int i,j,k,cout = 0;
  8. 8 for(i = 0;i<4;i++)
  9. 9 {
  10. 10 for(j = 0;j<4;j++)
  11. 11 {
  12. 12 for(k = 0;k<4;k++)
  13. 13 {
  14. 14 if(i != j && j != k && i != k)
  15. 15 {
  16. 16 printf("%d%d%d ",a[i],a[j],a[k]);
  17. 17 cout++;
  18. 18 }
  19. 19 }
  20. 20 }
  21. 21 printf("\n");
  22. 22 }
  23. 23 printf("可以组成%d个互不相同且无重复数字的三位数\n",cout);
  24. 24 return 0;
  25. 25 }

 12.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

  1. 1 #include<stdio.h>
  2. 2
  3. 3 int main()
  4. 4 {
  5. 5 int a,n,s = 0,b;
  6. 6 printf("请输入相加个数n和加数a\n");
  7. 7 scanf("%d%d",&a,&n);
  8. 8 printf("s = %d ",a);
  9. 9 b = a;
  10. 10 for(int i = 0;i<n-1;i++)
  11. 11 {
  12. 12 s = s + a;
  13. 13 a = b + (a * 10);
  14. 14 printf("* %d ",a);
  15. 15 }
  16. 16 s = s + a;
  17. 17 printf(" = %d",s);
  18. 18
  19. 19 return 0;
  20. 20 }

13.一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程 找出1000以内的所有完数。

https://blog.csdn.net/m0_55028858/article/details/125577635

  1. 1 #include<stdio.h>
  2. 2
  3. 3 int main()
  4. 4 {
  5. 5 int s;
  6. 6 for(int i = 2;i<1000;i++)
  7. 7 {
  8. 8 s = 0;
  9. 9 for(int j = 1;j<i-1;j++)
  10. 10 {
  11. 11 if(0 == i%j)
  12. 12 {
  13. 13 s = s + j;
  14. 14 }
  15. 15 }
  16. 16 if(i == s)
  17. 17 {
  18. 18 printf("%d是完数\n",s);
  19. 19 }
  20. 20 }
  21. 21 return 0;
  22. 22 }

14.一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

https://blog.csdn.net/qq_45385706/article/details/110697089

  1. 1 #include<stdio.h>
  2. 2
  3. 3 int main()
  4. 4 {
  5. 5 double s = 100,h = s/2,k = 0;
  6. 6 for(int i = 0;i<9;i++)
  7. 7 {
  8. 8 k = k + (2 * h);
  9. 9 h = h/2;
  10. 10 }
  11. 11 k = k + s;
  12. 12 printf("总共经过%lf米,第10次反弹的高度为%lf",k,h);
  13. 13
  14. 14 return 0;
  15. 15 }

15.猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。

以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

  1. 1 #include<stdio.h>
  2. 2
  3. 3 int main()
  4. 4 {
  5. 5 int y = 1;
  6. 6 for(int i = 0;i<9;i++)
  7. 7 {
  8. 8 y = (y + 1) * 2;
  9. 9 }
  10. 10 printf("第一天总共有%d颗桃子\n",y);
  11. 11
  12. 12 return 0;
  13. 13 }

16.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。

  1. 1 #include<stdio.h>
  2. 2
  3. 3 int main()
  4. 4 {
  5. 5 double x = 2,y = 1,s = 0,x1 = 0;
  6. 6 for(int i = 0;i<20;i++)
  7. 7 {
  8. 8 s = s + (x/y);
  9. 9 x1 = x;
  10. 10 x = x + y;
  11. 11 y = x1;
  12. 12 }
  13. 13 printf("前20项的和为%lf\n",s);
  14. 14
  15. 15 return 0;
  16. 16 }

17.一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同

https://blog.csdn.net/daonanya/article/details/123506362

  1. 1 #include<stdio.h>
  2. 2
  3. 3 int main()
  4. 4 {
  5. 5 int a = 12321,b,c,d,e;
  6. 6 b = a/10000;
  7. 7 c = (a%10000)/1000;
  8. 8 d = (a%100)/10;
  9. 9 e = a%10;
  10. 10 if(b == e && c == d)
  11. 11 {
  12. 12 printf("是回文数\n");
  13. 13 }
  14. 14 else
  15. 15 {
  16. 16 printf("不是回文数\n");
  17. 17 }
  18. 18
  19. 19 return 0;
  20. 20 }

 18.两数之和

https://blog.csdn.net/azulgrana02/article/details/109644046

  1. 1 #include<iostream>
  2. 2 #include<vector>
  3. 3 #include<unordered_map>
  4. 4 using namespace std;
  5. 5
  6. 6 class node{
  7. 7 public:
  8. 8 vector<int> twosun(vector<int>& nums,int target)
  9. 9 {
  10. 10 unordered_map<int,int> record;
  11. 11 for(int i = 0;i<nums.size();i++){
  12. 12 int num = target - nums[i];
  13. 13 if(record.find(num) != record.end()){
  14. 14 return {record[num],i};
  15. 15 }
  16. 16 record[nums[i]] = i;
  17. 17 }
  18. 18 return {-1,-1};
  19. 19 }
  20. 20 };
  21. 21
  22. 22 int main()
  23. 23 {
  24. 24 node n;
  25. 25 vector<int> cur;
  26. 26 vector<int> nums = {2,7,11,15};
  27. 27 cur = n.twosun(nums,9);
  28. 28 for (auto i : cur)
  29. 29 cout << i << endl;
  30. 30
  31. 31 return 0;
  32. 32 }

19.整数反转

  1. 1 #include <iostream>
  2. 2 #include <vector>
  3. 3 using namespace std;
  4. 4
  5. 5 class node{
  6. 6 public:
  7. 7 int reverse(int x){
  8. 8 int ans = 0;
  9. 9 while(x){
  10. 10 ans = ans*10 + x%10;
  11. 11 x /= 10;
  12. 12 }
  13. 13 return ans;
  14. 14 }
  15. 15 };
  16. 16
  17. 17 int main()
  18. 18 {
  19. 19 node n;
  20. 20 cout << n.reverse(-123) << endl;
  21. 21
  22. 22 return 0;
  23. 23 }

 20.合并区间(力扣56题)

  1. 1 #include <iostream>
  2. 2 #include <vector>
  3. 3 #include<algorithm>
  4. 4 using namespace std;
  5. 5
  6. 6 class node{
  7. 7 public:
  8. 8 vector<vector<int>> merge(vector<vector<int>>& cur){
  9. 9 vector<vector<int>> ans;
  10. 10 sort(cur.begin(),cur.end());
  11. 11 int strat = cur[0][0],end = cur[0][1];
  12. 12 for(int i = 1;i<cur.size();i++){
  13. 13 if(cur[i][0]>end){
  14. 14 ans.push_back({strat,end});
  15. 15 strat = cur[i][0];
  16. 16 end = cur[i][1];
  17. 17 }else{
  18. 18 end = max(end,cur[i][1]);
  19. 19 }
  20. 20 }
  21. 21 ans.push_back({strat,end});
  22. 22 return ans;
  23. 23 }
  24. 24 };
  25. 25
  26. 26 int main()
  27. 27 {
  28. 28 node n;
  29. 29 vector<vector<int>> top;
  30. 30 vector<vector<int>> tem;
  31. 31 tem.push_back({1,3});
  32. 32 tem.push_back({2,6});
  33. 33 tem.push_back({8,10});
  34. 34 tem.push_back({15,18});
  35. 35 top = n.merge(tem);
  36. 36 int x = top.size(),y = top[0].size();
  37. 37 for(int i = 0;i<x;i++){
  38. 38 for(int j = 0;j<y;j++){
  39. 39 cout << top[i][j] << " ";
  40. 40 }
  41. 41 cout << endl;
  42. 42 }
  43. 43
  44. 44 return 0;
  45. 45 }

 21.插入区间(力扣57题)

  1. 1 #include <iostream>
  2. 2 #include <vector>
  3. 3 #include<algorithm>
  4. 4 using namespace std;
  5. 5
  6. 6 class node{
  7. 7 public:
  8. 8 vector<vector<int>> insert(vector<vector<int>>& a,vector<int>& b){
  9. 9 vector<vector<int>> ans;
  10. 10 int n = a.size(),i = 0;
  11. 11 while(i<n && a[i][1]<b[0]){
  12. 12 ans.push_back(a[i++]);
  13. 13 }
  14. 14 if(i<n){
  15. 15 b[0] = min(a[i][0],b[0]);
  16. 16 while(i<n && a[i][0]<=b[1]){
  17. 17 b[1] = max(a[i++][1],b[1]);
  18. 18 }
  19. 19 }
  20. 20 ans.push_back(b);
  21. 21 while(i<n){
  22. 22 ans.push_back(a[i++]);
  23. 23 }
  24. 24 return ans;
  25. 25 }
  26. 26 };
  27. 27
  28. 28 int main()
  29. 29 {
  30. 30 node n;
  31. 31 vector<int> tur ={2,5};
  32. 32 vector<vector<int>> tem;
  33. 33 tem.push_back({1,3});
  34. 34 tem.push_back({6,9});
  35. 35 vector<vector<int>> top;
  36. 36 top = n.insert(tem,tur);
  37. 37 int x= top.size(),y = top[0].size();
  38. 38 for(int i = 0;i<x;i++){
  39. 39 for(int j = 0;j<y;j++){
  40. 40 cout << top[i][j] << " ";
  41. 41 }
  42. 42 cout << endl;
  43. 43 }
  44. 44
  45. 45 return 0;
  46. 46 }

 22.加一(力扣66题)给定一个数组,在原数组的基础上加一、

  1. 1 #include <iostream>
  2. 2 #include <vector>
  3. 3 using namespace std;
  4. 4
  5. 5 class node{
  6. 6 public:
  7. 7 vector<int> piusone(vector<int>& cur){
  8. 8 for(int i = cur.size()-1;i>=0;i++){
  9. 9 if(cur[i]<9){
  10. 10 cur[i]++;
  11. 11 break;
  12. 12 }else{
  13. 13 cur[i] = 0;
  14. 14 if(i==0){
  15. 15 cur.insert(cur.begin(),1);
  16. 16 }
  17. 17 }
  18. 18 }
  19. 19 return cur;
  20. 20 }
  21. 21 };
  22. 22
  23. 23 int main()
  24. 24 {
  25. 25 node n;
  26. 26 vector<int> tem;
  27. 27 vector<int> top = {1,2,3};
  28. 28 tem = n.piusone(top);
  29. 29 for(int i = 0;i<tem.size();i++){
  30. 30 cout << tem[i] << " ";
  31. 31 }
  32. 32
  33. 33 return 0;
  34. 34 }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/868849
推荐阅读
相关标签
  

闽ICP备14008679号