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

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

相关文章

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

购买磨轮平衡机时应该注意什么问题和技巧

在购买磨轮平衡机时,您应该注意以下几个关键点: 平衡精度 平衡精度是衡量平衡机性能的核心指标,直接影响到不平衡量的检测与校准的准确性,从而决定磨轮的振动和噪声水平。高精度的平衡机能显著减少振动和噪声,提高磨削加工的精度。 转速范围 宽广的转速范围意味着平衡机能够处理更多种类的磨轮,适应不同的工作条件和规格要求。 振动监测能力 振动监测能力是评估平衡机性能的重要因素。通过传感器实时监

缓存雪崩问题

缓存雪崩是缓存中大量key失效后当高并发到来时导致大量请求到数据库,瞬间耗尽数据库资源,导致数据库无法使用。 解决方案: 1、使用锁进行控制 2、对同一类型信息的key设置不同的过期时间 3、缓存预热 1. 什么是缓存雪崩 缓存雪崩是指在短时间内,大量缓存数据同时失效,导致所有请求直接涌向数据库,瞬间增加数据库的负载压力,可能导致数据库性能下降甚至崩溃。这种情况往往发生在缓存中大量 k

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

EMLOG程序单页友链和标签增加美化

单页友联效果图: 标签页面效果图: 源码介绍 EMLOG单页友情链接和TAG标签,友链单页文件代码main{width: 58%;是设置宽度 自己把设置成与您的网站宽度一样,如果自适应就填写100%,TAG文件不用修改 安装方法:把Links.php和tag.php上传到网站根目录即可,访问 域名/Links.php、域名/tag.php 所有模板适用,代码就不粘贴出来,已经打

跨系统环境下LabVIEW程序稳定运行

在LabVIEW开发中,不同电脑的配置和操作系统(如Win11与Win7)可能对程序的稳定运行产生影响。为了确保程序在不同平台上都能正常且稳定运行,需要从兼容性、驱动、以及性能优化等多个方面入手。本文将详细介绍如何在不同系统环境下,使LabVIEW开发的程序保持稳定运行的有效策略。 LabVIEW版本兼容性 LabVIEW各版本对不同操作系统的支持存在差异。因此,在开发程序时,尽量使用

【VUE】跨域问题的概念,以及解决方法。

目录 1.跨域概念 2.解决方法 2.1 配置网络请求代理 2.2 使用@CrossOrigin 注解 2.3 通过配置文件实现跨域 2.4 添加 CorsWebFilter 来解决跨域问题 1.跨域概念 跨域问题是由于浏览器实施了同源策略,该策略要求请求的域名、协议和端口必须与提供资源的服务相同。如果不相同,则需要服务器显式地允许这种跨域请求。一般在springbo

题目1254:N皇后问题

题目1254:N皇后问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 题目描述: N皇后问题,即在N*N的方格棋盘内放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在同一斜线上。因为皇后可以直走,横走和斜走如下图)。 你的任务是,对于给定的N,求出有多少种合法的放置方法。输出N皇后问题所有不同的摆放情况个数。 输入