| |

VerySource

 Forgot password?
 Register
Search
View: 18|Reply: 7

An algorithmic problem

[Copy link]

1

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-3-22 11:30:01
| Show all posts |Read mode
The user enters a unique string,
Write a program to print out the full arrangement of this string
For example: if you enter "123", it will print "123", "132"; "213"; "321"; "231"; "312"
Requirements, written in recursive method, c language.

This is a java topic, it is easy to compile it with java, but I just learned c soon, but I always make mistakes when I want to compile it with c, please help.
Reply

Use magic Report

0

Threads

23

Posts

8

Credits

Newbie

Rank: 1

Credits
8

 China

Post time: 2020-7-1 16:15:01
| Show all posts
Also know what is the arrangement?
Reply

Use magic Report

0

Threads

73

Posts

26

Credits

Newbie

Rank: 1

Credits
26

 China

Post time: 2020-7-2 23:30:01
| Show all posts
#include <stdio.h>
#define n 4 /*Find all permutations of 1-n*/
int a[n],d[n],e[n];
void main()
{
               int i,q,p,k,r,total;
               a[1]=1;
               for(i=2;i<=n;i++)
               {
                               a[i]=i;
                               d[i]=i;
                               e[i]=-1;
               }
               total=0;
               s2:
               q=0;
                               for(i=1;i<=n;i++)printf("%d ",a[i]);
                               printf("\n");
                               total+=1;
                               for(k=n;k>=2;k--)
                               {
                                               d[k]+=e[k];
                                               p=d[k];
                                               if(p==k)
                                                               e[k]=-1;
                                               else if(p==0)
                                   {e[k]=1;q++;}
                                   else {
                                               p+=q;
                                                               r=a[p];
                                                               a[p]=a[p+1];
                                                               a[p+1]=r;
                                                               goto s2;
                                               }
                               }
               printf("total= %d\n",total);
}
Reply

Use magic Report

0

Threads

73

Posts

26

Credits

Newbie

Rank: 1

Credits
26

 China

Post time: 2020-7-3 01:00:01
| Show all posts
The data assignment process needs to be modified according to the input,
Is to receive input data one bit at a time,
Or use char array,
Then you can also access the array elements by element ~~~~~~
Reply

Use magic Report

0

Threads

3

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-7-3 15:00:01
| Show all posts
#include <stdio.h>

#define MAX 10
int used[MAX];
int result[MAX];
int N;

void print(){
int i;
for(i = 0; i <N; i++)
printf("%d ", result[i]);
printf("\n");
}
void proc(int step){
int i;
if(step == N)
print();
else{
for(i = 0; i <N; i++){
if(!used[i]){
used[i] = 1;
result[step] = i + 1;
proc(step + 1);
used[i] = 0;
}
}
}
}

main(){
scanf("%d",&N);
proc(0);
}
Reply

Use magic Report

1

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

 Author| Post time: 2020-7-6 10:45:01
| Show all posts
None of you understand the meaning of the question
zjwzjw
You don't use recursion. If you use a for loop, if the string is more complicated, for example, 5 or 6 characters: "ABCDE", it will be very confusing.


64081874
Your useful recursion, the idea is also very clear, and gave me a lot of tips, but you did not see clearly, I want a string
Please help to change the program so that you can use the string as a parameter
Whoever made it according to the meaning of the title, I will post another 50 points, thank you for your help
Knot this post on the 7th!
Reply

Use magic Report

0

Threads

5

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

Post time: 2020-7-9 21:15:01
| Show all posts
Change the above

#include <stdio.h>

#define MAX 20
int used[MAX];
int result[MAX];
int str[MAX];
int len;

void print(){
int i;
for(i = 0; i <len; i++)
printf("%d ", result[i]);
printf("\n");
}
void proc(int step){
int i;
if(step == len)
print();
else{
for(i = 0; i <len; i++){
if(!used[i]){
used[i] = 1;
result[step] = str[i];
proc(step + 1);
used[i] = 0;
}
}
}
}

main(){
gets(str);
len=strlen(str);
proc(0);
}
Reply

Use magic Report

0

Threads

4

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-7-11 16:45:01
| Show all posts
Do not repeat the string?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list