I would like to ask all my fellow programmers only about efficiency . I am currently solving problems that might be posed in an interview, and I came across known line-breaks. The code that I wrote below may be the most common in the history of programming, however, I do not know its status, since I did not check any solution.
In short, is the program I encoded below an appropriate solution? Or it can be done even more efficiently. By asking, because if I stumbled upon one day, I would like to be sure that I will implement one of the best approaches to this problem.
#include <iostream>
using namespace std;
int fac(int num)
{
int result=1;
for(int i=1;i<=num;i++)
result*=i;
return result;
}
int main(int argc, const char * argv[])
{
string str="abcd";
int limit=fac(str.size());
int mod=str.size();
for(int i=0;i<limit;i++){
swap(str[i%mod],str[(i+1)%mod]);
cout<<str<<endl;
}
return 0;
}
source
share