AlenaNuna

导航

HJ91~HJ100 华为机试题库

HJ91 走方格的方案数

题目:https://www.nowcoder.com/practice/e2a22f0305eb4f2f9846e7d644dba09b?tpId=37&tqId=21314&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D2%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int n,m;cin>>m>>n;
 5     m++; n++;
 6     vector<vector<int>>dp(n+1,vector<int>(m+1));
 7     for(int i=1;i<=m;i++)dp[1][i]=1;
 8     for(int i=1;i<=n;i++)dp[i][1]=1;
 9     for(int i=1;i<=n;i++){
10         for(int j=1;j<=m;j++){
11             if(i>1&&j>1)dp[i][j]=dp[i-1][j]+dp[i][j-1];
12         }
13     }
14     cout<<dp[n][m];
15 }

 HJ92 在字符串中找出连续最长的数字串

题目:https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec?tpId=37&tqId=21315&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D2%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     string s;
 5     while(cin>>s){
 6         int len=0,maxlen=0;
 7         for(int i=0;i<s.size();i++){
 8             if(s[i]>='0'&&s[i]<='9'){
 9                 len++;
10             }
11             else{
12                 maxlen=max(maxlen,len);
13                 len=0;
14             }
15         }
16         maxlen=max(maxlen,len);
17         len=0;
18         for(int i=0;i<s.size();i++){
19             if(s[i]>='0'&&s[i]<='9') len++;
20             else len=0;
21             if(len==maxlen){
22                 for(int j=i-len+1;j<=i;j++)cout<<s[j];
23                 len=0;
24             }
25         }
26         cout<<","<<maxlen<<endl;
27     }
28 }

 HJ93 数组分组

题目:https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86?tpId=37&tqId=21316&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D2%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,a[55],sum=0;
 4 const int maxn=55000;
 5 bool u[maxn][55];
 6 #define dp (u+(maxn>>1))
 7 //vector< int >fenzu(1010);
 8 int fenzu(int x){
 9     if(x%5==0) return 1;
10     if(x%3==0) return 2;
11     return 0;
12 }
13 void init(){
14     cin>>n;
15     for(int i=1;i<=n;i++){
16         scanf("%d",&a[i]);
17         sum+=a[i];
18     }
19 /*    for(int i=-500;i<=500;i+=5)fenzu[i]=1;
20     for(int i=-498;i<=500;i+=3)
21         if(fenzu[i]==0)fenzu[i]=2;*/
22     return;
23 }
24 void DP(){
25     if(!fenzu(a[1])){
26         dp[a[1]][1]=1;
27         dp[0][1]=1;
28     }
29     else if(fenzu(a[1])==1)
30         dp[a[1]][1]=1;
31     else dp[0][1]=1;
32     for(int i=2;i<=n;i++){
33         for(int j=-25000;j<=25000;j++){
34             if(fenzu(a[i])==0){
35 //                if(j-a[i-1]>=0)
36                     dp[j][i]=max(dp[j][i],dp[j-a[i]][i-1]);
37                 dp[j][i]=max(dp[j][i],dp[j][i-1]);
38             }
39             if(fenzu(a[i])==1){
40 //                if(j-a[i-1]>=0)
41                     dp[j][i]=max(dp[j][i],dp[j-a[i]][i-1]);
42             }
43             if(fenzu(a[i])==2) dp[j][i]=max(dp[j][i],dp[j][i-1]);
44         }
45     }
46     return;
47 }
48 void Output(int x){
49     if(x) {
50         puts("true");
51         exit(0);
52     }
53     else {
54         puts("false");
55         exit(0);
56     }
57     return;
58 }
59 int main(){
60 //    cout<<dp[1][-500];
61     init();
62     if(sum%2) Output(0);
63     DP();
64     if(dp[sum/2][n]) Output(1);
65     else Output(0);
66     return 0;
67 }

 HJ95 人民币转换

题目:https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b?tpId=37&tqId=21318&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D2%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

很有意思的一道题

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 double n;
  4 int zs,xs;
  5 string s[20];
  6 void init(){
  7     s[0]="";
  8     s[1]="";
  9     s[2]="";
 10     s[3]="";
 11     s[4]="";
 12     s[5]="";
 13     s[6]="";
 14     s[7]="";
 15     s[8]="";
 16     s[9]="";
 17     s[10]="";
 18     s[11]="";
 19     s[12]="";
 20     s[13]="";
 21     s[14]="亿";
 22     s[15]="";
 23     s[16]="";
 24     s[17]="";
 25     s[18]="";
 26     s[19]="";
 27     scanf("%lf",&n);
 28     zs=n;
 29     xs=round(n*100);
 30     xs=xs%100;
 31     return;
 32 }
 33 void Work2(int x){
 34     bool yq=0,yb=0,shi=0;
 35     if(x>=1000){
 36         cout<<s[x/1000]<<s[12];
 37         x%=1000;
 38         yq=1;
 39     }
 40     if(x>=100){
 41         cout<<s[x/100]<<s[11];
 42         x%=100;
 43         yb=1;
 44     }
 45     if(x>=10){
 46         if(yq&&!yb)cout<<s[18];
 47         if(x/10!=1) cout<<s[x/10]<<s[10];
 48         else cout<<s[10];
 49         x%=10;
 50         shi=1;
 51     }
 52     if(x>=0){
 53         if(!x&&!yq&&!yb&&!shi){
 54             cout<<s[0];
 55             return;
 56         }
 57         if((yq&&!yb&&!shi)||(yb&&!shi))
 58             if(x) cout<<s[18];
 59         if(x>0) cout<<s[x];
 60     }
 61     return;
 62 }
 63 void Xiaoshu(bool b){
 64     int jiao=xs/10,fen=xs%10;
 65     if(!b){
 66         if(jiao)cout<<s[jiao]<<s[16];
 67         if(fen)cout<<s[fen]<<s[17];
 68         return;
 69     }
 70     if(jiao)cout<<s[jiao]<<s[16];
 71     if(fen)cout<<s[fen]<<s[17];
 72     return;
 73 }
 74 void Work(){
 75     printf("人民币");
 76     int yiyi=1e8,yiwan=1e4;
 77     bool b=zs>0;
 78     if(zs>=yiyi){
 79         Work2(zs/yiyi);
 80         zs%=yiyi;
 81         cout<<s[14];
 82     }
 83     if(zs>=yiwan){
 84         Work2(zs/yiwan);
 85         zs%=yiwan;
 86         cout<<s[13];
 87     }
 88     if(zs>0){
 89         Work2(zs);
 90     }
 91     if(b)cout<<s[15];
 92     if(xs!=0)Xiaoshu(b);
 93     else cout<<s[19];
 94     return;
 95 }
 96 int main(){
 97     init();
 98     Work();
 99     return 0;
100 }

 

posted on 2024-09-23 01:07  AlenaNuna  阅读(8)  评论(0编辑  收藏  举报