当前位置:首页 » 编程语言 » 找伪币问题c语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

找伪币问题c语言

发布时间: 2022-08-09 17:05:39

⑴ POJ2692编程,假币问题,麻烦给个c语言范例

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define COL 5

char left[3][COL],right[3][COL],result[3][COL];
char arr[13]="ABCDEFGHIJKL";

int lighter(char c) {
int i;
for(i=0;i<3;i++) {
if(result[i][0]=='e') {
if(!strchr(left[i],c) != !strchr(right[i],c)) return 0;
}
if(result[i][0]=='u') {
if(!strchr(right[i],c)) return 0;
}
if(result[i][0]=='h') {
if(!strchr(left[i],c)) return 0;
}
}
return 1;
}

int heavier(char c) {
int i;
for(i=0;i<3;i++) {
if(result[i][0]=='e') {
if(!strchr(left[i],c) != !strchr(right[i],c)) return 0;
}
if(result[i][0]=='h') {
if(!strchr(right[i],c)) return 0;
}
if(result[i][0]=='u') {
if(!strchr(left[i],c)) return 0;
}
}
return 1;
}

int main() {
int i;
while(1) {
memset(left,'\0',3*COL);
memset(right,'\0',3*COL);
memset(result,'\0',3*COL);
for(i=0;i<3;i++) {
scanf("%s %s %s",left[i],right[i],result[i]);
}

for(i=0;i<12;i++) {
if(lighter(arr[i])) {
printf("%c is the counterfeit coin and it is lighter\n",arr[i]);
break;
}
else printf("%c is not the counerfeit coin\n",arr[i]);

if(heavier(arr[i])) {
printf("%c is the counterfeit coin and it is heavy\n",arr[i]);
break;
}
}
}

return 1;
}

⑵ C语言编程,有n个硬币,一直有一个硬币是假的,质量比真币小,现有一天平问最多需要撑几次可以把假币找出来

#include "stdio.h"
void main()
{ int i,n,count,m;
printf("输入测试次数:");
scanf("%d",&n);//测试次数
printf(" ");
for(i=0;i<n;i++)
{
printf("第%d次输入硬币个数:",i+1);
scanf("%d",&count);//硬币个数
printf(" ");
m=0;//m用于存储比较次数,每次折半比较
while(count>2)
{m++;
count=count/2;
}
printf("最大比较次数:");
printf("m=%d ",m);
printf(" ");

}
}

⑶ 请问一道C++编程题:求解查找假币问题

#include "stdio.h"
#include "string.h"
typedef enum {
even, up, down
}Res;
typedef struct {
char* l; char* r; Res v;
}Judge;

int main() {
Judge d[] = {
{"ABCD", "EFGH", even},
{"ABCI", "EFJK", up},
{"ABIJ", "EFGH", even}};
int possible[12];
int i,j;
for (i = 0; i < 12; i++) possible[i] = 1;
for (i = 0; i < sizeof(d)/sizeof(Judge); i++) {
if (d[i].v == even) {
char *s = d[i].l;
while (*s) { possible[*s-'A'] = 0; s++;}
s = d[i].r;
while (*s) { possible[*s-'A'] = 0; s++;}
}
}
for (i = 0; i < 12; i++) {
if (possible[i]) {
int match = 1;
int weight = 0;
char c = i + 'A';
for (j = 0; j < sizeof(d)/sizeof(Judge); j++) {
int p = 0;
if (d[j].v == even) continue;
if (strchr(d[j].l, c)) {
p = (d[j].v == up)?1:-1;
}
else if (strchr(d[j].r, c)) {
p = (d[j].v == up)?-1:1;
}
if (p == 0 || (weight && weight != p)) {
match = 0;
break;
}
weight = p;
}
if (match) {
printf("Find %c\n", c);
}
}
}
return 0;
}

⑷ 用C语言编程:80枚硬币 ,1个是假币, 质量较轻 ,有天平一个,怎样称量才能找出假币

没那么复杂,看我的:
1、将硬币分成2份,得出两份重量;
2、从重的那份取出一枚,其为真币重量;
3、将轻的那一份40枚硬币逐一与真币相比,若轻则为假币。
(注意,三元表达式中的问号和if语句,相当于天平的使用)
#include <stdio.h>
void main() {
int a[80]={2},s,s1=s2=0,i,*p;
a[51]=1; /* 假设第52枚为假币
for(i=0;i<40;i++) {
s1+=a[i];
s2+=a[i+40];
}
p=(s1>s2)? &a[40] : &a[0] ;
s=(s1>s2)? a[0] : a[40] ;

for(i=0; i<40; i++,p++) if(*p < s) break;
printf("假币为第%d枚,其重量为%d\n", p-&a[0]+1,*p);
}

⑸ 求用递归函数解决伪币问题的C++代码,编辑环境VC++6.0

/*
题目:81枚硬币中,有一枚假币且这枚假币比其它的轻。称四次把这枚硬币找出来。
算法描述:
81=3^4
把所有的硬币分成数目相同的三份,把两份(x和y)放在天平上,如果重量不等,则假币在轻的一份里;如果重量相等,则假币在没称的那份里(z)。此时硬币总量变为1/3。依此循环,称三次后,剩三枚硬币,再称一次则可找出假币。
这个题适合于用递归来解决。递归的特点是:函数里面嵌套本函数。递归函数的执行分为两个环节:递推和回归:遇到本函数则递推;函数有了最终值时开始回归。
主函数中定义了重量为10的81枚硬币(其中第10枚为假币,重量为6)。递归函数weight(m,n)的参数m,n分别是当前所有硬币的序号的开始数和结束数(例如,称了第1次发现硬币可能在第1-27枚中,则m=1,n=27。)当m=n时,假币找到,函数开始回归。
*/

#include <stdio.h>

#define count 81
#define badcoin 10

int weight(int m,int n);

int a[count];

int main()
{
int i;
int t = 0;
for (i = 0;i<count;i++)
a[i]= 10;
a[badcoin]=6;
t= weight(1,count);
printf("the %dth coin is bad!\n",t);
return 0;
}

int weight(int m,int n)
{
int j=0;
int i=0;
int x=0;
int y=0;
int z=0;
if(m>=n)
return m;
for (i=0;i<(n-m+1)/3;i++,j++){
x += (a[m+j]);
y += (a[m+(n-m+1)/3+j]);
z += (a[m+2*(n-m+1)/3+j]);
}
if (x==y){
m = m+2*(n-m+1)/3;
weight(m,n);
}
else if(x>y){
m = m+(n-m+1)/3;
n = m+(n-m+1)/2-1;
weight(m,n);
}
else{
n=m+(n-m+1)/3-1;
weight(m,n);
}
}

⑹ 用递归写伪币问题:n块金币中找一较轻假币,要求次数最少,显示找假币的实际过程,输出总金币数与假币序号

dw

⑺ c语言问题简单的!~假币

帮你分析下代码吧
#include<stdio.h>
int main()
{
int n,i,a,b;
while(scanf("%d",&n)!=EOF&&n!=0)
{
i=1; //还没有开始称重i应该赋值为0
while(n>3) //大于一枚的时候一定要称 n>1
{
a=n/3;
b=n%3+a;
if(a>b)
{n=a;}
else
{n=b;} //由于你要考虑的是最少称重的次数
所以直接取总数的三分之一就可以了(只是因为有最少次数的条件,所以才能这样 ,不然还得考虑不能整除时候多出来的硬币)
以上代码直接取n=n/3就可以了
i++;
}
printf("%d\n",i);
}
return 0;
}

⑻ 用c语言写程序模拟区分假币问题,有30枚硬币,其中一枚假币,已知假币比真币稍轻,求完整代码。

#include <stdio.h>
main()
{
int b[30]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1}; //用0代表假币
int i=0;
int c=b[0]; //把第一个赋给c
for (;i<30;i++)
//把第二个以后的币都和第一个比较,如果比它轻就输出它。
if(c>b[i])
printf("第%d个是假的",i+1);

}

代码如上。不懂可以问我。

⑼ C语言 猜假币问题

因为每次输入一个字符之后需要敲一个回车键,而scanf()是不接受这个回车符的,因此呢第二次的scanf就直接认为这个回车符就是你第二次的输入了,所以不会有想要的结果,在每次的scanf()之后清空一下输入流,这样fflush(stdin) ;这样多余的回车符就去掉了。