牛客华为机试题刷题笔记(五)

本以为108题能在机考前刷完,却因为各种事情耽搁了。。anyway,明天就要机考了,今天最后一次刷题,其他的自求多福吧

41.称砝码

在组合数学中可以用母函数的方式求解。

这里,先计算出能称的最大重量:
Summax=w1m1+...wnmn

然后从 Summax 减去 jwi 大小砝码的质量,j的范围是 (1,mi)

这里用一个set可以互斥递增的存储能称出砝码的质量。

#include 
#include 
#include 
#include 
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
    vector<int>weight(n);
    vector<int>num(n);
    for(int i=0;icin>>weight[i];
    }
    for(int i=0;icin>>num[i];
    }


#if 1
    int maxsum=0;
    for(int i=0;iset<int >s;
    s.insert(maxsum);
    for(int i=0;ifor(auto it = s.begin();it!=s.end();++it)
        {
        for(int j=1;j<=num[i];++j)
        {
            if(*it-j*weight[i]>0)
            {
            s.insert(*it-j*weight[i]);
            }
        }
        }

    }
    s.insert(0);
#endif

    cout<

42.学英语

是一道简单但麻烦的题目,把数字读出来。。

这里处理分为两步:
先处理百位一下的比如234:two hundred and thirty four

然后将各个位组合一下。

#include 
#include 
#include 
using namespace std;
const vector<string> str1 = {"zero", "one", "two", "three", "four","five", "six", "seven", "eight", "nine"};
const vector<string> str2 = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen","sixteen", "seventeen", "eighteen", "nineteen"};
const vector<string> str3 = {"","ten", "twenty", "thirty", "forty", "fifty","sixty", "seventy", "eighty", "ninety"};
string toString(int num)
{
    string res;
    if (num >= 0 && num <= 9)
    res += str1[num];

    else if (num >= 10 && num <= 19)
    res += str2[num % 10];
    else if (num >= 20 && num <= 99)
    {
    res += str3[num / 10];
    if (num % 10 == 0)
        return res;
    res += " ";
    res += str1[num % 10];
    }
    else 
    {
    res += str1[num / 100];
    res += " hundred";
    num %= 100;
    if (num) 
    {
        res += " and ";
        res += toString(num);
    }
    }
    return res;

}
int main()
{
    long n;
    while(cin>>n)
    {
    if(n<=0||n>999999999)
        cout<<"error"<long billion=n / 1000000000;
    string billion_str;
    if(billion!=0)
    {
       billion_str=toString(billion) + " billion ";
    }
    n%=1000000000;
    long million=n/1000000;
    string million_str;
    if(million!=0)
    {

       million_str=toString(million) + " million ";
    }
    n%=1000000;

    long thousand=n/1000;
    string thousand_str;
    if(thousand!=0)
    {
        thousand_str=toString(thousand)+" thousand ";
    }
    n%=1000;
    string hundred_str;
    if(n!=0)
        hundred_str=toString(n);

    string res=billion_str+million_str+thousand_str+hundred_str;
    cout<

43.走迷宫

这里我直接暴力深度搜索,然后把所有可行解保存,然后算出最小值。

#include 
#include 
#include 

using namespace std;
struct pair_
{
    int x;
    int y;
};
ostream& operator<<(ostream&os,const struct pair_ &p)
{
    os<<"("<","<")"<return os;
}

void dfs(vector<vector<int>>&maze,int i,int j,vectorpath,vector<vector>&res)
{
    maze[i][j]=1;
    pair_ p;
    p.x=i;
    p.y=j; 
    path.push_back(p);
    if(i==maze.size()-1 && j==maze[0].size()-1)
    {
    res.push_back(path);
    }

    if(i-1>=0 && maze[i-1][j]==0)
    {
    dfs(maze,i-1,j,path,res);
    }
    if(i+11][j]==0)
    {
    dfs(maze,i+1,j,path,res);
    }
    if(j-1>=0&&maze[i][j-1]==0)
    {
    dfs(maze,i,j-1,path,res);
    }
    if(j+10].size()&&maze[i][j+1]==0)
    {
    dfs(maze,i,j+1,path,res);
    }

    maze[i][j]=0;
}
int main()
{
    int N,M;
    while(cin>>N>>M)
    {
    vector<vector<int>>maze(N,vector<int>(M));
    vectorpath;
    vector<vector >res;
    for(int i=0;ifor(int j=0;jcin>>maze[i][j];
        }
    }
    dfs(maze,0,0,path,res);
    vectorrespath;
    int minVal=numeric_limits<int>::max();
    for(int i=0;iif(res[i].size()for(int i=0;icout<return 0;
}

44.数独

pass

45.名字的漂亮度

就是看字符串里面哪个字符出现次数最多,那么给他的漂亮度定义为26,以此类推。

#include 
#include 
#include 
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
    for(int i=0;ivector<int>m(26);
        string name;
        cin>>name;
        for(int i=0;itolower(name[i])-'a'];
        }
        sort(m.begin(),m.end(),greater<int>());
        int sum=0;
        for(int i=0;iif(m[i]==0)
            break;
        sum+=m[i]*(26-i);
        }
        cout<

你可能感兴趣的:(笔试面试)