代码改变世界

Sicily/1704. Pencils from the Nineteenth Century

2011-08-28 09:34  Min·zc  阅读(188)  评论(0编辑  收藏  举报

x+y+z=n

4x+0.5y+0.25z=n

消去y,得到x和z的关系式14x=2n+z,枚举所有的x

#include <stdio.h>
using namespace std;
int main()
{
        int n;
        int t=0;
        while(scanf("%d",&n)&&n!=0)
        {
                t++;
                printf("Case %d:\n",t);
                printf("%d pencils for %d cents\n",n,n);
                int x=1;
                int flag=0;
                for(;x<n;x++)
                {
                        int z=14*x-2*n;
                        if(z>0&&x+z<n)
                        {
                                flag=1;
                                printf("%d at four cents each\n",x);        
                                printf("%d at two for a penny\n",n-x-z);    
                                printf("%d at four for a penny\n",z);
                                printf("\n");       
                        }
                }
                if(!flag)
                        printf("No solution found.\n\n");
        }
}