题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3153
Case n: nn pencils for nn cents nn at four cents each nn at two for a penny nn at four for a penny
10 20 40 0
Case 1: 10 pencils for 10 cents No solution found. Case 2: 20 pencils for 20 cents 3 at four cents each 15 at two for a penny 2 at four for a penny Case 3: 40 pencils for 40 cents 6 at four cents each 30 at two for a penny 4 at four for a penny 7 at four cents each 15 at two for a penny 18 at four for a penny
代码如下:
#include<cstdio> int main() { int n; int flag; int cas = 0; while(scanf("%d",&n)&&n) { double sum = n*1.0; flag = 0; printf("Case %d:\n",++cas); printf("%d pencils for %d cents\n",n,n); for(int i = 1; i < n; i++) //1 { for(int j = 1; j < n; j++) //2 { for(int k = 1; k < n; k++) //3 { if(sum==i*4+j*0.5+k*0.25 && i+j+k==n) { flag = 1; printf("%d at four cents each\n",i); printf("%d at two for a penny\n",j); printf("%d at four for a penny\n\n",k); } } } } if(!flag) printf("No solution found.\n\n"); } return 0; }
HDU 3153 Pencils from the 19th Century(数学题)
原文地址:http://blog.csdn.net/u012860063/article/details/39857405