• 征战蓝桥 —— 2015年第六届 —— C/C++A组第5题——九数组分数


    九数组分数

    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;
    }
    

    注意:只填写缺少的内容,不要书写任何题面已有代码或说明性文字。

    代码

    #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;
    }
    
  • 相关阅读:
    python中的pip
    代码块
    Java 中的main方法
    mysql的decimal(10,0) not null问题
    vue 超大 table
    apache2 的https配置和代理https后端nodejs配置
    3分钟解决MySQL 1032 主从错误(转)
    CentOS 7 Apache服务的安装与配置(转)
    mybatis pagehelper多数据源配置的坑
    web worker的用法及应用场景(转)
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338964.html
Copyright © 2020-2023  润新知