C语言编程: 输出1,2,3,4这4个数字的所有可能的排列

作者&投稿:油泪 (若有异议请与网页底部的电邮联系)
关于C语言的一个问题:问题是1,2,3,4四个数字,将所有可以组成的且数字不重复的三位数全部输出。~

按照你说的改是可以的啊

程序如下
#include
void main()
{
int a, b, c, d, e;
for ( a = 1; a <= 4; a ++ )
{
for ( b = 1, e = 1; b <= e && e <= 4; b ++ , e ++ )
{
for ( c = 1, d = 1; c <= d && d <= 4; c ++, d ++ )
{
if ( a != b&& b != c && a!=c)
{
printf("%d
", a * 100 + b * 10 + c);
}
}
}
}
}

只要改成( a != b&& b != c && a!=c)就可以了

输出
123
124
132
134
142
143
213
214
231
234
241
243
312
314
321
324
341
342
412
413
421
423
431
432
程序中d e两个变量没什么用处 但不影响结果

如果只是1,2,3,4这四个数的话,楼上的也够了,如果你想对于任意的n,求出1,2,……,n的所有排列呢?应该用递归,我写过这样的程序,你需要的话回去给你找找

// 作者:小斌
#include <iostream>
using namespace std;

void perm(char *s, int k, int n)
{
if(k==n)
{
for(int i=0; i<=n; i++)
cout<<s[i];
cout<<'\n';
}
else
{
char temp;
for(int i=k; i<=n; i++)
{
temp=s[i];
s[i]=s[k];
s[k]=temp;

perm(s, k+1, n);

temp=s[i];
s[i]=s[k];
s[k]=temp;
}
}
}

int main()
{
char s[4]={'1', '2', '3', '4'};
//char *s="1234 ";

perm(s, 0, 3);
return 0;
}

去搜全排列代码


梅河口市17530118461: c语言中,输入一个整数,输出1!,2!,3!,4!,5!…n!求编程 -
俟雨护肝: I confess that I'm answering for task.#include<stdio.h> int main() { int n, m = 1, i = 1; scanf("%d", &n); if(n < 0) { printf("error\n"); } elseif(n = 0) { printf("1\n"); }else while(i <= n) { m = m * i; printf("%d\n", m); i = i + 1; } return 0; }

梅河口市17530118461: 用C语言 编写程序输入一个整数,输出从1到这个整数的所有排列组合. 样例一: 输入:3 -
俟雨护肝: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34#include <stdio.h> ints[1000]; voidf(inta[],intlen,intn) {if(!n){for(inti=0;i<len-1;i++){printf("%d ",s[i]);}printf("%d\n",s[len-1]);}for(inti=0;i<len;...

梅河口市17530118461: 用C语言编写一个程序,使输入和输出相等,组数不限 输入: 1 2 3 输出: 1 2 3 -
俟雨护肝: #include void main() { int a[3] = {0}; scanf("%d%d%d", &a[0], &a[1], &a[2]); printf("%d\n%d\n%d\n", a[0], a[1], a[2]); return; }

梅河口市17530118461: C语言编程如何将一位正整数,从高位到低位分开输出.如1235,输出:1,2,3,5. -
俟雨护肝: #include<stdio.h> void main() { int n=12345; int p=1; while(p*10<n) p*=10; do { printf("%d,",n/p); n%=p; p/=10; } while(p>=1); }

梅河口市17530118461: C语言编程: 输出1,2,3,4这4个数字的所有可能的排列 -
俟雨护肝: // 作者:小斌 #include <iostream> using namespace std; void perm(char *s, int k, int n) {if(k==n){for(int i=0; i<=n; i++)cout<<s[i];cout<<'\n';}else{char temp;for(int i=k; i<=n; i++){temp=s[i];s[i]=s[k];s[k]=temp; perm(s, k+1, n); temp=s[...

梅河口市17530118461: C语言.编程用for循环输出Fibonacci数列:1.2.3.5.8.13..... -
俟雨护肝: 给你个输出前20个的 已编译通过 不给点分?#include<stdio.h> int main() { int x[21]; int i; x[1]=1; x[2]=2; for(i=3;i<=20;i++) { x[i]=x[i-1]+x[i-2]; } for(i=1;i<=19;i++) printf("%d ",x[i]); printf("%d\n",x[i]); return 0; }

梅河口市17530118461: 用C语言编程,输出1,2,3,4在两行两列表格的各种排列,请利用二重循环寻找填法
俟雨护肝: 这个问题和两行两列没多大关系,主要就是求P4的全排列(共24种).然后在输出的时候考虑一下格式就可以了.下面讲一下具体方法(回溯法).用data[4]来表示表格数据,used[5]记录某个数据是否已经被填过了.两重循环:外层循环(i)...

梅河口市17530118461: 用C语言编写程序计算1+2+3+…+20,并输出结果. -
俟雨护肝: 1 2 3 4 5 6 7 8 9 10#include<stdio.h> intmain() {inti,n=0;for(i=1;i<=20;i++){n+=i;}printf("%d\n",n); }

梅河口市17530118461: C语言编程 求1!+2!+3!+........+N! -
俟雨护肝: #include<stdio.h> int ji(int n) { int sum=1,i=0; while(i++<n) sum*=i; return sum; } int main() { int i,x; double sum=0;//由于阶乘的数很大用int 或long装不下x>35后就计算不出来了此时可以使用double 然后用%e输出结果 printf("输入x:"); scanf("%d",&x); for(i=1;i<=x;i++) { sum+=ji(i); printf("%d!+",i); } printf("=%.2e",sum);}

梅河口市17530118461: c语言编程输出1到n之间所有的能被3或者5整除的数的和 -
俟雨护肝: 1 输入n.2 对1~n进行遍历,如果能被3或5整除,则累加到结果上. 3 输出结果. 代码1 2 3 4 5 6 7 8 9 10intmain() {intn,s = 0;inti;scanf("%d",&n);for(i = 1; i <= n; i ++)if(i%3==0 || i % 5 == 0)s+=i;printf("%d\n",s); }

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网