⑴ 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) ;這樣多餘的回車符就去掉了。