CCF試題及答案(C++)
阿新 • • 發佈:2018-12-30
具體程式碼見下:
#include <iostream> using namespace std; int main(){ int a[30]; int n ; int count=0; while(cin>>n&&n){ a[count]=n; count++; } int sum=0; int num=1; for(int i=0;i<count;i++){ if(a[i]==1){ sum++; num=1; } else if(a[i]==2){ sum+=2*num; num++; } } cout<<sum<<endl; }
本題完~~~
具體程式碼見下:
#include <iostream> using namespace std; int main(){ int n,L,t; cin>>n>>L>>t; int a[100]; char right[100]; for(int i=0;i<n;i++){ cin>>a[i]; right[i]=true; } while(t--){ for(int i=0;i<n;i++){ if(right[i]){ a[i]++; } else{ a[i]--; } if(a[i]==L){ right[i]=false; } if(a[i]==0){ right[i]=true; } } for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(a[i]==a[j]){ right[i]=!(right[i]); right[j]=!(right[j]); } } } } cout<<a[0]; for(int i=1;i<n;i++){ cout<<" "<<a[i]; } cout<<endl; }
本題有一個注意點:由於第一個測試用例是小球按順序位置依次為4、6、8,但第二個測試用例小球就不是按從左往右的順序啦,這個需要注意!我在這一點兒上栽了個跟頭,浪費了一點兒時間!
ccf第三題一直是我不太容易突破的瓶頸,一定要仔細看,耐心做,一點兒一點兒解決問題,切勿著急上火~~~
一般ccf第三題這種型別題做起來都較為複雜,不涉及什麼演算法知識,但是要顧及許多方方面面,稍有遺漏就很容易丟分。然後考試的時候看到這種題容易讓人頭大。。。。。。哎,說多了都是淚!
個人感覺還是考試的時候先在腦海裡想好到底要採用什麼辦法來解決這道題,認真讀清題意,大致有了思路之後,再寫程式碼。
題意分析:
大體上上就是把規則和地址中的“斜槓”兩邊的字串給分開,存進vector容器中,然後再一一進行匹配。但是中間卻又許多細節需要注意,比如匹配也分幾種不同的情況。還有要注意的是:最後地址的情況,有斜槓的和沒有斜槓的規則和地址是不能匹配的!!!
程式碼見下:
#include <bits/stdc++.h>
using namespace std;
string rules[100],name[100];
void divide(vector<string> &v, string str){
v.clear();
string temp;
//temp.clear();
for(int i=1;i<str.length();i++){
if(str[i]=='/'){
v.push_back(temp);
temp.clear();
}
else{
temp+=str[i];
}
}
if(temp.size()!=0){
v.push_back(temp);
}
}
string check(string str){
//str.clear();
int flag =1;
for(int i=0;i<str.length();i++){
if(str[i]<'0'||str[i]>'9'){
flag=0;
break;
}
}
if(flag==1)
{
return "<int>";
}
else{
return "<str>";
}
}
vector <string> canshu;
bool check(string s,string rule){
vector<string> v1,v2;
divide(v1,s);
divide(v2,rule);
// for(int i=0;i<v1.size();i++){
// cout<<v1[i]<<endl;
// }
// for(int i=0;i<v2.size();i++){
// cout<<v2[i]<<endl;
// }
if(v1.size()<v2.size()){
return false;
}
if(v2.size()>=1){
if(v1.size()>v2.size()&&v2[v2.size()-1]!="<path>"){
return false;
}
}
if(v2.size()==0 && v1.size()!=0) return false;
int count=1;
canshu.clear();
for(int i=0;i<v2.size();i++){
if(v1[i]==v2[i]){
//cout<<"aa"<<endl;
count+=(v1[i].length()+1);
}
else if(check(v1[i])==v2[i]){
//cout<<"bb"<<endl;
//cout<<v1[i]<<"***"<<check(v1[i])<<endl;
count+=(v1[i].length()+1);
canshu.push_back(v1[i]);
}
else if(v2[i]=="<path>"){
//cout<<"cc"<<endl;
//cout<<s.substr(count,s.length()-count)<<endl;
canshu.push_back(s.substr(count,s.length()-count));
}
else{
return false;
}
}
if(s[s.size()-1]=='/' && rule[rule.size()-1]!='/') return false;
if(s[s.size()-1]!='/' && rule[rule.size()-1]=='/') return false;
return true;
}
int strToNum(string str){
int num=0;
for(int i=0;i<str.length();i++){
num=num*10+(str[i]-'0');
}
return num;
}
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>rules[i]>>name[i];
}
while(m--){
string s;
cin>>s;
bool flag = true;
int i;
for(i=0;i<n;i++){
flag = check(s,rules[i]);
//cout<<flag<<endl;
if(flag==true){
break;
}
}
if(flag==false){
cout<<"404"<<endl;
}
else{
cout<<name[i];
for(int i=0;i<canshu.size();i++){
if(check(canshu[i])=="<int>"){
cout<<" ";
cout<<strToNum(canshu[i]);
}
else{
cout<<" "<<canshu[i];
}
}
//cout<<endl;
puts("");
}
// for(int i=0;i<canshu.size();i++){
// cout<<canshu[i]<<endl;
// }
}
return 0;
}
執行結果見下:
5 4
/articles/2003/ special_case_2003
/articles/<int>/ year_archive
/articles/<int>/<int>/ month_archive
/articles/<int>/<int>/<str>/ article_detail
/static/<path> static_serve
/articles/2004/
year_archive 2004
/articles/1985/09/aloha/
article_detail 1985 9 aloha
/articles/hello/
404
/static/js/jquery.js
static_serve js/jquery.js