天平秤杂币问题一般解求解程序

2023-12-19 22:50

本文主要是介绍天平秤杂币问题一般解求解程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

(这是我很久之前写的一个程序,目前博客上还没有什么文章,就算充个数把吐舌头

本程序的问题描述可参见:Balance puzzle,或者也可以阅读我之前写的一个PPT。


头文件balance.h如下:

/************************************************************************************/
/*        This demo implements the coding method for soving n coins problem         */
/*                   No Copyrights! Feel free to copy it!                           */
/*----------------------------------------------------------------------------------*/
/*Algorithm:                                                                        */
/*1. Violence search the solution for 3<=n<=12                                      */
/*2. Use the induction steps in the proof to get the following solution             */
/************************************************************************************/#include<stdio.h>
#include<malloc.h>
#include<string.h>#define MAX 100;//k is no more than 100typedef struct set{int n;char * * node;
}SET;void convert(char * set[],int n,int k);
int thresh(int k);
SET * searchT(int n,int k,char * a);
SET * search(int n,int k);
void show(int k,bool state,SET * a);
bool check(SET * a); 
搜索程序:

#include"balance.h"char * s1[3]={"01","20","12"};
char * s2[4]={"001","010","220","102"};
char * s3[5]={"001","010","220","012","120"};
char * s4[6]={"001","010","022","110","202","021"};
char * s5[7]={"001","010","011","220","202","120","102"};
char * s6[8]={"001","010","011","220","202","012","121","122"};
char * s7[9]={"001","010","011","220","202","120","201","112","122"};
char * s8[10]={"001","010","011","220","202","012","120","102","221","100"};
char * s9[11]={"001","010","111","011","220","202","120","102","221","212","122"};
char * s10[12]={"001","010","111","011","220","202","012","120","102","221","122","200"};/*calculate the threshold (3^k-3)/2*/
int thresh(int k)
{int tmp=1;for(int i=0;i<k;i++)tmp=tmp*3;return (tmp-3)/2;
}/*put k char 'a' ahead of a1*/
char * putch(char a,int k,char * a1)
{int n=strlen(a1);char * result;result=(char *)malloc(n+k+1);for(int i=0;i<n+k;i++){if(i<k)*(result+i)=a;else*(result+i)=*(a1+i-k);}*(result+n+k)='\0';return result;
}/*make a srting array that add k chars 'c' ahead of each string of a*/
char * * makeSet(char * a[],int n,int k,char c)
{char * * result;result=(char * *)malloc(sizeof(char *)*n);for(int i=0;i<n;i++){*(result+i)=putch(c,k,a[i]);}return result;
}/*make a SET from a string array*/
SET * MakeSET(char * * a,int n)
{SET * result;result=(SET *)malloc(sizeof(set));result->n=n;result->node=a;return result;
}char * * getlow(int n)
{switch(n){case 3:return s1;case 4:return s2;case 5:return s3;case 6:return s4;case 7:return s5;case 8:return s6;case 9:return s7;case 10:return s8;case 11:return s9;case 12:return s10;default:return NULL;}
}SET * below12(int k,int n)
{if(n==3){if(k<3)return MakeSET(getlow(3),3);elsereturn MakeSET(makeSet(getlow(3),3,k-2,'0'),3);}if(n>12||n<3)return NULL;SET * result;char * * tmp;tmp=(char * *)malloc(sizeof(char *)*n);result=(SET *)malloc(sizeof(set));result->n=n;tmp=makeSet(getlow(n),n,k-3,'0');result->node=tmp;return result;
}/*combine two set*/
SET * combine(SET * a,SET * b)
{SET * result;result=(SET *)malloc(sizeof(set));char * * tmp;tmp=(char * *)malloc(sizeof(char *)*(a->n+b->n));for(int i=0;i<a->n+b->n;i++){if(i<a->n)*(tmp+i)=*(a->node+i);else*(tmp+i)=*(b->node+i-a->n);}result->n=a->n+b->n;result->node=tmp;return result;
}char * Inverse(char * a)
{char * result;result=(char *)malloc(strlen(a)+1);for(int i=0;i<strlen(a);i++){if(*(a+i)=='0')*(result+i)='0';else if(*(a+i)=='1')*(result+i)='2';else*(result+i)='1';}*(result+strlen(a))='\0';return result;
}/*The case when n=(3^k-3)/2*/
SET * searchT(int n,int k,char * a)
{SET * temp,* temp1,* temp2;char * * tmp,c;if(n!=thresh(k))return NULL;tmp=(char * *)malloc(sizeof(char *)*3);if(n==3){if(a[0]=='2'&&a[1]=='2'){char * s1="10",*s2="02",*s3="21";*tmp=s1;*(tmp+1)=s2;*(tmp+2)=s3;}else{char * s1="10",*s2="01",*s3="22";*tmp=s1;*(tmp+1)=s2;*(tmp+2)=s3;}return MakeSET(tmp,3);}else{temp=searchT((n-3)/3,k-1,a+1);if(*a=='1')c='2';elsec='1';*tmp=putch(*a,1,putch('0',k-1,""));*(tmp+1)=putch(c,1,a+1);*(tmp+2)=putch('0',1,Inverse(a+1));temp1=MakeSET(tmp,3);temp2=combine(MakeSET(makeSet(temp->node,temp->n,1,'0'),temp->n),MakeSET(makeSet(temp->node,temp->n,1,'1'),temp->n));temp2=combine(MakeSET(makeSet(temp->node,temp->n,1,'2'),temp->n),temp2);return combine(temp2,temp1);}}/*
The algorithm we used here is different from we 
presented in PPT, since we just simply let the 
uncantained string to be "2...2". 
*/
SET * search(int n,int k)
{int thre=thresh(k),threp=thresh(k-1);if(n==thre)return searchT(n,k,putch('2',k,""));if(n>thre||n<3)return NULL;else if(n<=12)return below12(k,n);else{SET * result,* temp1,* temp2,*temp3,*temp4,*temp5;char * * tmp;if(n<2*threp+1){if(n%2==0){temp1=search(n/2,k-1);temp2=MakeSET(makeSet(temp1->node,temp1->n,1,'1'),temp1->n);temp3=MakeSET(makeSet(temp1->node,temp1->n,1,'2'),temp1->n);return combine(temp2,temp3);}else{temp1=search((n-3)/2,k-1);temp2=search(3,k-1);temp3=MakeSET(makeSet(temp1->node,temp1->n,1,'1'),temp1->n);temp4=MakeSET(makeSet(temp1->node,temp1->n,1,'2'),temp1->n);temp1=MakeSET(makeSet(temp2->node,temp2->n,1,'0'),temp2->n);return combine(combine(temp3,temp1),temp4);}}else if(n==2*threp+2){temp1=search((n-4)/2,k-1);temp2=search(4,k-1);temp3=MakeSET(makeSet(temp1->node,temp1->n,1,'1'),temp1->n);temp4=MakeSET(makeSet(temp1->node,temp1->n,1,'2'),temp1->n);temp1=MakeSET(makeSet(temp2->node,temp2->n,1,'0'),temp2->n);return combine(combine(temp3,temp1),temp4);}else if(n<=3*threp){temp1=search(threp,k-1);temp2=search(n-2*threp,k-1);temp3=MakeSET(makeSet(temp1->node,temp1->n,1,'1'),temp1->n);temp4=MakeSET(makeSet(temp1->node,temp1->n,1,'2'),temp1->n);temp1=MakeSET(makeSet(temp2->node,temp2->n,1,'0'),temp2->n);return combine(combine(temp3,temp1),temp4);}else{int j=n-2-2*threp;temp1=search(threp,k-1);temp2=search(j,k-1);temp3=MakeSET(makeSet(temp1->node,temp1->n,1,'1'),temp1->n);temp4=MakeSET(makeSet(temp1->node,temp1->n,1,'2'),temp1->n);temp1=MakeSET(makeSet(temp2->node,temp2->n,1,'0'),temp2->n);temp5=combine(combine(temp3,temp1),temp4);tmp=(char **)malloc(sizeof(char *)*2);*tmp=putch('1',k,"");*(tmp+1)=putch('2',k,"");return combine(temp5,MakeSET(tmp,2));}}
}bool check(SET * a)
{char ** temp=a->node;int k=strlen(*(a->node)),*tmp;tmp=(int *)malloc(sizeof(int)*k);for(int i=0;i<k;i++)tmp[i]=0;for(int i=0;i<a->n;i++){for(int j=0;j<k;j++){if(*(*(temp+i)+j)=='1')tmp[j]++;else if(*(*(temp+i)+j)=='2')tmp[j]--;}}for(int i=0;i<k;i++)if(tmp[i]!=0)return false;return true;
}
演示主程序:

#include"balance.h"int main()
{SET *result;int n,k;printf("Please input the order: ");scanf("%d",&n);printf("Please input the dimension: ");scanf("%d",&k);result=search(n,k);if(result==NULL){printf("Can't find solution!\n");system("pause");exit(1);}else{printf("The constructible set is as follow:\n");for(int i=0;i<result->n;i++)printf("%s\t",*(result->node+i));printf("\n");}show(1,true,result);system("pause");return 0;
}


这篇关于天平秤杂币问题一般解求解程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/513978

相关文章

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

MySQL主从同步延迟问题的全面解决方案

《MySQL主从同步延迟问题的全面解决方案》MySQL主从同步延迟是分布式数据库系统中的常见问题,会导致从库读取到过期数据,影响业务一致性,下面我将深入分析延迟原因并提供多层次的解决方案,需要的朋友可... 目录一、同步延迟原因深度分析1.1 主从复制原理回顾1.2 延迟产生的关键环节二、实时监控与诊断方案

SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法

《SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法》在SQLyog中执行存储过程时出现的前置缩进问题,实际上反映了SQLyog对SQL语句解析的一个特殊行为,本文给大家介绍了详... 目录问题根源正确写法示例永久解决方案为什么命令行不受影响?最佳实践建议问题根源SQLyog的语句分

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

Redis 热 key 和大 key 问题小结

《Redis热key和大key问题小结》:本文主要介绍Redis热key和大key问题小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、什么是 Redis 热 key?热 key(Hot Key)定义: 热 key 常见表现:热 key 的风险:二、

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图