牛客華爲機試題刷題筆記(五)

本以爲108題能在機考前刷完,卻因爲各種事情耽擱了。。anyway,明天就要機考了,今天最後一次刷題,其他的自求多福吧

41.稱砝碼

在組合數學中可以用母函數的方式求解。

這裏,先計算出能稱的最大重量:
Summax=w1m1+...wnmn

然後從Summax 減去jwi 大小砝碼的質量,j的範圍是(1,mi)

這裏用一個set可以互斥遞增的存儲能稱出砝碼的質量。

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


#if 1
    int maxsum=0;
    for(int i=0;i<n;++i)
    {
        maxsum+=(weight[i]*num[i]);
    }
    set<int >s;
    s.insert(maxsum);
    for(int i=0;i<n;++i)
    {
        for(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<<s.size()<<endl;
    }
}

42.學英語

是一道簡單但麻煩的題目,把數字讀出來。。

這裏處理分爲兩步:
先處理百位一下的比如234:two hundred and thirty four

然後將各個位組合一下。

#include <iostream>
#include <string>
#include <vector>
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"<<endl;
    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<<res<<endl;

    }
}

43.走迷宮

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

#include <iostream>
#include <vector>
#include <limits>

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

void dfs(vector<vector<int>>&maze,int i,int j,vector<pair_>path,vector<vector<pair_>>&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+1<maze.size()&&maze[i+1][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+1<maze[0].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));
    vector<pair_>path;
    vector<vector<pair_> >res;
    for(int i=0;i<N;++i)
    {
        for(int j=0;j<M;++j)
        {
        cin>>maze[i][j];
        }
    }
    dfs(maze,0,0,path,res);
    vector<pair_>respath;
    int minVal=numeric_limits<int>::max();
    for(int i=0;i<res.size();++i)
    {
        if(res[i].size()<minVal)
        {
        minVal=res[i].size();
        respath.swap(res[i]);
        }

    }
    for(int i=0;i<respath.size();++i)
        cout<<respath[i];
    }

    return 0;
}

44.數獨

pass

45.名字的漂亮度

就是看字符串裏面哪個字符出現次數最多,那麼給他的漂亮度定義爲26,以此類推。

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


    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章