如有侵权立即删除
7-1
#include <bits/stdc++.h>
using namespace std;
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
int n;
int a = 0,b=1;
cin >> n;
int res = n;
while(true)
{
if(a > n) break;
if(abs(n - a) > abs(n - b)) res = b;
else res = a;
int t = a + b;
a = b;
b = t;
}
cout << res << endl;
return 0;
}
7-2
#include <bits/stdc++.h>
using namespace std;
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
string s,t;
cin >> s >> t;
int n = s.size();
int m = t.size();
pair<int,int> res = {-1,-1};
for(int i=0;i+m<=n;i++)
{
int p = 0,j=i;
for(;j<n&&p<m;j++)
if(t[p] == s[j])
p ++ ;
if(p == m)
{
if(res.first == -1 || res.second - res.first > j - i)
res = {i,j};
}
}
cout << s.substr(res.first,res.second-res.first) << endl;
return 0;
}
7-3
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n;
string s[N];
int pre[N];
map<string,int> mp;
stack<int> stk;
int idx;
int d[N];
int work(string &s)
{
int c = 0;
reverse(s.begin(),s.end());
while(s.size() > 0 && s.back() == ' ')
{
s.pop_back();
c++;
}
reverse(s.begin(),s.end());
return c;
}
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
memset(pre,-1,sizeof pre);
cin >> n;
int last = -1;
getline(cin,s[0]);
for(int i=0;i<n;i++)
{
string str;
getline(cin,str);
int c = work(str);
mp[str] = ++ idx;
s[idx] = str;
d[idx] = c;
while(stk.size() > 0 && d[stk.top()] >= c)
stk.pop();
if(stk.size() > 0)
pre[idx] = stk.top();
stk.push(idx);
}
int T;
cin >> T;
while(T--)
{
string str; cin >> str;
if(mp.count(str))
{
vector<string> res;
int S = mp[str];
while(S != -1)
{
res.push_back(s[S]);
S = pre[S];
}
for(int i=res.size()-1;i>=0;i--)
{
if(i!=0) cout << res[i] << "->";
else cout << res[i] << endl;
}
}
else
{
printf("Error: %s is not found.\n",str.c_str());
}
}
return 0;
}
7-4
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
bool st[N];
vector<int> ver[N];
string equation[N];
vector<int> e[N];
int nd[N],m;
vector<int> res;
void dfs(int u)
{
if(u == m)
{
for(auto x : res)
{
cout << equation[x] << endl;
}
exit(0);
}
int t = nd[u];
for(auto x : ver[t])
{
bool flag = true;
for(auto y : e[x])
if(!st[y])
flag = false;
if(flag)
{
for(auto y : e[x])
st[y] = false;
res.push_back(x);
dfs(u + 1);
res.pop_back();
for(auto y : e[x])
st[y] = true;
}
}
}
string get(int x)
{
string s;
if(x <= 9) s = "0" + to_string(x);
else s = to_string(x);
return s + " -> " + s;
}
bool CMP(int a,int b)
{
return e[a] < e[b];
}
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
int n;
cin >> n;
for(int i=0;i<n;i++)
{
int x; cin >> x;
st[x] = true;
}
cin >> m;
for(int i=0;i<m;i++)
cin >> nd[i];
int k;
cin >> k;
string str;
getline(cin,str);
for(int i=0;i<k;i++)
{
getline(cin,str);
stringstream ssin(str);
string s;
vector<int> v;
while(ssin >> s)
{
if(isdigit(s[0]))
{
v.push_back(stoi(s));
}
}
equation[i] = str;
ver[v.back()].push_back(i);
for(int j=0;j<v.size()-1;j++)
{
e[i].push_back(v[j]);
}
}
for(int i=0;i<m;i++)
{
int x = nd[i];
ver[x].push_back(i + k);
e[i + k].push_back(x);
equation[i + k] = get(x);
sort(ver[nd[i]].begin(),ver[nd[i]].end(),CMP);
}
dfs(0);
return 0;
}
也就是说a, b是构成这个输入的fib数n的数(本身也是fib),或者是n后面一个fib数。(根据
break
位置比n大即下一个数)。因为fib是一个一个加上来的。👍