Click here to Skip to main content
14,238,485 members
Rate this:
Please Sign up or sign in to vote.
def readinput():
  n = int(input())     # Length
  for j in range(n):   
    nextnum = int(input())  # Read each value
    insequence.append(nextnum)
    best.append(0)     # Initialize best[k] for each position
  return

def solve():
  for j in range(len(insequence)):
    # Collect best[k] for values to left of j that divide insequence[j]
    prev = [ best[k] for k in range(j) if insequence[j]%insequence[k] == 0 ]
    if prev:
       best[j] = 1 + max(prev)
    else:
       best[j] = 1

insequence = []
best = []
readinput()
solve()
print(max(best))


What I have tried:

I don't know where to start. please help
Posted
Updated 17-Oct-18 10:54am
v4
Comments
Patrice T 17-Oct-18 15:27pm
   
What about learning Python to understand this code, then C to write the translation?
Rate this:
Please Sign up or sign in to vote.

Solution 2

C++ is quite expressive:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> readinput()
{
  size_t n;
  cin >> n; 
  vector<int> v(n);
  for (auto & x : v)
    cin >> x;
  return v;
}
vector <int> solve(const vector<int> & inseq)
{
  auto size = inseq.size();
  vector<int> best(size);
  for (size_t j=0; j<size; ++j)
  {
    vector<int> prev;
    for (size_t k=0; k<j; ++k)
      if ( inseq[j] % inseq[k] == 0)
        prev.push_back(best[k]);
    best[j] = prev.size() > 0 ? 1 + *max_element(prev.begin(), prev.end()) : 1;
  }
  return best;
}

int main()
{
  auto inseq = readinput();
  auto best = solve(inseq);
  cout << *max_element(best.begin(), best.end()) << endl;
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Forget it. Python is too different from C to make any conversion simple. Many of the features of Python have no direct equivalent in C so you would need to design your own alternatives. Just use the design details of the program to write it in C.
   
Comments
CPallini 17-Oct-18 16:55pm
   
Maybe difficult to implement using C. On the other hand, C++...
Richard MacCutchan 18-Oct-18 4:02am
   
That's easy for you to say ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100