There seems to be a test case did not pass ... Do not know where to do wrong ...
//
Main.cpp
11_29
//
Created by Han Xue on 11/29/16.
Copyright? 2016 Han Xue. 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;
Positive solution:
#include <iostream>#include<vector>#include<Set>#include<math.h>using namespacestd;voidGet_yue_shu (intN, vector<int>&a) { for(intI=2; I<=sqrt (n); i++){ if(n%i==0) {a.push_back (i); if(n/i! =i) a.push_back (n/i); } }} intMain () {intn,m; while(cin>>n>>m) {Vector<int> Res (m+1,0); Res[n]=1; for(inti=n;i<=m;i++) {vector<int>A; //position I cannot reach, skip if(res[i]==0) Continue; Get_yue_shu (i, a); //record the minimum number of steps from the starting point to I for(intj=0; J<a.size (); j + +){ //points to be reached starting from point I if((a[j]+i) <=m&&res[a[j]+i]!=0) //other points can also be reached, compare size, record the minimum number of stepsRes[a[j]+i] = min (Res[a[j]+i], res[i] +1); Else if((a[j]+i) <=m)//minimum number of steps to reach point I plus 1Res[a[j]+i] = Res[i] +1; } } if(res[m]==0) cout<<-1<<Endl; Elsecout<<res[m]-1<<Endl; } return 0;}
Daily algorithm Questions