面试随缘刷题--day4

leetcode no.46 全排列

递归,到边界退出,题目本身无难度

限制了使用vector,注意二维的vector不可以访问下标来赋值,可以读取数据,比如vec[3][2]=1是不合理的

另外如果一维的给了长度再push_back到二维会报错,二维初始化给长度也报错,这个可能是leetcode导致的

如果未初始化直接使用下标赋值是不行的,就只能用push_back,前面有一个push_back了后面就可以使用下标赋值(即使他们中间有没赋值过的)

如果vector<int>t(100),这种可以从最开始就下标赋值

vector<int>temp(1000),注意括号不是方的,如果是方的代表有1000个vector,是vector数组,这种是说一个vector有1000个元素

因为用push_back不能那种像数组下标那样直接占位,记得处理完以后pop_back一下,避免影响下一轮

pd直接用下标就行,不用映射到数,因为也不知道具体数有多大。

到达边界记得写return

class Solution {
public:
    vector<vector<int> >ans;
    int len;
    int pd[1000]={0};
    int nowansnum=0;
    vector<int> temp;
    void generate(int index,vector<int>&nums){
        if(index==len){
            ans.push_back(temp);
            return;
        }
        for(int i=0;i<len;i++){
            if(pd[i]==0){
                pd[i]=1;
                temp.push_back(nums[i]);
                generate(index+1,nums);
                temp.pop_back();
                pd[i]=0;
            }
        }
    }
    vector<vector<int>> permute(vector<int>& nums) {
        len=nums.size();
        generate(0,nums);
        return ans;
    }
};
View Code

 leetcode no.51 N皇后

反向的时候不能用abs判断,加一个足够大的数,不然两条线会重复计算。

可以让竖向的数组直接对应横轴,省空间。

没啥难度就注意一下,如果想好了从一开始就都从1开始,再就是转换成他要的结果,先存string,所有string存在同一个vector这就是一个表,这个表再push_back到真正的输出里面。

class Solution {
public:
    vector<vector<string> >ans;
    int hashTable[1000]={0};
    int plus[1000]={0};
    int minus[1000]={0};
    void generate(int index,int n){
        if(index==n+1){
            vector<string> tempans;
            for(int i=1;i<=n;i++){
                string tempstring="";
                for(int j=1;j<=n;j++){
                    if(hashTable[j]==i){
                        tempstring+="Q";
                    }
                    else
                        tempstring+='.';
                }
                tempans.push_back(tempstring);
            }
            ans.push_back(tempans);
            return;
        }
        for(int i=1;i<=n;i++){
            if(hashTable[i]==0 && plus[index+i]==0 && minus[i-index+200]==0)
            {
                hashTable[i]=index;//第i列对应index行
                plus[index+i]=1;
                minus[i-index+200]=1;
                generate(index+1,n);
                hashTable[i]=0;
                plus[index+i]=0;
                minus[i-index+200]=0;
            }
        }
        
    }
    vector<vector<string>> solveNQueens(int n) {
        generate(1,n);
        return ans;
    }
};
View Code

 PAT B1020 月饼

题本身就是单价最高先卖,无难度,但注意题目说的正数和正整数,该用double用double

注意double读取是%lf,输出%f,float都是%f,longlong是"%lld",输入输出都是

用min函数,不能一个double一个int的比较,给int*1.0完成一下类型转换

fabs是处理浮点数的,返回double类,abs是处理int的,都是绝对值(虽然这题用不到。。用减法直接做结果纯属突然懵逼了qwq)

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int n,target;
double summoney=0;
struct item{
    double store;
    double sumprice;
    double perprice;
}temp[1005];
bool cmp(item a,item b)
{
    return a.perprice>b.perprice;
}
int main()
{
    scanf("%d %d",&n,&target);
    for(int i=0;i<n;i++)
        scanf("%lf",&temp[i].store);
    for(int i=0;i<n;i++)
    {
        scanf("%lf",&temp[i].sumprice);
        temp[i].perprice=temp[i].sumprice/temp[i].store;
    }
    sort(temp,temp+n,cmp);
    for(int i=0;i<n;i++)
    {
        if(target==0)
            break;
        double tempnow=min(target*1.0,temp[i].store);
        temp[i].store=temp[i].store-tempnow;
        target=target-tempnow;
        summoney+=tempnow*temp[i].perprice;

    }
    printf("%.2f",summoney);
    return 0;
}
View Code
时间才能证明一切,选好了就尽力去做吧!
原文地址:https://www.cnblogs.com/tingxilin/p/13457277.html