讯飞笔试算法题

题目描述: 一个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;
    }
    

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值