牛客練習記錄第三週

第一題: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)%32767;
		a[n-1]=x;
	}
	if(a[n-2]!=0) y=a[n-2];
	else{
		y=H(n-2)%32767;
		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]%32767<<endl;
		}
		else cout<<H(m)%32767<<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


*/

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章