HJ51~HJ60 华为机试题库
HJ51 输出单向链表中倒数第k个结点
题目:https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d?tpId=37&tqId=21274&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=
本身是很简单的,就是限制条件有点多。这里用了队列来处理,一旦超出长度就出队,最后输出队头元素就ok。
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct ListNode{ 4 int Key; 5 ListNode* Next; 6 ListNode(int x){ 7 Key=x; 8 Next=nullptr; 9 } 10 }; 11 ListNode* Add(ListNode* a,ListNode* b){ 12 a->Next=b; 13 return b; 14 } 15 void Work(ListNode* hd,int k){ 16 queue<ListNode*>que; 17 while(!que.empty())que.pop(); 18 while(1){ 19 que.push(hd); 20 if((int)que.size()>k)que.pop(); 21 if(hd->Next!=nullptr) hd=hd->Next; 22 else break; 23 } 24 cout<<que.front()->Key<<endl; 25 return; 26 } 27 void init(){ 28 int n,b; 29 ListNode* hd; 30 while(cin>>n){ 31 ListNode* a=new ListNode(0); 32 hd=a; 33 for(int i=1;i<=n;i++){ 34 cin>>b; 35 a=Add(a,new ListNode(b)); 36 } 37 cin>>n; 38 Work(hd->Next,n); 39 } 40 return; 41 } 42 int main(){ 43 ios_base::sync_with_stdio(false); 44 cin.tie(NULL); 45 init(); 46 return 0; 47 }
HJ52 计算字符串的编辑距离
题目:https://www.nowcoder.com/practice/3959837097c7413a961a135d7104c314?tpId=37&tqId=21275&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
感觉自己写dp水平还不行,写这个题的时候忘记处理边界情况了…>_<
1 #include<bits/stdc++.h> 2 using namespace std; 3 char s1[1010],s2[1010]; 4 const int inf=1e9; 5 int main(){ 6 scanf("%s%s",s1+1,s2+1); 7 int l1=strlen(s1+1),l2=strlen(s2+1); 8 vector<vector<int>>dp(l1+1,vector<int>(l2+1,inf)); 9 dp[0][0]=0;dp[0][1]=dp[1][0]=1; 10 for(int i=1;i<=l1;i++)dp[i][0]=i; 11 for(int i=1;i<=l2;i++)dp[0][i]=i; 12 for(int i=1;i<=l1;i++){ 13 for(int j=1;j<=l2;j++){ 14 if(s1[i]==s2[j])dp[i][j]=min(dp[i][j],dp[i-1][j-1]); 15 dp[i][j]=min(dp[i][j],dp[i-1][j-1]+1); 16 dp[i][j]=min(dp[i][j],dp[i-1][j]+1); 17 dp[i][j]=min(dp[i][j],dp[i][j-1]+1); 18 } 19 } 20 cout<<dp[l1][l2]; 21 }
HJ53 杨辉三角的变形
题目:https://www.nowcoder.com/practice/8ef655edf42d4e08b44be4d777edbf43?tpId=37&tqId=21276&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
打了个杨辉三角的表,发现规律就是除了前两行是-1 -1以外,剩下的循环节就是2 3 2 4……
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[110][110]; 4 int main(){ 5 int n; 6 cin>>n; 7 a[1][1]=a[2][1]=a[2][2]=a[2][3]=1; 8 for(int i=3;i<=n;i++){ 9 for(int j=1;j<=2*i-1;j++){ 10 if(j==1||j==2*i-1){ 11 a[i][j]=1; 12 continue; 13 } 14 if(j==2){ 15 a[i][j]=a[i-1][j-1]+a[i-1][j]; 16 continue; 17 } 18 a[i][j]=a[i-1][j-2]+a[i-1][j-1]+a[i-1][j]; 19 } 20 } 21 for(int i=1;i<=n;i++){ 22 for(int j=1;j<=2*i-1;j++){ 23 printf("%d ",a[i][j]); 24 } 25 puts(""); 26 } 27 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 int n; 5 cin>>n; 6 if(n==1||n==2)cout<<-1; 7 else{ 8 n-=2; 9 if(n%4==1||n%4==3)cout<<2; 10 else if(n%4==2)cout<<3; 11 else cout<<4; 12 } 13 }
HJ54 表达式求值
1 #include<bits/stdc++.h> 2 using namespace std; 3 char s[110],fuhao[110]; 4 int sum=0,l,top_shuzi=0,shuzi[110],top_fuhao=0; 5 int kuohao[110],top_kuohao=0; 6 void init(){ 7 scanf("%s",s); 8 l=strlen(s); 9 return; 10 } 11 void Jisuan(int f,int t){ 12 int sz[110],w=top_fuhao-(t-f)+1,top=0,tpjj=0; 13 sz[++top]=shuzi[f]; 14 char jj[110]; 15 for(int k=f+1;k<=t;k++){ 16 if(fuhao[w]=='*'){ 17 sz[top]*=shuzi[k]; 18 } 19 if(fuhao[w]=='/'){ 20 sz[top]/=shuzi[k]; 21 } 22 if(fuhao[w]=='+'||fuhao[w]=='-'){ 23 jj[++tpjj]=fuhao[w]; 24 sz[++top]=shuzi[k]; 25 } 26 w++; 27 top_fuhao--; 28 } 29 int x=sz[1]; 30 for(int k=2;k<=top;k++){ 31 if(jj[k-1]=='+'){ 32 x+=sz[k]; 33 } 34 else x-=sz[k]; 35 } 36 shuzi[f]=x; 37 top_shuzi=f; 38 return; 39 } 40 void Work(){ 41 for(int i=0;i<l;){ 42 if(s[i]=='('){ 43 kuohao[++top_kuohao]=top_shuzi+1; 44 i++; 45 } 46 int x=0,f=0; 47 while(s[i]>='0'&&s[i]<='9'){ 48 x=x*10+s[i]-'0'; 49 i++; 50 f=1; 51 } 52 if(f) shuzi[++top_shuzi]=x; 53 if(s[i]=='-'){ 54 if((s[i-1]<'0'||s[i-1]>'9')&&s[i-1]!=')'){ 55 i++; 56 x=0; 57 while(s[i]>='0'&&s[i]<='9'){ 58 x=x*10+s[i]-'0'; 59 i++; 60 } 61 shuzi[++top_shuzi]=-x; 62 } 63 } 64 if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'){ 65 fuhao[++top_fuhao]=s[i]; 66 i++; 67 } 68 if(s[i]==')'){ 69 Jisuan(kuohao[top_kuohao],top_shuzi); 70 top_kuohao--; 71 i++; 72 } 73 } 74 if(top_shuzi>1)Jisuan(1,top_shuzi); 75 return; 76 } 77 int main(){ 78 init(); 79 Work(); 80 cout<<shuzi[1]; 81 return 0; 82 }
HJ55 挑7
nowcoder.com/practice/ba241b85371c409ea01ac0aa1a8d957b?tpId=37&tqId=21278&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
1 #include<bits/stdc++.h> 2 using namespace std; 3 bool Check(int x){ 4 if(x%7==0)return 1; 5 stringstream v; 6 v.clear(); 7 v<<x; 8 string s; 9 v>>s; 10 for(auto i:s) 11 if(i=='7')return 1; 12 return 0; 13 } 14 int main(){ 15 int n,cnt=0; 16 cin>>n; 17 for(int i=1;i<=n;i++){ 18 if(Check(i))cnt++; 19 } 20 cout<<cnt; 21 }
HJ56 完全数计算
https://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84?tpId=37&tqId=21279&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=
接近1e9都毫无压力地5ms跑过去了。。。。。。牛客是什么少爷机啊……???我怎么这么懵啊
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 5 // 判断一个数是否为完全数 6 bool isPerfectNumber(int num) { 7 if (num < 2) return false; // 小于2的数不可能是完全数 8 int sum = 1; // 1是所有自然数的因子 9 int limit = sqrt(num); // 只需遍历到根号num 10 for (int i = 2; i <= limit; ++i) { 11 if (num % i == 0) { 12 sum += i; 13 if (i != num / i) { // 避免加上平方根两次 14 sum += num / i; 15 } 16 } 17 } 18 return sum == num; 19 } 20 21 int main() { 22 int n; 23 cin >> n; 24 int count = 0; 25 for (int i = 1; i <= n; ++i) { 26 if (isPerfectNumber(i)) { 27 ++count; 28 } 29 } 30 cout << count << endl; 31 return 0; 32 }
HJ57 高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6?tpId=37&tqId=21280&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 n1[11000],n2[11000],a[11000],b[11000]; 4 int c[11000]; 5 int main(){ 6 scanf("%s",n1); 7 scanf("%s",n2); 8 int l1=strlen(n1),l2=strlen(n2); 9 int cnt=0; 10 for(int i=l1-1;i>=0;i--)a[++cnt]=n1[i]-'0'; 11 cnt=0; 12 for(int i=l2-1;i>=0;i--)b[++cnt]=n2[i]-'0'; 13 cnt=max(l1,l2); 14 int z=0; 15 for(int i=1;i<=cnt;i++){ 16 c[i]=a[i]+b[i]+z; 17 z=0; 18 if(c[i]>=10){ 19 c[i]%=10; 20 z=1; 21 } 22 } 23 if(z)c[++cnt]=z; 24 for(int i=cnt;i>=1;i--)printf("%d",c[i]); 25 }
HJ58 输入n个整数,输出其中最小的k个
https://www.nowcoder.com/practice/69ef2267aafd4d52b250a272fd27052c?tpId=37&tqId=21281&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=
一个挺好的优先队列仿函数的模板priority_queue<int,vector<int>,comparepri>
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct comparepri{ 4 bool operator()(const int&a,const int&b){ 5 return a>b; 6 } 7 }; 8 int main(){ 9 ios_base::sync_with_stdio(false); 10 cin.tie(NULL); 11 int n,t,k; 12 cin>>n>>k; 13 priority_queue<int,vector<int>,comparepri>pri; 14 for(int i=1;i<=n;i++){ 15 cin>>t; 16 pri.push(t); 17 } 18 for(int i=1;i<=k;i++){ 19 cout<<pri.top()<<" "; 20 pri.pop(); 21 } 22 }
HJ59 找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4?tpId=37&tqId=21282&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 c[300]; 4 int main(){ 5 string s; 6 cin>>s; 7 for(auto i:s) c[(int)i]++; 8 for(auto i:s){ 9 if(c[(int)i]==1){ 10 cout<<i; 11 return 0; 12 } 13 } 14 cout<<-1; 15 }
HJ60 查找组成一个偶数最接近的两个素数
题目:https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9?tpId=37&tqId=21283&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 vector<bool> XianXingShai(const int&n){ 4 vector<int>Prime(n+1); 5 vector<bool>isPrime(n+1); 6 vector<int>minPrime(n+1); 7 int num_Prime=0; 8 for(int i=2;i<=n;i++){ 9 if(minPrime[i]==0){ 10 isPrime[i]=1; 11 minPrime[i]=i; 12 Prime[++num_Prime]=i; 13 } 14 for(int j=1;j<=num_Prime;j++){ 15 if(Prime[j]*i>n||Prime[j]>minPrime[i])break; 16 minPrime[Prime[j]*i]=Prime[j]; 17 } 18 } 19 return isPrime; 20 } 21 int main(){ 22 int n;cin>>n; 23 vector<bool>isPrime=XianXingShai(n); 24 int a,b,mindis=1e9; 25 for(int i=1;i<=n-i;i++){ 26 if(isPrime[i]&&isPrime[n-i]){ 27 if(abs(i-(n-i))<mindis){ 28 mindis=abs(i-(n-i)); 29 a=i;b=n-i; 30 } 31 } 32 } 33 cout<<a<<endl<<b; 34 }
by:AlenaNuna