韩信点兵
来源:互联网 发布:《申报》数据库 编辑:程序博客网 时间:2024/06/16 13:38
相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排、五人一排、七人一排地变换队形,而他每次只掠一眼队伍的排尾就知道总人数了。输入包含多组数据,每组数据包含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(){ int one=0; int count = 0; while (scanf("%d",&one)==1){ count++; int two = 0; int three = 0; scanf("%d", &two); scanf("%d",&three); for (int i= 1;; i++){ int tp = i * 7 + three; if (tp > 100){ printf("Case %d:%s\n",count,"No answer"); break; } if (tp % 5 == two&&tp % 3 == one){ printf("Case %d:%d\n",count,tp); break; } } } return 0;}