获得总和计算的银行票据数量

时间:2017-04-11 18:24:55

标签: php

嗨,我正在尝试实施算法,计算提供的金额中有多少钞票。例如:

总和为135欧元 结果将是: 1张100欧元的钞票 1 20欧元的票据 1欧元10欧元的注释 1注5欧元

我知道的算法工作得很好,因为我已经用其他语言测试了它,但由于某种原因它在PHP上给了我一个错误的结果。无论我传递给什么数字,它总是返回685.有人知道为什么会发生这种情况吗?谢谢你的帮助。

<?php 

function getNotes($moneystart){
   $works = 0;
   $attempt = 0;
   $moneystart = 0;
   $money = 0;
   $p500 = 0;
   $p100 = 0; 
   $p50 = 0; 
   $p20 = 0; 
   $p10 = 0; 
   $p5 = 0; 
   $p1=0;

   while($works == 0){

   $money = $moneystart;

    if ($attempt <= 0){
        $p500 = $money / 500;
        $money = $money % 500;
    }

    if ($attempt <= 1){
        $p100 = $money / 100;
        $money  = $money % 100;
    }

    if($attempt <= 2){
        $p50 = $money/50;
        $money = $money%50;
    }

    if($attempt <= 3){
       $p20 = $money/20;
       $money = $money%20;
    }

    if($attempt <= 4){
        $p10 = $money/10;
        $money = $money%10;
    }

    if($attempt <= 5){
       $p5 = $money/5;
       $money = $money%5;
    }

    if($attempt <= 6){
      $p1 = $money;
      $works++;
    }

    if ($p500 + $p100 + $p50 + $p20 + $p10 + $p5 + $p1 >= 4){ 
        $works++;
    }

    else {
        $attempt++;
    }

    if($attempt > 6){
      echo "the amount is too little, please enter a higher value";
    }

   }

   $result  = "Result: "+"\n" 
            + $p500 + " 500e notes,"+"\n" 
            + $p100 + " 100e notes " +"\n"
            + $p50 + " 50e notes" +"\n"
            + $p20 + " 20e notes" +"\n"
            + $p10 + " 10e notes" +"\n"
            + $p5 + " 5e notes";

    echo $result;          

}

?>

1 个答案:

答案 0 :(得分:2)

你非常接近。

您正在覆盖程序顶部附近的$ moneystart参数。评论$ moneystart = 0;

PHP连接运算符是。不是+

<?php 

function getNotes($moneystart){
   $works = 0;
   $attempt = 0;
   //$moneystart = 0;
   $money = 0;
   $p500 = 0;
   $p100 = 0; 
   $p50 = 0; 
   $p20 = 0; 
   $p10 = 0; 
   $p5 = 0; 
   $p1=0;

   while($works == 0){

   $money = $moneystart;

    if ($attempt <= 0){
        $p500 = $money / 500;
        $money = $money % 500;
    }

    if ($attempt <= 1){
        $p100 = $money / 100;
        $money  = $money % 100;
    }

    if($attempt <= 2){
        $p50 = $money/50;
        $money = $money%50;
    }

    if($attempt <= 3){
       $p20 = $money/20;
       $money = $money%20;
    }

    if($attempt <= 4){
        $p10 = $money/10;
        $money = $money%10;
    }

    if($attempt <= 5){
       $p5 = $money/5;
       $money = $money%5;
    }

    if($attempt <= 6){
      $p1 = $money;
      $works++;
    }

    if ($p500 + $p100 + $p50 + $p20 + $p10 + $p5 + $p1 >= 4){ 
        $works++;
    }

    else {
        $attempt++;
    }

    if($attempt > 6){
      echo "the amount is too little, please enter a higher value";
    }

   }

   $result  = "Result: "."\n" 
            . $p500 . " 500e notes,"."\n" 
            . $p100 . " 100e notes " ."\n"
            . $p50 . " 50e notes" ."\n"
            . $p20 . " 20e notes" ."\n"
            . $p10 . " 10e notes" ."\n"
            . $p5 . " 5e notes";

    echo $result;          

}


getNotes(150);

/**
Output 
Result: 
0.3 500e notes,
1.5 100e notes 
1 50e notes
0 20e notes
0 10e notes
0 5e notes
*/ 


?>