习题2-2 韩信点兵

相传韩信才智过人,从不会直接清点自己军队的人数,只要让士兵先后三人一排、五人一排、七人一排的变换队形,而他每次只掠一眼排尾就知道总人数了。输入包含多组数据,每组数据包含3个非负整数a,b,c,表示每种队形排尾的人数(a<3,b<5,c<7),输出总人数的最小值(或报告无解)。已知总人数不小于10,不超过100,输入到文件结束为止。

样例输入:

2 1 6

2 1 3

样例输出:

case 1:41

case 2:No answer

#include <stdio.h>
 int main()
 {
   freopen("datain.txt","r",stdin);
   freopen("dataout.txt","w",stdout);
   int n,i,j,k,l=1;
   while(scanf("%d",&i)&&scanf("%d",&j)&&scanf("%d",&k))
   {
   	int flag=0,flag2=1; 
   	for(n=10;n<=100;n++)
    {
   	  if(n%3==i&&n%5==j&&n%7==k)
   	  {
   	  	printf("case %d:%d\n",l,n);
   	  	flag=1;
	  }
	  else
	  {
	  	if(n==100&&flag==0)
	  	{
	  		printf("case %d:No answer\n",l);
	  		flag2=0;
		}
	  }
    }
    l++;
	} 
  } 

猜你喜欢

转载自blog.csdn.net/weixin_27848283/article/details/80979583