AlenaNuna

导航

HJ01~HJ10 华为机试题库

HJ1 字符串最后一个单词的长度

题目:https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da?tpId=37&tqId=21224&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 char s[5010];
4 int main(){
5     while(~scanf("%s",s));
6     cout<<strlen(s);
7     return 0;
8 }

 HJ2 计算某字符出现次数

题目:https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1?tpId=37&rp=1&ru=%2Fexam%2Foj%2Fta&qru=%2Fexam%2Foj%2Fta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=&judgeStatus=&tags=&title=&gioEnter=menu

不知道为啥,读整行不让我用gets,只能用cin.getline(s,1001)或者cin.get(s,1001)。getline和get的区别是getline读完之后不包括换行符,get会把换行符也弄进s里。

注意c可以对应大小写的情况。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 char c;
 4 char s[1010];
 5 int main(){
 6     cin.getline(s,1001);
 7     cin>>c;
 8     int l=strlen(s),cnt=0,f=0;
 9     if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))f=1;
10     for(int i=0;i<l;i++)
11         if(s[i]==c||(f&&abs(c-s[i])=='a'-'A')) cnt++;
12     cout<<cnt;
13     return 0;
14 }

 HJ3 明明的随机数

题目:https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0?tpId=37&tqId=21226&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 f[510];
 4 int n,a;
 5 int main(){
 6     cin>>n;
 7     for(int i=1;i<=n;i++) {
 8         scanf("%d",&a);
 9         f[a]=1;
10     }
11     for(int i=1;i<=500;i++)
12         if(f[i])printf("%d\n",i);
13     return 0;
14 }

 HJ4 字符串分隔

题目:https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7?tpId=37&tqId=21227&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 char s[110];
 4 int main(){
 5     while(~scanf("%s",s)){
 6         int l=strlen(s),i,cnt=0;
 7         for(i=0;i<l;i++){
 8             printf("%c",s[i]);
 9             cnt++;
10             if(cnt==8){
11                 cnt=0;
12                 printf("\n");
13             }
14         }
15         if(cnt){
16             for(;cnt<8;cnt++)printf("0");
17             printf("\n");
18         }
19     }
20     return 0;
21 }

 HJ5 进制转换

题目:https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6?tpId=37&tqId=21228&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 #define ll long long
 3 using namespace std;
 4 char s[50];
 5 int l;
 6 int main(){
 7     while(~scanf("%s",s+1)){
 8         l=strlen(s+1);
 9         ll tim=1;
10         long long sum=0;
11         for(int i=l;i>=3;i--){
12             ll t;
13             if(s[i]>='A'&&s[i]<='F'){
14                 t=s[i]-'A'+10;
15             }
16             else t=s[i]-'0';
17             sum+=t*tim;
18             tim*=16;
19         }
20         printf("%lld\n",sum);
21     }
22     return 0;
23 }

 HJ6 质数因子

题目:https://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607?tpId=37&tqId=21229&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

n的范围是2e9+14

每个数都可以被sqrt(n)内的素数分解,除了一种情况:分到最后如果x大于sqrt(n),那就说明x本身是一个素数。

线性筛预处理出sqrt(n)范围内的素数表,然后一直枚举依次判断该素数是否是n的因子,如果是就输出这个素数并且n/=Prime[i]。

如果最后x大于1,就说明存在一个大于sqrt(n)的素数就是它本身

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,Prime[100100],num_prime=0,MinSS[100100];
 4 void XXS(int x){
 5     for(int i=2;i<=x;i++){
 6         if(MinSS[i]==0){
 7             MinSS[i]=i;
 8             Prime[++num_prime]=i;
 9         }
10         for(int j=1;Prime[j]<=MinSS[i]&&i*Prime[j]<=x&&j<=num_prime;j++){
11             MinSS[i*Prime[j]]=Prime[j];
12         }
13     }
14     return;
15 }
16 void Work(int x){
17     for(int i=1;i<=num_prime;i++){
18         while(x%Prime[i]==0){
19             printf("%d ",Prime[i]);
20             x/=Prime[i];
21         }
22     }
23     if(x!=1) printf("%d",x);
24     return;
25 }
26 int main(){
27     cin>>n;
28     XXS(sqrt(n));
29     Work(n);
30     return 0;
31 }

 HJ7 取近似值

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

round是一个四舍五入保留到整数的函数。

1 #include<bits/stdc++.h>
2 using namespace std;
3 int main(){
4     double n;
5     cin>>n;
6     n=round(n);
7     printf("%.0lf",n);
8     return 0;
9 }

 HJ8 合并表记录

题目:https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201?tpId=37&tqId=21231&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 struct NUM{
 4     int idx;
 5     long long val;
 6 }a[510];
 7 int n,cnt=0;
 8 bool cmp(const NUM&a,const NUM&b){
 9     return a.idx<b.idx;
10 }
11 long long sum;
12 int main(){
13     cin>>n;
14     for(int i=1;i<=n;i++)
15         scanf("%d%lld",&a[i].idx,&a[i].val);
16     sort(a+1,a+n+1,cmp);
17     sum=a[1].val;
18     printf("%d ",a[1].idx);
19     for(int i=2;i<=n;i++){
20         if(a[i].idx==a[i-1].idx){
21             sum+=a[i].val;
22         }
23         else{
24             printf("%lld\n",sum);
25             sum=a[i].val;
26             printf("%d ",a[i].idx);
27         }
28     }
29     printf("%lld\n",sum);
30     return 0;
31 }

 HJ9 提取不重复的整数

题目:https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1?tpId=37&tqId=21232&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 char s[10];
 4 int l;
 5 bool mp[200];
 6 int main(){
 7     scanf("%s",s);
 8     l=strlen(s);
 9     for(int i=l-1;i>=0;i--){
10         if(mp[s[i]]==0){
11             printf("%c",s[i]);
12             mp[s[i]]=1;
13         }
14     }
15     return 0;
16 }

 HJ10 字符个数统计

题目:https://www.nowcoder.com/practice/eb94f6a5b2ba49c6ac72d40b5ce95f50?tpId=37&tqId=21233&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 char s[550];
 4 bool mp[200];
 5 int main(){
 6     cin>>s;
 7     int l=strlen(s),ans=0;
 8     for(int i=0;i<l;i++){
 9         if(mp[s[i]]==0){
10             ans++;
11             mp[s[i]]=1;
12         }
13     }
14     cout<<ans;
15     return 0;
16 }

就,反正总体来说都很容易。除了感觉自己基础有点一般以外=。=

posted on 2024-09-06 14:02  AlenaNuna  阅读(9)  评论(0编辑  收藏  举报