### Solutions for Skillrack CSE1002 Inlab 3

Total time : 20 mins

Challenges : 1

### Question (Guiding a Travelling Salesman)

A laptop sales company wants their salesperson to visit some cities every month and promote their sales. A code has to be written to find the sequence of visit so that he travels the minimum. Given ‘n’ cities and a home city, design an algorithm and write a C++ code to determine the number of routes to be evaluated when the problem is to be solved by brute force approach. For example, if there are four cities A, B, C, and D and ‘A’ is the home city then brute force method calculate distance of the following routes:

A B C D A

A B D C A

A C B D A

A C D B A

A B C D A

A B D C A

By default the company assign only five months every month for each of its employee but it may vary.

Input Format

Number of cities

Name of the cities

Home city name

Output Format

Number of routes to be analyzed in brute force approach

### Solution

#include< iostream > using namespace std; int fact(int n) { int f=1; for(int i=n;i > 1;i--) f*=i; return(f); } int main() { int n; char ch; cin>>n; for(int i=0;i < n;i++) cin>>ch; cin>>ch; cout<<fact(n-1); return(0); }

### Input

INPUT : 4 A B C D A

### Output

OUTPUT : 6

if you run this

int f=1;

for(int i=n;i > 1;i–)

f*=i;

return(f);

you will get the output then y do you need to give such a big program and the question itself is wrong how did you get the answer?

LikeLike

The question is correct please read it properly, but the given test case is wrong. You need to take input of all the characters and print (n-1)!

Also I created a function for factorial as code must be easy to understand to others also.

LikeLike