讯飞笔试算法题-程序员宅基地

技术标签: 算法  c++  开发语言  

题目描述: 一个m*n的矩阵,矩阵的数值代表礼物的价值,从矩阵的左上角出发,并且每次向右或者向下(不能回退) 移动一格,直到到达矩阵的右下角。计算所走的路径礼物价值加起来最大是多少?

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
    void maxValue(vector<vector<int>> &arr, int m, int n)
    {
        int value = 0;
        if (m <= 0 || n <= 0)
            cout << value << endl;
        else
        {
            for (int i = 1; i < m; i++) //第1列
                arr[i][0] += arr[i - 1][0];
            for (int j = 1; j < n; j++) //第1行
                arr[0][j] += arr[0][j - 1];
            for (int i = 1; i < m; i++) //中间的值
                for (int j = 1; j < n; j++)
                    arr[i][j] += max(arr[i - 1][j], arr[i][j - 1]); //左边或者上边最大的值。
            cout << arr[m - 1][n - 1] << endl;
        }
    }
};
int main()
{
    Solution sol;
    int m, n;
    cin >> m;
    if (cin.get() == ',')  //cin.get()用来接收字符
        cin >> n;
    vector<vector<int>> arr(m, vector<int>(n, 0));
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            cin >> arr[i][j];
    sol.maxValue(arr, m, n);
    system("pause");
    return 0;
}
  • 题目描述: 设计一个递归实现将一个正整数分解质因数,如50=255,程序打印“255”,每个素因子之间使用*隔开。如果这个数本身是素数,则直接输出该数。
    #include <iostream>
    using namespace std;
    
    class Solution
    {
    public:
        void toZhiyinshu(int n)
        {
            int i, j;
            for (i = 2; i <= n; i++) //判断是不是质数;
            {
                if (n % i == 0) //是质数;
                {
                    j = n / i;
                    if (j == 1)
                    {
                        cout << i;
                        return;
                    }
                    else
                    {
                        cout << i << "*";
                        toZhiyinshu(j);
                        break;
                    }
                }
            }
            cout << endl;
        }
    };
    int main()
    {
        Solution sol;
        int n;
        cin >> n;
        sol.toZhiyinshu(n);
        system("pause");
        return 0;
    }
    

    题目描述:定义一个n*m的数字矩阵,找出不同行不同列的两个数的乘积的最大值。

    #include <iostream>
    using namespace  std;
     
    int main()
    {
        string str;
        getline(cin,str);
        int n;//move bits numbers
        cin>>n;
        int m=str.size();
        if(n<=0){
            cout<<str;
        }
        if(n>m){
            n%=m;
        }
        string res="";
        for(int i=n;i<m;i++){
            res+=str[i];
        }
        for(int i=0;i<n;i++)
        {
            res+=str[i];
        }
       cout<<res;
       return 0;
    }

    题目描述:输入一个数N,求数的二进制中1的个数

    //写一个函数返回参数的二进制中1的个数
    #include<stdio.h>
    #include<stdlib.h>
    int count_one_bits(unsigned int value)
    {
        int count = 0;
        while (value != 0)
        {
            if (value % 2 == 1)
            {
                count++;
            }
            value = value >>1;
        }
        return count;
    }
    int main()
    {
        int num;
        int ret;
        printf("请输入一个大于0的数\n");
        scanf("%d", &num);
        ret=count_one_bits(num);
        printf("%d", ret);
        system("pause");
        return 0;
    }

    题目描述:数组的排序(时间复杂度最小),应该是让你用冒泡或者选择排序。

    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
     
    void sortA1(int a[], int length){
        int i, j, temp;
        for(i = 0; i < length; ++i){
            for(j = i + 1; j < length; ++j){
                if(a[j] < a[i]){    //如果后一个元素小于前一个元素则交换
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }
     
    void printA1(int a[], int length){
     
        int i;
        for(i = 0; i < length; ++i){
            printf("%d,", a[i]);
        }
        printf("\n");
    }
     
    void sortA2(int a[], int length){
        int i, j, temp;
        for(i = 0; i < length; ++i){
            for(j = length - 1; j > i; --j){
                if(a[j] > a[j - 1]){
                    temp = a[j];
                    a[j] = a[j - 1];
                    a[j - 1] = temp;
                }
            }
        }
    }
     
    int main(){
     
        int length = 0;
        int a[] = {12, 43, 8, 50, 100, 52,0};
        length = sizeof(a) / sizeof(a[0]);
        printf("排序前\n");
        printA1(a, length);
        sortA1(a, length);
        printf("选择排序后\n");
        printA1(a, length);
        sortA2(a, length);
        printf("冒泡排序后\n");
        printA1(a, length);
        system("pause");
    }

    题目描述:字符串左旋(输入 1234abcd , 左旋3,输出 4abcd123)

    void swap(char *start, char *end)
    {
        while (start < end)
        {
            *start ^= *end;
            *end ^= *start;
            *start ^= *end;
            start++, end--;
        }
    }
     
    void reverse_left_2(char *str, int n, int len)
    {
        char *mid = NULL;//定义一个指针,将指向左旋分段点
     
        n %= len;//判断左旋的有效次数
        mid = str + n - 1;//指向分段点的最后一个字符
        swap(str, mid);//逆置前一段字符串
        swap(mid + 1, str + len - 1);//逆置后一段字符串
        swap(str, str + len - 1);//整个字符串逆置
    }
     
    int main()
    {
        char str[] = "abcd1234";
        int n = 0, len = strlen(str);
     
        printf("please enter->");
        scanf("%d", &n);//输入左旋的次数
        printf("before reverse_left string is :%s\n", str);
        reverse_left_2(str, n, len);
        printf("reverse_left string is :%s\n", str);
     
        system("pause");
        return 0;
    }

    题目描述:对于一个给定的字符序列S,请你把其左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。

    void swap1(char &a,char &b)//位运算实现交换字符
    {
    	a=a^b;
    	b=a^b;
    	a=a^b;
    }
    int strlength(char *str)//返回字符串长度
    {
    	int length=0;
    	if(str!=NULL)
    	{
    		while (*str!='\0')
    		{
    			length++;
    			str++;
    		}
    	}
    	return length;
    }
    void reverse(char *str,char *begin,char *end)//反转指定区间的字符串
    {
    	while(begin<end)
    	{
    		swap1(*begin,*end);
    		begin++;
    		end--;
    	}
    }
    void leftRotate(char *str,int n)//将前n个字符移动到后面
    {
    	int length=strlength(str);
    	if(str!=NULL&&n>0&&n<length)
    	{
    		char *firstbegin=str;
    		char *firstend=str+n-1;
    		char *secbegin=str+n;
    		char *secend=str+length-1;
    		reverse(str,firstbegin,firstend);//先反转第一段字符串
    		reverse(str,secbegin,secend);//再反转第二段字符串
    		reverse(str,firstbegin,secend);//反转整个字符串
    	}
    }

    问题描述:两个字符串的最长公共子串

    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
        string a, b;
        while (cin >> a >> b)
        {
            if (a.size() > b.size())
                swap(a, b);
            string str_m;//存储最长公共子串
            for (int i = 0; i < a.size(); i++)
            {
                for (int j = i; j < a.size(); j++)
                {
                    string temp = a.substr(i, j - i + 1);
                        if (int(b.find(temp))<0)
                        break;
                    else if (str_m.size() < temp.size())
                        str_m = temp;
                }
            }
            cout << str_m << endl;
        }
        return 0;
    }

    问题描述:三角形

    class Solution {
    public:
        int triangleNumber(vector<int>& nums) {
            int count = 0, size = nums.size();
            sort(nums.begin(), nums.end());
            for (int i = size - 1; i >= 2; i--) {
                int left = 0, right = i - 1;
                while(left < right) {
                    if (nums[left] + nums[right] > nums[i]) {
                        count += (right - left);
                        right--;
                    }
                    else {
                        left++;
                    }
                }
            }
            return count;
        }
    };
    

    问题描述:排序然后看相邻的元素之差是否大于4

    n=int(input())
    a=[]
    for i in range(n):
        a.append(int(input()))
    a.sort()
    flag=True
    for i in range(1,len(a)):
        if a[i]-a[i-1]>4:
            flag=False
            break
    a=[str(v) for v in a]
    print(' '.join(a))
    if flag:
        print(1)
    else:
        print(0)

    问题描述:给出8个数据,前四个数据代表第一个矩形的对角线上的点坐标,后四个数据代表第二个矩形的对角线上的点坐标 判断两矩形能否相交,能则输出1,否则输出0

    #include<iostream>
    #include<string>
    using namespace std;
    int main() {
    	int n[4],n2[4];
    	for (int i = 0; i < 4; i++) {
    		cin >> n[i];
    	}
    	for (int i = 0; i < 4; i++) {
    		cin >> n2[i];
    	}
    	int x1[2],y1[2];
    	int x2[2], y2[2];
    	x1[0] = n[0] < n[2] ? n[0]: n[2];//x最小
        x1[1]= n[0] > n[2] ? n[0] : n[2];//x最大
    	y1[0] = n[1] < n[3] ? n[1] : n[3];//y最小
    	y1[1] = n[1] > n[3] ? n[1] : n[3];//y最大
    	x2[0] = n2[0] < n2[2] ? n2[0] : n2[2];//x最小
    	x2[1] = n2[0] > n2[2] ? n2[0] : n2[2];//x最大
    	y2[0] = n2[1] < n2[3] ? n2[1] : n2[3];//y最小
    	y2[1] = n2[1] > n2[3] ? n2[1] : n2[3];//y最大
    	bool flag=false;
    	for (int i = 0; i < 2; i++) {
    		if (x2[i] >= x1[0] && x2[i] <= x1[1]) {
    			for (int j = 0; j < 2; j++) {
    				if (y2[j] >= y1[0] && y2[j] <= y1[1])
    					flag = true;
    			}
    		}
    	}
    	if (flag) {
    		cout << "1";
    	}
    	else {
    		cout << "0";
    	}
    	return 0;
    }
    

    题目描述:输入任意字符串,从字符串中提取整数

    #include<iostream>
    #include<string>
    using namespace std;
    int main() {
    	string str;
    	getline(cin, str);遇回车结束,可以读入空格等除回车之外的其他字符
    	int i = 0;
    	while (str[i] != '\0') {
    		if (str[i]>='0'&&str[i] <= '9') {
    			cout << str[i];
    		}
    		else if (str[i] == '-' && (str[i + 1] >= '0'&&str[i + 1] <= '9')) {
    			cout << "-";
    		}
    		i++;
    	}
    	return 0;
    }
    

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_46870302/article/details/126418591

智能推荐

攻防世界_难度8_happy_puzzle_攻防世界困难模式攻略图文-程序员宅基地

文章浏览阅读645次。这个肯定是末尾的IDAT了,因为IDAT必须要满了才会开始一下个IDAT,这个明显就是末尾的IDAT了。,对应下面的create_head()代码。,对应下面的create_tail()代码。不要考虑爆破,我已经试了一下,太多情况了。题目来源:UNCTF。_攻防世界困难模式攻略图文

达梦数据库的导出(备份)、导入_达梦数据库导入导出-程序员宅基地

文章浏览阅读2.9k次,点赞3次,收藏10次。偶尔会用到,记录、分享。1. 数据库导出1.1 切换到dmdba用户su - dmdba1.2 进入达梦数据库安装路径的bin目录,执行导库操作  导出语句:./dexp cwy_init/[email protected]:5236 file=cwy_init.dmp log=cwy_init_exp.log 注释:   cwy_init/init_123..._达梦数据库导入导出

js引入kindeditor富文本编辑器的使用_kindeditor.js-程序员宅基地

文章浏览阅读1.9k次。1. 在官网上下载KindEditor文件,可以删掉不需要要到的jsp,asp,asp.net和php文件夹。接着把文件夹放到项目文件目录下。2. 修改html文件,在页面引入js文件:<script type="text/javascript" src="./kindeditor/kindeditor-all.js"></script><script type="text/javascript" src="./kindeditor/lang/zh-CN.js"_kindeditor.js

STM32学习过程记录11——基于STM32G431CBU6硬件SPI+DMA的高效WS2812B控制方法-程序员宅基地

文章浏览阅读2.3k次,点赞6次,收藏14次。SPI的详情简介不必赘述。假设我们通过SPI发送0xAA,我们的数据线就会变为10101010,通过修改不同的内容,即可修改SPI中0和1的持续时间。比如0xF0即为前半周期为高电平,后半周期为低电平的状态。在SPI的通信模式中,CPHA配置会影响该实验,下图展示了不同采样位置的SPI时序图[1]。CPOL = 0,CPHA = 1:CLK空闲状态 = 低电平,数据在下降沿采样,并在上升沿移出CPOL = 0,CPHA = 0:CLK空闲状态 = 低电平,数据在上升沿采样,并在下降沿移出。_stm32g431cbu6

计算机网络-数据链路层_接收方收到链路层数据后,使用crc检验后,余数为0,说明链路层的传输时可靠传输-程序员宅基地

文章浏览阅读1.2k次,点赞2次,收藏8次。数据链路层习题自测问题1.数据链路(即逻辑链路)与链路(即物理链路)有何区别?“电路接通了”与”数据链路接通了”的区别何在?2.数据链路层中的链路控制包括哪些功能?试讨论数据链路层做成可靠的链路层有哪些优点和缺点。3.网络适配器的作用是什么?网络适配器工作在哪一层?4.数据链路层的三个基本问题(帧定界、透明传输和差错检测)为什么都必须加以解决?5.如果在数据链路层不进行帧定界,会发生什么问题?6.PPP协议的主要特点是什么?为什么PPP不使用帧的编号?PPP适用于什么情况?为什么PPP协议不_接收方收到链路层数据后,使用crc检验后,余数为0,说明链路层的传输时可靠传输

软件测试工程师移民加拿大_无证移民,未受过软件工程师的教育(第1部分)-程序员宅基地

文章浏览阅读587次。软件测试工程师移民加拿大 无证移民,未受过软件工程师的教育(第1部分) (Undocumented Immigrant With No Education to Software Engineer(Part 1))Before I start, I want you to please bear with me on the way I write, I have very little gen...

随便推点

Thinkpad X250 secure boot failed 启动失败问题解决_安装完系统提示secureboot failure-程序员宅基地

文章浏览阅读304次。Thinkpad X250笔记本电脑,装的是FreeBSD,进入BIOS修改虚拟化配置(其后可能是误设置了安全开机),保存退出后系统无法启动,显示:secure boot failed ,把自己惊出一身冷汗,因为这台笔记本刚好还没开始做备份.....根据错误提示,到bios里面去找相关配置,在Security里面找到了Secure Boot选项,发现果然被设置为Enabled,将其修改为Disabled ,再开机,终于正常启动了。_安装完系统提示secureboot failure

C++如何做字符串分割(5种方法)_c++ 字符串分割-程序员宅基地

文章浏览阅读10w+次,点赞93次,收藏352次。1、用strtok函数进行字符串分割原型: char *strtok(char *str, const char *delim);功能:分解字符串为一组字符串。参数说明:str为要分解的字符串,delim为分隔符字符串。返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。其它:strtok函数线程不安全,可以使用strtok_r替代。示例://借助strtok实现split#include <string.h>#include <stdio.h&_c++ 字符串分割

2013第四届蓝桥杯 C/C++本科A组 真题答案解析_2013年第四届c a组蓝桥杯省赛真题解答-程序员宅基地

文章浏览阅读2.3k次。1 .高斯日记 大数学家高斯有个好习惯:无论如何都要记日记。他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?高斯出生于:1777年4月30日。在高斯发现的一个重要定理的日记_2013年第四届c a组蓝桥杯省赛真题解答

基于供需算法优化的核极限学习机(KELM)分类算法-程序员宅基地

文章浏览阅读851次,点赞17次,收藏22次。摘要:本文利用供需算法对核极限学习机(KELM)进行优化,并用于分类。

metasploitable2渗透测试_metasploitable2怎么进入-程序员宅基地

文章浏览阅读1.1k次。一、系统弱密码登录1、在kali上执行命令行telnet 192.168.26.1292、Login和password都输入msfadmin3、登录成功,进入系统4、测试如下:二、MySQL弱密码登录:1、在kali上执行mysql –h 192.168.26.129 –u root2、登录成功,进入MySQL系统3、测试效果:三、PostgreSQL弱密码登录1、在Kali上执行psql -h 192.168.26.129 –U post..._metasploitable2怎么进入

Python学习之路:从入门到精通的指南_python人工智能开发从入门到精通pdf-程序员宅基地

文章浏览阅读257次。本文将为初学者提供Python学习的详细指南,从Python的历史、基础语法和数据类型到面向对象编程、模块和库的使用。通过本文,您将能够掌握Python编程的核心概念,为今后的编程学习和实践打下坚实基础。_python人工智能开发从入门到精通pdf

推荐文章

热门文章

相关标签