牛客练习记录第三周

    xiaoxiao2022-07-04  146

    第一题:https://www.nowcoder.com/practice/e605ba77112b425889bee3f40481fe93?tpId=90&tqId=30974&tPage=10&rp=10&ru=/ta/2018test&qru=/ta/2018test/question-ranking

    题目描述

    输入一个正整数的字符串,输出与它最接近的对称数字(不包括它自己)的字符串

    注1: 输入字符串的长度最多不会超过18

    注2: 当大于输入数字和小于输入数字的对称数字与输入数字距离相同时,取小的数字作为答案 

    输入描述:

    输入为一个正整数的字符串

    输出描述:

    输出为与输入数字最接近的对称数字(不包括输入本身)的字符串

    根据题目可以知道,直接把字符串前半串复制给后半串就满足题意,但是可能出现原字符串就是他本身,这时候就要分字符串长度为奇偶了,注二说明数值要取小的,当字符串等于他本身时,只要 最中间的数减一就好了,奇数就最中间一位减一,偶数就中间两位减一。

    #include<iostream> #include<algorithm> #include<string> #include<math.h> #include<stdlib.h> #include <iostream> #include <vector> #include <queue> #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<stack> #include<iostream> using namespace std; #define N 100 #define Inf 0x3f3f3f3f int main() { string str,str_copy; cin>>str; str_copy=str; int index=str.size()-1; for(int i=0;i<str.size()/2;i++){ str[index]=str[str.size()-1-index]; index-=1; } // cout<<str<<endl; if(str==str_copy){ if(str.size()%2==1){ str[str.size()/2+1]=str[str.size()/2+1]-1; } else{ str[str.size()/2-1]=str[str.size()/2-1]-1; str[str.size()/2]=str[str.size()/2]-1; } } cout<<str; } /* 6 5 0 2 5 1 4 3 2 6 4 4 6 1 2 4 3 */

    第二题:https://www.nowcoder.com/practice/716d8eef56774b5899efbcd284710630?tpId=90&tqId=30975&rp=10&ru=/ta/2018test&qru=/ta/2018test/question-ranking

    题目描述

    输入一个或多个车牌号码,多个以逗号分割开,再输入想查询的日期(数字,周几),输出该日期限行的车牌号

    车牌号码有以下要求,只要取后五位,如:AD123,12101,车牌号不可能全是字母。

     *现在对尾号进行限制:尾号为1,9则周一限行,尾号为2,8则周二限行,尾号为3,7则周三限行 尾号为4,6则周四限行,尾号为5,0的周五限行,周六周日不限行。

     *尾号不为数字,则看第4位是否是数字,如果第4位还不是 数字,继续看第3位,以此下去,直到找到有数字的时候止.

     *由于用户不熟悉系统,有可能输入错误车牌,如车牌不满5位或大于5位、车牌全是字母、没用逗号分割等,如有输入错误情况 一律返回error

     *如输入没有问题则返回限行的车牌号,如没有,刚返回none

    输入描述:

    一个或多个车牌号码 周几

    输出描述:

    限行的车牌号,如没有限行的则返回none

    emmmmm 麻烦的题,按要求就好了

    #include<iostream> #include<algorithm> #include<string> #include<math.h> #include<stdlib.h> #include <iostream> #include <vector> #include <queue> #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<stack> #include<iostream> using namespace std; #define N 100 #define Inf 0x3f3f3f3f int main() { string str,str_copy; cin>>str; int n; cin>>n; vector<string> strbbb; if(n<=5){ int count=0; string a; for(int i=0;i<str.size();i++){ if(str[i]==','){ if(count!=5||a.size()==0){ cout<<"error"; return 0; } else{ if(n==1&&(a=="1"||a=="9")) strbbb.push_back(str_copy); if(n==2&&(a=="2"||a=="8")){ strbbb.push_back(str_copy); // cout<<11111<<endl; } if(n==3&&(a=="3"||a=="7")) strbbb.push_back(str_copy); if(n==4&&(a=="4"||a=="6")) strbbb.push_back(str_copy); if(n==5&&(a=="5"||a=="0")) strbbb.push_back(str_copy); a=""; str_copy=""; } } else{ if(str[i]>='0'&&str[i]<='9') a=str[i]; count++; str_copy+=str[i]; // cout<<str_copy<<' '<<a<<endl; } } } if(strbbb.size()==0) cout<<"none"; else{ for(int i=0;i<strbbb.size();i++) cout<<strbbb[i]<<endl; } } /* Y008U,T8899 2 */

    3.https://www.nowcoder.com/practice/69682e8bd0654795955c2e478b988f93?tpId=90&tqId=30977&rp=10&ru=/ta/2018test&qru=/ta/2018test/question-ranking

    题目描述

    小雅同学认为6,8是她的幸运数字,而其他数字均不是,一个幸运数是指在十进制表示下只含有幸运数字的数。给定你一个区间(a,b)a和b之间(其中包括a和b幸)运数的个数。

    输入描述:

    输入两个整数a和b,a的取值范围在1和1000000000之间(其中包括1和1000000000),b的取值范围在a和1000000000之间(其中包括a和1000000000)。

    输出描述:

    返回a和b之间的幸运数个数,如果入参不合法,请输出-1

    枚举所有情况,注意边界。

    #include<iostream> #include<algorithm> #include<string> #include<math.h> #include<stdlib.h> #include <iostream> #include <vector> #include <queue> #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<stack> #include<iostream> using namespace std; int ans=0; string a=""; string b=""; void dfs(string str){ if(str.length()>b.length()||(str.length()==b.length()&&str.compare(b) > 0)) return; else{ if(str.length()>a.length()||(str.length()==a.length()&&str.compare(a)>0)) ans++; dfs(str+"6"); dfs(str+"8"); } } int main() { cin>>a>>b; if(a.length()>b.length()||a.compare(b)>=0) cout<<"-1"; else{ dfs("6"); dfs("8"); cout<<ans; } return 0; } /* Y008U,T8899 2 */

    第三题:

    数论课上,老师给 DreamFox 安排了一项任务,用编程实现 A 的 B 次方模 C 。这个当然难不了 ACMer 。于是 DreamFox 回去后就开始用代码实现了。

    输入格式:  三个整数:a,b,c(0≤a,c<231,0≤b<263)。

    输出格式:  一个整数,即 ab mod c 的结果。

    样例数据 1:  输入  5 100000000000000 12830603

    输出:  5418958

    代码:

    #include<bits/stdc++.h> using namespace std; long long a,b,c; inline void ksm() {     long long ans=1;     a=a%c;     while(b>0)     {         if(b&1) ans=(ans*a)%c;         b=b>>1;         a=(a*a)%c;     }     cout<<ans<<endl; } int main() {     cin>>a>>b>>c;     ksm();     return 0; }

    第四题:https://www.nowcoder.com/practice/1843c3b052984e3f98c68935ea3c0d79?tpId=90&tqId=30983&rp=10&ru=/ta/2018test&qru=/ta/2018test/question-ranking

    题目描述

     

    某种特殊的数列a1, a2, a3, ...的定义如下:a1 = 1, a2 = 2, ... , an = 2 * an − 1 + an - 2 (n > 2)。

    给出任意一个正整数k,求该数列的第k项模以32767的结果是多少?

    输入描述:

    第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数k (1 ≤ k < 1000000)。

    输出描述:

    n行,每行输出对应一个输入。输出应是一个非负整数。 #include<iostream> #include<algorithm> #include<string> #include<math.h> #include<stdlib.h> #include <iostream> #include <vector> #include <queue> #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<stack> #include<iostream> using namespace std; int a[1000000]={0}; long long H(int n){ if(n<=2) return n; long long x,y; if(a[n-1]!=0) x=a[n-1]; else{ x=H(n-1)2767; a[n-1]=x; } if(a[n-2]!=0) y=a[n-2]; else{ y=H(n-2)2767; a[n-2]=y; } return 2*x+y; } int main(){ int n; cin>>n; for(int i=0;i<n;i++){ int m; cin>>m; if(a[m]!=0){ cout<<a[m]2767<<endl; } else cout<<H(m)2767<<endl; } } /* 1 15 54 66 60 24 100 24 2 67 74 80 55 61 1 51 78 6 52 18 100 95 10 14 15 55 1 8 70 33 2 63 44 24 28 43 52 8 18 58 16 93 67 80 16 33 20 79 2 47 53 88 88 25 59 89 45 89 45 3 72 52 */

    第五题:https://www.nowcoder.com/practice/024c3b99edc34b84999c5830f748a841?tpId=90&tqId=30985&rp=10&ru=/ta/2018test&qru=/ta/2018test/question-ranking

    题目描述

    在十进制表示中,任意一个正整数都可以用字符’0’-‘9’表示出来。但是当’0’-‘9’这些字符每种字符的数量有限时,可能有些正整数就无法表示出来了。比如你有两个‘1’,一个‘2’,那么你能表示出11,12,121等等,但是无法表示出10,122,200等数。  现在你手上拥有一些字符,它们都是’0’-‘9’的字符。你可以选出其中一些字符然后将它们组合成一个数字,那么你所无法组成的最小的正整数是多少?

    输入描述:

    第一行包含一个由字符’0’-‘9’组成的字符串,表示你可以使用的字符。 1 ≤字符串长度≤ 1000

    输出描述:

    输出你所无法组成的最小正整数

    我的思路是统计所有数值出现的次数,然后找到最小出现次数数字,如果相同选小的数字。如果将这个数字打印,打印次数是这个数字出现次数加1;零的情况单独处理;

    #include<iostream> #include<algorithm> #include<string> #include<math.h> #include<stdlib.h> #include <iostream> #include <vector> #include <queue> #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<stack> #include<iostream> using namespace std; int main(){ string str; int a[10]={0}; a[0]++; cin>>str; for(int i=0;i<str.size();i++){ a[str[i]-'0']++; } int min=a[0]; int index=0; for(int i=1;i<10;i++){ if(min>a[i]){ min=a[i]; index=i; } } if(index==0){ cout<<'1'; for(int i=0;i<min;i++){ cout<<"0"; } } else{ for(int i=0;i<min+1;i++){ cout<<index; } } } /* 2 */

     

     

    最新回复(0)