九数组分数

九数组分数
1,2,3...9 这九个数字组成一个分数,其值恰好为1/3,如何组法?

下面的程序实现了该功能,请填写划线部分缺失的代码。


#include <stdio.h>  
  
void test(int x[])  
{  
    int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];  
    int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];  
      
    if(a*3==b) printf("%d / %d\n", a, b);  
}  
  
void f(int x[], int k)  
{  
    int i,t;  
    if(k>=9){  
        test(x);  
        return;  
    }  
      
    for(i=k; i<9; i++){  
        {t=x[k]; x[k]=x[i]; x[i]=t;}  
        f(x,k+1);  
        _____________________________________________ // 填空处  
    }  
}  
      
int main()  
{  
    int x[] = {1,2,3,4,5,6,7,8,9};  
    f(x,0);   
    return 0;  
}  


思路:f(x,k+1)回溯之后,将交换后的结果还原.


#include<bits/stdc++.h>
#include <stdio.h>  
#include <string.h>  
#include<iostream>
#include<algorithm>
#include <stdio.h>  
void test(int x[])  
{  
    int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];  
    int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];  
      
    if(a*3==b) printf("%d / %d\n", a, b);  
}  
  
void f(int x[], int k)  
{  
    int i,t;  
    if(k>=9){  
        test(x);  
        return;  
    }  
      
    for(i=k; i<9; i++){  
        {t=x[k]; x[k]=x[i]; x[i]=t;}  
        f(x,k+1);  
        {t=x[k]; x[k]=x[i]; x[i]=t;}  // 填空处  恢复之前的操作
    }  
}  
      
int main()  
{  
    int x[] = {1,2,3,4,5,6,7,8,9};  
    f(x,0);   
    return 0;  
}  
参考博客:::https://www.cnblogs.com/brifuture/p/6553665.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值