C/C++ 经典面试算法题

1.打印杨辉三角

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

2.斐波那契数列 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

9.输出9*9口诀。

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

10.用*打印菱形图案

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

 18.两数之和

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

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

19.整数反转

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

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

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

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

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

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/148240.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

postgresql新特性之Merge

postgresql新特性之Merge 创建测试表测试案例 创建测试表 create table cps.public.test(id integer primary key,balance numeric,status varchar(1));测试案例 官网介绍 merge into test t using ( select 1 id,0 balance,Y status) s on(t.id s.id) -- 当匹配上了,statu…

TempleteMethod

TempleteMethod 动机 在软件构建过程中&#xff0c;对于某一项任务&#xff0c;它常常有稳定的整体操作结构&#xff0c;但各个子步骤却有很多改变的需求&#xff0c;或者由于固有的原因 &#xff08;比如框架与应用之间的关系&#xff09;而无法和任务的整体结构同时实现。如…

嵌入式Linux应用开发-驱动大全-同步与互斥①

嵌入式Linux应用开发-驱动大全-同步与互斥① 第一章 同步与互斥①1.1 内联汇编1.1.1 C语言实现加法1.1.2 使用汇编函数实现加法1.1.3 内联汇编语法1.1.4 编写内联汇编实现加法1.1.5 earlyclobber的例子 1.2 同步与互斥的失败例子1.2.1 失败例子11.2.2 失败例子21.2.3 失败例子3…

使用CrawlSpider爬取全站数据。

CrawpSpider和Spider的区别 CrawlSpider使用基于规则的方式来定义如何跟踪链接和提取数据。它支持定义规则来自动跟踪链接&#xff0c;并可以根据链接的特征来确定如何爬取和提取数据。CrawlSpider可以对多个页面进行同样的操作&#xff0c;所以可以爬取全站的数据。CrawlSpid…

【2023年11月第四版教材】第17章《干系人管理》(合集篇)

第17章《干系人管理》&#xff08;合集篇&#xff09; 1 章节内容2 管理基础3 管理过程3.1 管理的过程★★★ &#xff08;22上44&#xff09;3.2 管理ITTO汇总★★★ 4 过程1-识别干系人4.1 数据收集★★★4.3数据分析4.4 权力利益方格4.5 数据表现&#xff1a;干系人映射分析…

springmvc中DispatcherServlet关键对象

以下代码为 spring boot 2.7.15 中自带的 spring 5.3.29 RequestMappingInfo 请求方法相关信息封装&#xff0c;对应的信息解析在 RequestMappingHandlerMapping 的 createRequestMappingInfo() 中实现。 对于 RequestMapping 赋值的相关信息进行解析 protected RequestMappi…

零基础Linux_11(进程)进程程序替换+实现简单的shell

目录 1. 进程程序替换 1.1 程序替换原理 1.2 execl 接口 1.3 execv execlp execvp 1.4 exec 调各种程序 1.5 execle 接口 2. 实现简单的shell 2.1 打印提示和获取输入 2.2 拆开输入的命令和选项 2.3 创建进程和程序替换执行命令 2.4 内建命令实现路径切换 2.5 my…

创建GCP service账号并管理权限

列出当前GCP项目的所有service account 我们可以用gcloud 命令 gcloud iam service-accounts list gcloud iam service-accounts list DISPLAY NAME EMAIL DISABLED terraform …

自动混剪多段视频、合并音频、添加文案的技巧分享

在如今的社交媒体时代&#xff0c;视频的重要性越来越被人们所重视。许多人喜欢记录生活中的美好瞬间&#xff0c;并将其制作成视频分享给朋友和家人。然而&#xff0c;对于那些拍摄了大量视频的人来说&#xff0c;一个一个地进行剪辑和合并可能是一项令人头痛的任务。但是&…

1.6 计算机网络的性能

思维导图&#xff1a; 1.6.1 计算机网络的性能指标 前言&#xff1a; 我的理解&#xff1a; 这段前言主要介绍了关于计算机网络性能的两个方面的讨论。首先&#xff0c;计算机网络的性能可以通过一些重要的性能指标来衡量。但除了这些指标之外&#xff0c;还有一些非性能特征…

【计算机网络】因特网中的电子邮件

文章目录 简单邮件传送协议SMTP邮件访问协议POP3IMAPHTTP 参考资料 电子邮件为异步通信媒介 因特网电子邮件系统 电子邮件系统的三个构件&#xff1a;用户代理、邮件服务器、邮件发送和读取协议 用户代理 User Agent 即UA 电子邮件客户端软件&#xff0c;用户与电子邮件系统的接…

代码随想录算法训练营第五十六天 | 动态规划 part 14 | 1143.最长公共子序列、1035.不相交的线、53. 最大子序和(dp)

目录 1143.最长公共子序列思路代码 1035.不相交的线思路代码 53. 最大子序和&#xff08;dp&#xff09;思路代码 1143.最长公共子序列 Leetcode 思路 本题和718. 最长重复子数组 区别在于这里不要求是连续的了&#xff0c;但要有相对顺序&#xff0c;即&#xff1a;“ace” …

Moonbeam Ignite强势回归

参与Moonbeam上最新的流动性计划 还记得新一轮的流动性激励计划吗&#xff1f;Moonbeam Ignite社区活动带着超过300万枚GLMR奖励来啦&#xff01;体验新项目&#xff0c;顺便薅一把GLMR羊毛。 本次Moonbeam Ignite活动的参与项目均为第二批Moonbeam生态系统Grant资助提案中获…

语义分割 Semantic Segmentation

之前了解过语义分割的内容&#xff0c;感觉可以做好多东西&#xff0c;然后就抽空学习了一下&#xff0c;这里记录一下方便以后查阅&#xff0c;这篇文章可能也会随着学习的深入不断更新。 语义分割 Semantic Segmentation 一些基本概念几种语义分割算法Fully Convolutional Ne…

【单片机】16-LCD1602和12864显示器

1.LCD显示器相关背景 1.LCD简介 &#xff08;1&#xff09;显示器&#xff0c;常见显示器&#xff1a;电视&#xff0c;电脑 &#xff08;2&#xff09;LCD&#xff08;Liquid Crystal Display&#xff09;&#xff0c;液晶显示器&#xff0c;原理介绍 &#xff08;3&#xff…

【分布式计算】二、架构 Architectures

1.中心化架构&#xff08;Centralized Architectures&#xff09; 1.1.经典C/S模型 服务器&#xff1a;一个或多个进程提供服务 客户端&#xff1a;一个或多个进程使用服务 客户端和服务器可以在不同的机器上 客户端遵循请求/回复模型 1.2.传统三层视图 用户界面层&#x…

JUC中的设计模式

文章目录 1. 终止模式之两阶段终止模式 1. 终止模式之两阶段终止模式 需求&#xff1a;用一个线程每两秒检测***状态&#xff0c;当不想检测时&#xff0c;用另一个线程将其停止 在一个线程 T1 中如何“优雅”终止线程 T2&#xff1f;这里的【优雅】指的是给 T2 一个料理后事…

910数据结构(2019年真题)

算法设计题 问题1 有一种排序算法叫做计数排序。这种排序算法对一个待排序的表&#xff08;采用顺序存储&#xff09;进行排序&#xff0c;并将排序结果存放到另一个新的表中。必须注意的是&#xff0c;表中所有待排序的关键字互不相同&#xff0c;计数排序算法针对表中的每个…

【AI处理器组合】python实现-附ChatGPT解析

1.题目 AI处理器组合 知识点数组 时间限制:1s 空间限制: 256MB 限定语言:不限 题目描述: 某公司研发了一款高性能AI处理器。每台物理设备具备8颗AI处理器,编号分别为0、1、2、3、4、5、6、7。编号0-3的处理器处于同一个链路中,编号4-7的处理器处于另外一个链路中,不通链路中…

10.03

代码 #include <iostream>using namespace std; class cz { private:int num1; //实部int num2; //虚部 public:cz(){}cz(int a,int b):num1(a),num2(b){}cz(const cz &other):num1(other.num1),num2(other.num2){}~cz(){}const cz operator(const cz &othe…