AlenaNuna

导航

HJ81~HJ90 华为机试题库

HJ82 将真分数分解为埃及分数

题目:https://www.nowcoder.com/practice/e0480b2c6aa24bfba0935ffcca3ccb7b?tpId=37&tqId=21305&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=

注意到埃及分数的定义

于是…………我有一个主意◔ ‸◔?

直接把x/y拆分成x个1/y……23333333还真行

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     char c=getchar();
 5     int x=0,y=0;
 6     while(c!='/'){
 7         x=x*10+c-'0';
 8         c=getchar();
 9     }
10     c=getchar();
11     while(c>='0'&&c<='9'){
12         y=y*10+c-'0';
13         c=getchar();
14     }
15     for(int i=1;i<x;i++)cout<<"1/"<<y<<"+";
16     cout<<"1/"<<y;
17 }

 HJ85 最长回文子串

题目:https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507?tpId=37&tqId=21308&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=

拿来练个manacher

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 string Pretreatment(string s){
 4     string t="^";
 5     for(auto i:s)
 6         t=t+"#"+i;
 7     t+="#$";
 8     return t;
 9 }
10 void Manacher(const string&s){
11     int r=0,c=0;
12     vector<int>p(s.size()+10);
13     for(int i=0;i<s.size();i++){
14         int mirr=c*2-i;
15         if(r>i)p[i]=min(p[mirr],r-i);
16         while(s[i+p[i]+1]==s[i-p[i]-1])p[i]++;
17         if(i+p[i]>r){
18             r=i+p[i];
19             c=i;
20         }
21     }
22     int ans=0;
23     for(int i=0;i<s.size();i++)
24         ans=max(ans,p[i]);
25     cout<<ans;
26 }
27 int main(){
28     string s;cin>>s;
29     string t=Pretreatment(s);
30     Manacher(t);
31 }

 HJ88 扑克牌大小

题目:https://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb?tpId=37&tqId=21311&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 char sp[2][10][10];
  4 int paishu[2],spNum[2][10];
  5 void init(){
  6     char c=getchar();
  7     while(c!='-'){
  8         paishu[0]++;
  9         int len=0;
 10         if(c==' ')c=getchar();
 11         while(c!=' '&&c!='-'){
 12             sp[0][paishu[0]][len++]=c;
 13             c=getchar();
 14         }
 15     }
 16     while(c!='\n'){
 17         paishu[1]++;
 18         int len=0;
 19         c=getchar();
 20         while(c!=' '&&c!='\n'){
 21             sp[1][paishu[1]][len++]=c;
 22 //            if(paishu[1]==2&&len==6) printf("%c %d\n",c,len);
 23             c=getchar();
 24 //            if(c==EOF&&paishu[1]==2&&len==6) printf("%c %d\n",c,len);
 25         }
 26     }
 27     return;
 28 }
 29 void WorkspNum(){
 30     for(int i=0;i<2;i++){
 31         for(int j=1;j<=paishu[i];j++){
 32             int len=strlen(sp[i][j]);
 33 //            if(i==1&&j==2)printf("%d\n",len);
 34             if(len==5){
 35                 if(sp[i][j][0]=='j')spNum[i][j]=99;
 36                 else spNum[i][j]=100;
 37             }
 38             if(len==2) spNum[i][j]=10;
 39             if(len==1){
 40                 char c=sp[i][j][0];
 41                 if(c>='3'&&c<='9') spNum[i][j]=c-'3'+3;
 42                 if(c=='J') spNum[i][j]=11;
 43                 if(c=='Q') spNum[i][j]=12;
 44                 if(c=='K') spNum[i][j]=13;
 45                 if(c=='A') spNum[i][j]=14;
 46                 if(c=='2') spNum[i][j]=15;
 47             }
 48         }
 49     }
 50     return;
 51 }
 52 int ZhaDan(int u){
 53     if(spNum[u][1]+spNum[u][2]==199) return 10000;
 54     if(paishu[u]==4){
 55         return spNum[u][1]*100;
 56     }
 57     return -1;
 58 }
 59 int Work(){
 60     if(paishu[0]==1||paishu[0]==3||paishu[0]==5){
 61         if(paishu[1]==paishu[0]){
 62             if(spNum[0][1]>spNum[1][1]) return 0;
 63             else return 1;
 64         }
 65         else{
 66             if(ZhaDan(1)>0) return 1;
 67             else return -1;
 68         }
 69     }
 70     if(paishu[0]==2){
 71         if(ZhaDan(0)>0) return 0;
 72         if(paishu[1]!=2){
 73             if(ZhaDan(1)>0) return 1;
 74         }
 75         else{
 76             if(ZhaDan(1)>0) return 1;
 77             if(spNum[0][1]>spNum[1][1])return 0;
 78             else return 1;
 79         }
 80     }
 81     if(paishu[0]==4){
 82 //        printf("%s\n",sp[1][2]);
 83         if(ZhaDan(0)>ZhaDan(1)) return 0;
 84         else return 1;
 85     }
 86     return -1;
 87 }
 88 void Win(int x){
 89     if(x==0||x==1){
 90         for(int i=1;i<=paishu[x];i++)
 91             printf("%s ",sp[x][i]);
 92         exit(0);
 93     }
 94     printf("ERROR");
 95     exit(0);
 96     return;
 97 }
 98 int main(){
 99     init();
100     WorkspNum();
101     int ans=Work();
102     if(ans==0) Win(0);
103     else if(ans==1) Win(1);
104     else Win(-1);
105     return 0;
106 }

HJ89 24点运算

题目:https://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d?tpId=37&tqId=21312&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 char fuhao[5];
 4 int id[10];
 5 bool book[10];
 6 struct Pai{
 7     int num;
 8     char s[10];
 9 }pai[5];
10 void init(){
11     for(int i=1;i<=4;i++)
12         scanf("%s",pai[i].s);
13     for(int i=1;i<=4;i++){
14         int len=strlen(pai[i].s);
15         char c=pai[i].s[0];
16         if(len==1){
17             if(c>='2'&&c<='9')pai[i].num=c-'2'+2;
18             else if(c=='J') pai[i].num=11;
19             else if(c=='Q') pai[i].num=12;
20             else if(c=='K') pai[i].num=13;
21             else if(c=='A') pai[i].num=1;
22         }
23         if(len==2) pai[i].num=10;
24         if(len==5){
25             puts("ERROR");
26             exit(0);
27         }
28     }
29     return;
30 }
31 void Output(){
32     printf("%s",pai[id[1]].s);
33     for(int i=2;i<=4;i++){
34         printf("%c",fuhao[i]);
35         printf("%s",pai[id[i]].s);
36     }
37     exit(0);
38     return;
39 }
40 void Dfs(int top,int nowsum){
41     if(top>4){
42         if(nowsum==24){
43             Output();
44             return;
45         }
46         return;
47     }
48     fuhao[top]='+';
49     Dfs(top+1,nowsum+pai[id[top]].num);
50     fuhao[top]='-';
51     Dfs(top+1,nowsum-pai[id[top]].num);
52     fuhao[top]='*';
53     Dfs(top+1,nowsum*pai[id[top]].num);
54     fuhao[top]='/';
55     Dfs(top+1,nowsum/pai[id[top]].num);
56     return;
57 }
58 int main(){
59     init();
60     for(int i=1;i<=4;i++)book[i]=1;
61     for(int i=1;i<=4;i++){
62         book[i]=0;
63         for(int j=1;j<=4;j++){
64             if(book[j]){
65                 book[j]=0;
66                 for(int m=1;m<=4;m++){
67                     if(book[m]){
68                         book[m]=0;
69                         for(int n=1;n<=4;n++){
70                             if(book[n]){
71                                 id[1]=i;
72                                 id[2]=j;
73                                 id[3]=m;
74                                 id[4]=n;
75                                 Dfs(2,pai[id[1]].num);
76                             }
77                         }
78                         book[m]=1;
79                     }
80                 }
81                 book[j]=1;
82             }
83         }
84         book[i]=1;
85     }
86     puts("NONE");
87     return 0;
88 }

 HJ90 合法IP

题目:https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9?tpId=37&tqId=21313&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 bool isdigital(char c){
 4     if(c>='0'&&c<='9')return 1;
 5     return 0;
 6 }
 7 int main(){
 8     string s;
 9     while(cin>>s){
10         bool ans=1;
11         if(s[0]=='.'||s[s.size()-1]=='.')ans=0;
12         int cnt=0;
13         for(int i=0;i<s.size();i++)
14             if(s[i]=='-'||s[i]=='+')ans=0;
15         for(int i=0;i<s.size()-1;i++){
16             if(s[i]=='.')cnt++;
17             if(s[i]=='.'&&s[i+1]=='.')ans=0;
18             if((i==0&&s[i]=='0'&&isdigital(s[1]))||
19             (i>0&&s[i-1]=='.'&&s[i]=='0'&&isdigital(s[i+1])))ans=0;
20         }
21         if(cnt!=3)ans=0;
22         if(ans){
23             int x=0;
24             for(int i=0;i<s.size();i++){
25                 if(isdigital(s[i]))
26                     x=x*10+s[i]-'0';
27                 else{
28                     if(x<0||x>255)ans=0;
29                     x=0;
30                 }
31             }
32             if(x<0||x>255)ans=0;
33         }
34         if(ans==0)puts("NO");
35         else puts("YES");
36     }
37 }

by:AlenaNuna

posted on 2024-09-22 13:27  AlenaNuna  阅读(22)  评论(0编辑  收藏  举报