Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第二章:韩信点兵 #38

Open
zd007 opened this issue May 8, 2019 · 4 comments
Open

第二章:韩信点兵 #38

zd007 opened this issue May 8, 2019 · 4 comments

Comments

@zd007
Copy link

zd007 commented May 8, 2019

#include<stdio.h>
int main()
{
int a[40],b[40],c[40],i;
int kase=0;
int x=0;
while(scanf("%d %d %d",&a[x],&b[x],&c[x])==3)
{
x++;
if(scanf("%d %d %d",&a[x],&b[x],&c[x])==3)
x++;
else
break;
}

int j=0;
for(;j<x;j++)
{	int print=1;//是否有解 
	for(i=10;i<=100;i++)
	{
		if(i%3==a[j]&&i%5==b[j]&&i%7==c[j])
		{
			printf("case %d:%d\n",++kase,i);
			print=0;
			break; 
		}
	}
	if(print) printf("case %d:No answer.\n",++kase);
	
}
return 0;

}

@kengwang
Copy link

可以只列举7的倍数哦!然后再根据条件还原为测试人数,之后再判断是否符合其他条件
Example见下:

#include <stdio.h>
int main(){
	int a,b,c,kase=0,hasans=0;
	while(scanf("%d %d %d",&a,&b,&c)==3){
		//这边我们边读入边计算 
		for (int i=14;i<=93;i=i+7){//列举7的倍数 
			int person=i+c;//猜测的总人数 
			if (person%3==a && person%5==b){//判断是否符合先前的条件 
				printf("Case %d: %d\n",++kase,person);//输出结果 
				hasans=1;//找到结果 
				break;//结束列举7倍数 
			}			
		} 
		if (!hasans){
				printf("Case %d: No answer\n",++kase);//输出结果 
			} 
			hasans=0;
	}
	
	return 0;
}

@Coder6886
Copy link

@kengwang
你没有考虑到人数在10到13之间的情况,如:Case1:1 0 3 是10,但是思路挺好。

@kengwang
Copy link

kengwang commented Jun 3, 2020

@kengwang
你没有考虑到人数在10到13之间的情况,如:Case1:1 0 3 是10,但是思路挺好。

感谢指出

@3099462480
Copy link

#include<stdio.h>
int main(){
int a,b,c,n;
while(scanf("%d%d%d",&a,&b,&c)==3){
for(n=10;n<=100;n++){
if(n%3==a&&n%5==b&&n%7==c){
printf("%d\n",n);
break;
}
}
if(n==101){printf("No answer");}
}
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants