//貌似有测试用例没通过。。。不知道哪里做的不对。。。
//
// main.cpp
// 11_29
//
// Created by 韩雪滢 on 11/29/16.
// Copyright ? 2016 韩雪滢. All rights reserved.
//
#include <iostream>
using namespace std;
#define MAX 10000000
int step = MAX;
int currentStep = 0;
int start,terminal;
class Node{
public:
int index;
int *edge;
int num;
Node(int i){
index = i;
calEdge();
}
void calEdge(){
num = 0;
int *tempedge = new int[index];
for(int i=2;i<index/2;i++){
if(index % i == 0)
{
tempedge[num] = i;
num++;
}
}
if(num >0){
edge = new int[num-1];
for(int j=0;j< num;j++)
edge[j] = tempedge[j];
}
}
};
void findStepNum(int s){
Node startNode(s);
for(int i=0;i<startNode.num;i++){
int nextNode = startNode.index + startNode.edge[i];
if(nextNode < terminal){
currentStep ++;
findStepNum(nextNode);
}
if(nextNode == terminal){
if(currentStep != 0 && currentStep < step){
step = currentStep;
break;
}
}
}
currentStep--;
}
int main(int argc, const char * argv[]) {
cin >> start >> terminal;
findStepNum(start);
if(step != MAX)
cout << ++step <<endl;
else
cout << -1 << endl;
return 0;
正解:
#include<iostream> #include<vector> #include<set> #include<math.h> using namespace std; void get_yue_shu(int n, vector<int>&a){ for(int i=2;i<=sqrt(n);i++){ if(n%i==0){ a.push_back(i); if(n/i != i) a.push_back(n/i); } } } int main(){ int n,m; while(cin>>n>>m){ vector<int> res(m+1, 0); res[n] = 1; for(int i=n;i<=m;i++){ vector<int>a; //位置i无法到达,跳过 if(res[i]==0) continue; get_yue_shu(i, a); //记录从起点到i的最小步数 for(int j=0;j<a.size();j++){ //由点i出发能到达的点 if((a[j]+i)<=m&&res[a[j]+i]!=0) //其它点也能到达,比较大小,记录最小步数 res[a[j]+i] = min(res[a[j]+i], res[i] + 1); else if((a[j]+i)<=m) //到达点i的最小步数加1 res[a[j]+i] = res[i] + 1; } } if(res[m]==0) cout<<-1<<endl; else cout<<res[m]-1<<endl; } return 0; }
原文:http://www.cnblogs.com/HackHer/p/6115360.html