Go: ///
// File:  0521.go
// Author: ymiyamoto
//
// Created on Wed Mar  7 22:11:28 2018
//
package main

import (
	"fmt"
)

var money int

func main() {
	for {
		fmt.Scan(&money)
		if money == 0 {
			break
		}
		rem := 1000 - money

		count := 0
		for _, coin := range []int{500, 100, 50, 10, 5, 1} {
			count += rem / coin
			rem %= coin
		}
		fmt.Println(count)
	}
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int main() {
	int n;

	while(cin >> n, n) {
		int p = 1000-n;
		int m = 0;

		m += p/500;
		p = p%500;
		m += p/100;
		p = p%100;
		m += p/50;
		p = p%50;
		m += p/10;
		p = p%10;
		m += p/5;
		p = p%5;
		m += p;

		cout << m << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
using namespace std;
int main()
{
	int a = 0;
	int b;
	int c = 0;
	for (; a != 0;)
	{
		cin >> a;
		b = a;
		for (; b != 0;)
		{
			if (b >= 500)
			{
				b = b - 500;
				c++;
			}
			if (b >= 100)
			{
				b = b - 100;
				c++;
			}
			if (b >= 50)
			{
				b = b - 50;
				c++;
			}
			if (b >= 10)
			{
				b = b - 10;
				c++;
			}
			if (b >= 5)
			{
				b = b - 5;
				c++;
			}
			if (b >= 1)
			{
				b = b - 1;
				c++;
			}
			cout<<c<<endl;
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
	int a;
	int i;
	int s=0;
	
	cin>>a;
	a=1000-a;
	if(a>499){
		a=a-500;
		s=s+1;
	}
	for(i=0;i<4;i++){
		if(a>99){
			a=a-100;
			s=s+1;
		}
	}
	if(a>49){
		a=a-50;
		s=s+1;
	}
	for(i=0;i<4;i++){
		if(a>9){
			a=a-10;
			s=s+1;
		}
	}
	if(a>4){
		a=a-5;
		s++;
	}
	s=s+a;
	cout<<s;
}

C++: #include<iostream>
using namespace std;
int main(){
	int cut=0;
	int a;
	int b;
	cin>>a;
	b=1000-a;
	cut+=b/500;
	b=b%500;
	cut+=b/100;
	b=b%100;
	cut+=b/50;
	b=b%50;
	cut+=b/10;
	b=b%10;
	cut+=b/5;
	b=b%5;
	cut+=b;
	cout<<cut<<endl;
	return 0;
}

C++: #include<iostream>
using namespace std;

int main(){
int buy,oturi,kouka;
cin>>buy;
oturi=1000-buy;
if(oturi>500){
oturi-=500;
kouka+=1;
}
while(oturi>=100){
oturi-=100;
kouka+=1;
}
while(oturi>=50){
oturi-=50;
kouka+=1;
}
while(oturi>=10){
oturi-=10;
kouka+=1;
}
while(oturi>=5){
oturi-=5;
kouka+=1;
}
while(oturi>=1){
oturi-=1;
kouka+=1;
}
cout<<kouka<<'\n';
return 0;
}

C++: //47
#include<iostream>

using namespace std;

int main(){
  for(int n;cin>>n,n;){
    int rest=1000-n;
    int coin[]={500,100,50,10,5,1};
    int ans=0;
    for(int i=0;i<6;i++){
      ans+=rest/coin[i];
      rest%=coin[i];
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;


int main() {
int n;
while(cin>>n){
	if(n==0) break;

n=1000-n;
int ans=0;
ans=ans+n/500;
n=n%500;

ans=ans+n/100;
n=n%100;

ans=ans+n/50;
n=n%50;

ans=ans+n/10;
n=n%10;

ans=ans+n/5;
n=n%5;
ans=ans+n;
cout<<ans<<endl;
}
	return 0;
}

C++: #include<stdio.h>

int main(){
int n,c;
while(1){

scanf("%d",&n);
if(n==0)break;
c=0;
n=1000-n;
do{
if(n>=500){n=n-500;c++;}
else if(n>=100){n=n-100;c++;}
else if(n>=50){n=n-50;c++;}
else if(n>=10){n=n-10;c++;}
else if(n>=5){n=n-5;c++;}
else if(n>=1){n=n-1;c++;}
}while(n!=0);
printf("%d\n",c);
}
return 0;
}

C++: #include<iostream>
#include<stdio.h>
using namespace std;
int main(){
	int coin=0;
	int price=0;
	int flag=0;
	cin>>price;
	while(1){
		if(flag==0){
			break;
		}
		if(price>=500){
			price=price-500;
			coin ++;
		}
		else if(price>=100){
			price=price-100;
			coin++ ;
		}
		else if(price>=50){
			price=price-50;
			coin++ ;
		}
		else if(price>=10){
			price=price-10;
			coin++ ;
		}
		else if(price>=5){
			price=price-5;
			coin++ ;
		}
		else if(price>=1){
			price=price-1;
			coin++ ;
		}
		else if(price==0){
			flag=1;
		}
	}
	cout<<coin<<endl;

	return 0;

}

C++: #include <stdio.h>

int main(){
  int i0[] = {500,100,50,10,5,1};
  int i1 = 1,i2 = 0;
  int i4[5] = {0};

  for(int j = 0; j < 6; j++){
    i2 = 0;
    scanf("%d",&i1);
    
    if(i1 == 0){   
      for(int n : i4) if(n != 0) printf("%d\n",n);
      return 0;
    }
    
    i1 = 1000 - i1;

    for(int n = 0; n < 6; n++){
      while(i1 >= i0[n]){
        i1 -= i0[n];
        i2++;
      }
    }
    i4[j] = i2;    
  }
  return -1;
}

C++: #include <iostream>
#include <climits>
using namespace std;
int main() {
    int coin[] = {1,5,10,50,100,500};
    int dp[1000];
    fill_n(dp, 1000, INT_MAX);
    dp[0] = 0;
    for (int i=1; i<1000; ++i) {
        for (int j=0; j<6; ++j) {
            if (i - coin[j] >= 0) {
                dp[i] = min(dp[i], dp[i-coin[j]]+1);
            }
        }
    }

    int n;
    while (cin >> n && n!=0) {
        cout << dp[1000-n] << endl;
    }

    return 0;
}

C++: #include<stdio.h>
int main()
{
	int n,r;
	while(scanf("%d",&n),n)
	{
		n=1000-n;r=n/500;r+=(n%=500)/100;r+=(n%=100)/50;r+=(n%=50)/10;r+=(n%=10)/5;r+=(n%=5);
		printf("%d\n",r);
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
    while(1){
    int n;
    cin>>n;
    if(n==0)break;
    int a,b,c,d,e,f,g;
    g=1000-n;
    for(a=0;a<2;a++){
        for(b=0;b<5;b++){
            for(c=0;c<2;c++){
                for(d=0;d<5;d++){
                    for(e=0;e<2;e++){
                        for(f=0;f<5;f++){
                            if(a*500+b*100+c*50+d*10+e*5+f==g){
                                cout<<a+b+c+d+e+f<<endl;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
        }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
#include <array>
#include <complex>
#include <string>
#include <utility>
#include <map>
int main() {
    int a[6], sum[6] = { 0 };
    int i = 0;
    while(a[i]) {
        std::cin >> a[i];
        if (a[i] == 0) {
            break;
        }
        i++;
        
    }
	i = 0;
    while (a[i] != 0) {
        sum[i] += (1000 - a[i]) / 500;
        a[i] = (1000 - a[i]) % 500;
        sum[i] += a[i] / 100;
        a[i] = a[i] % 100;
        sum[i] += a[i] / 50;
        a[i] = a[i] % 50;
        sum[i] += a[i] / 10;
        a[i] = a[i] % 10;
        sum[i] += a[i] / 5;
        a[i] = a[i] % 5;
        sum[i] += a[i];
        i++;
    }
    for (int j = 0; j<i; j++) {
        std::cout << sum[j] << std::endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>

using namespace std;


    int main(){
        int price=0,money[6]={500,100,50,10,5,1},ret=0,cnt;
        while(1){
        cnt=0;
        cin>>price;
        ret=1000-price;
        if(price==0)break;
        for(int i=0;i<6;i++){
                while(1){
                    if(ret<money[i])break;
                    ret-=money[i];
                    cnt++;

                }
            }

        cout<<cnt<<endl;
    }
    return 0;
}