Vincent's profile异国情调 世界视野 One World On...PhotosBlogListsMore Tools Help

Blog


    October 24

    完整的鸡兔同笼问题(重温C语言)

    1.    简要文字说明与分析

    Ø        整体思路:Input à computation àoutput

    Ø        逐步精化:

    l        对用户输入合法性需要处理:

    因为鸡和兔的个数必是正数,由    chicken+rabbit=heads,2*chicken+4*rabbit=feet可知

    Feet>=2heads&&feet=<4heads

    l        逻辑计算:

    chicken初始化从0开始,每循环一次递增一,相应的rabbit减一,    保持和是定数,即heads  值,当且仅当2*chicken+4*rabbit)==feet 时,问题有解,得问题的解,打印输出

    l        漏洞细节:

    rabbit<0时,问题域无解,client须从新输入信息和计算

     

     

    2.    程序代码与运行结果(注释略)

     

    #include <stdio.h>

     

    #define TRUE 1

     

    main()

    {

           int heads,feet,rabbit,chicken;

     

           printf("please enter the total numbers of heads and feet espectively\n");

     

    while(TRUE)

    {

           scanf("%d%d",&heads,&feet);

     

           while(feet<2heads||feet>4heads||feet<0||heads<0)    /*I fail here*/

           {

                  printf("the number you entered is not appropriate,please try again\n");

                  scanf("%d%d",&heads,&feet);

           }

     

           chicken=0;

           rabbit=heads-chicken;

     

           while(rabbit>=0)

           {

                  if((2*chicken+4*rabbit)==feet)

                  {

                         printf("chicken=%d,rabbit=%d\n",chicken,rabbit);

                         break;

                  }

     

                  chicken++;

                  rabbit--;

           }

          

    if(rabbit<0)

    {

    printf("the number you entered is not appropriate,please try again\n");

    }else{

    printf("have a good time!\n");

    break;

    }

    }

     

    }

     

     

     

    School of Software , NEU

    Vincent Wei

    23th,Oct,2009

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://vincentibm2007.spaces.live.com/blog/cns!1656C6D42D1E61!1605.trak
    Weblogs that reference this entry
    • None