2017-04-11 2 views
0

안녕하세요, 나는 얼마나 많은 지폐가 제공된 돈의 합계에 있는지를 계산하는 알고리즘을 구현하려고합니다. 예를 들어 : 돈의합계 계산의 은행 노트의 수를 얻으십시오

합계가 135 유로 입니다 결과는 다음과 같습니다 1 주 100 유로 1 주 20 유로 10 유로의 1 주 5 유로

나는이의 1 주 내가 아는 알고리즘은 다른 언어로 테스트 했으므로 잘 작동하지만 어떤 이유로 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

일부 floor() 호출이 없습니다. '$ p500 = floor ($ money/500); 예를 들면. 모든 부서에서이 작업을 수행해야합니다. –

+0

고마워, 고칠거야. 그러나 floor() 없이는 어떤 차이가 있는지 궁금해 할 것입니다. 어리석은 질문일지도 모릅니다. 알고리즘을 위해서 PHP를 많이 사용하지 않았습니다. – MariusBrave

+1

'$ p500 '은 500 개의 노트 수를 나타 내기위한 것이라고 생각했습니다. 노트의 분수를 가질 수 없습니다. –

답변

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 
*/ 


?> 
+0

감사합니다. 바보 같은 실수가 있음을 알고있었습니다. 이 문제가 해결되었습니다. – MariusBrave

관련 문제