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<iostream>
#include<string>

using namespace std;

int main(){
	/*
	//数あて
	int n;
	cin >> n;
	int p[200][3];

	for(int i=0;i<n;i++)
	{
		cin >> p[i][0] >> p[i][1] >> p[i][2];
	}

	int _p;
	for(int k=0;k<3;k++)
	{
		for(int i=0;i<n;i++)
		{
			_p = p[i][k];
			for(int j=i+1;j<n;j++)
			{
				if(_p == p[j][k])
				{
					p[i][k] = 0;
					p[j][k] = 0;
				}
			}
		}
	}

	for(int i=0;i<n;i++)
	{
		cout << p[i][0] + p[i][1] + p[i][2] << endl;
	}
	*/

	/*
	//得点
	int ai,am,as,ae,bi,bm,bs,be;
	cin >> ai >> am >> as >> ae >> bi >> bm >> bs >> be;
	int answer=ai+am+as+ae;
	if(answer < bi+bm+bs+be)
	{
		answer=bi+bm+bs+be;
	}
	cout << answer << endl;
	*/
	
	//おつり
	int onum;
	while(true){
		int N;
		cin >> N;
		if(N == 0) break;
				
		int oturi = 1000-N;
		if(oturi >= 500)
		{
			onum++;
			oturi -= 500;
		}
		while(oturi>=100)
		{
			onum++;
			oturi -= 100;
		}
		if(oturi >= 50)
		{
			onum++;
			oturi -= 50;
		}
		while(oturi>=10)
		{
			onum++;
			oturi -= 10;
		}
		if(oturi >= 5)
		{
			onum++;
			oturi -= 5;
		}
		while(oturi>=1)
		{
			onum++;
			oturi -= 1;
		}
		 cout << onum << endl;
	}
   
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;

const double PI = acos(-1);
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9+7;

#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i,n) for (int i = 0; i<n; i++)
#define ALL(a) begin(a), end(a)

int main (void) {
    int n;
    while (cin >> n , n) {
        int m = 1000 - n;
        int ans = 0;
        if (m >= 500) {
            ans ++;
            m -= 500;
        }
        //cout << m << " " << ans << endl;
        if (m >= 100) {
            ans += m/100;
            m = m % 100;
        }
        //cout << m << " " << ans << endl;
        if (m >= 50) {
            ans += m/50;
            m = m % 50;
        }
        //cout << m << " " << ans << endl;
        if (m >= 10) {
            ans += m / 10;
            m = m % 10;
        }
        //cout << m << " " << ans << endl;
        if (m >= 5) {
            ans += m / 5;
            m = m % 5;
        }
        if (m >= 1) {
            ans += m/1;
            m = m % 1;
        }
        //cout << m << " " << ans << endl;
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

int main() {
  const int coin[] = {500,100,50,10,5,1};
  vector<int> dp(1000,INF);
  dp[0] = 0;
  REP(i,1000)
    REP(k,6)
      if (i+coin[k] < 1000)
        dp[i+coin[k]] = min(dp[i+coin[k]], dp[i] + 1);
  int n;
  while(cin>>n,n) {
    cout << dp[1000-n] << endl;
  }
}

C++: #include<iostream>

using namespace std;

int main(void){
	
	int coin[10],price,sum;
	while(cin>>price,price){
		for(int i=0;i<10;i++){
			coin[i]=0;
		}
		sum=0;
		cin>>price;
		price=1000-price;
		if(price/500){
			coin[0]=(int)(price/500);
			price=price-500*coin[0];
		}
		if(price/100){
			coin[1]=(int)(price/100);
			price=price-100*coin[1];
		}
		if(price/50){
			coin[2]=(int)(price/50);
			price=price-50*coin[2];
		}
		if(price/10){
			coin[3]=(int)(price/10);
			price=price-10*coin[3];
		}
		if(price/5){
			coin[4]=(int)(price/5);
			price=price-5*coin[4];
		}
		if(price/1){
			coin[5]=(int)(price/1);
			price=price-coin[5];
		}
		for(int i=0;i<5;i++){
			sum=sum+coin[i];
		}
		cout<<sum<<endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
	int kin,sum;
	while(1)
	{
		cin>>kin;
		if(kin==0)
			break;
		kin=1000-kin;
		sum=kin/500+(kin%500)/100+(kin%500%100)/50+(kin%500%100%50)/10+(kin%500%100%50%10)/5+(kin%500%100%50%10%5);
		cout<<sum<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define PI 4*atan(1)
#define INF 1e8

typedef long long ll;

int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

int main(){
  int N;
  while(cin >> N, N){
    N = 1000 - N;
    int ans = 0;
    ans += N / 500;
    N %= 500;
    ans += N / 100;
    N %= 100;
    ans += N / 50;
    N %= 50;
    ans += N / 10;
    N %= 10;
    ans += N / 5;
    ans += N %= 5;
    cout << ans << endl;
  }
}

C++: #include<iostream>
using namespace std;
int main()
{
  int money,oturi,maisu = 0;
  
  cin >> money;
  oturi = 1000 - money;
  for(int i = 1000;oturi > 10;i /= 10){
    oturi %= i;
    if(oturi >= i/10){
      (oturi / (i/10) >= 5) ? (maisu += oturi / (i/10) - 4) : (maisu += oturi / (i/10));
    }
  }
  cout << maisu << endl;
    
    return 0;
  }

C++: #include <cstdio>

int main (int argc, char const* argv[])
{
	
	int coin[6]={500,100,50,10,5,1};
	
	int a,r=0,m;
	
	while(true){
	
	scanf("%d",&m);
	if(m==0) break;
	m=1000-m;
	r=0
	for(int i=0;i<6;i++){
		a=m/coin[i];
		r+=a;
		m-=(a*coin[i]);
	}
	
	
	printf("%d\n",r);
	
	}
	return 0;
}

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

const int coin[6] = {500, 100, 50, 10, 5, 1};

int solve(int v) {
  int val = 0;
  for (int kind = 0; (kind < 6) or (v > 0); kind++) {
    int n = v / coin[kind];
    v -= coin[kind] * n;
    val += n;
  }
  return val;
}

int main() {
  int v;
  while (cin >> v and v != 0) {
    cout << solve(1000 - v) << endl;
  }
}

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

int main(){
  int t;
  while((cin >> t), t){
    int num=0;
    t = 1000-t;
    while(t>=500){
      num++;
      t-=500;
    }
    while(t>=100){
      num++;
      t-=100;
    }
    while(t>=50){
      num++;
      t-=50;
    }
    while(t>=10){
      num++;
      t-=10;
    }
    while(t>=5){
      num++;
      t-=5;
    }
    while(t>=1){
      num++;
      t-=1;
    }
    cout << num << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
while(1){
int n,a,i=0;
cin>>n;
if(n==0)
break;
a=1000-n;
if(a>=500){
i+=1;
a-=500;
}
if(a>=100){
int b=a/100;
i+=b;
a-=100*b;
}
if(a>=50){
i+=1;
a-=50;
}
if(a>=10){
int b=a/10;
i+=b;
a-=10*b;
}
if(a>=5){
i+=1;
a-=5;
}
if(a>=1){
int b=a/1;
i+=b;
a-=1*b;
cout<<i<<'\n';
}
}
return 0;
}

C++: #include<iostream>
using namespace std;
int main() {
	int pay[6] = { 500,100,50,10,5,1 };
	while (true) {
		int a = 0, b = 0;
		cin >> a;
		if (a == 0) break;
		a = 1000 - a;
		for (int i = 0; i < 6; ++i) {
			b = b + a / pay[i];
			a = a%pay[i];
		}
		cout << b << endl;
	}
	return 0;
}

C++: coin = [500,100,50,10,5,1]
loop do
  n = gets.chomp.to_i
  break if n==0
  n=1000-n
  ans = 0
  for c in coin
    ans+=n/c
    n=n%c
  end
  puts ans
end

C++: #include <iostream>
using namespace std;
int main(){
  int n ,coin=0;
  while ( 1 ){
    cin >> n;
    if( cin.eof() ){
      break;
    }
 
    n = 1000 - n; 
    
    if( n >= 500 ){
      while( n >= 500 ){
	n -= 500;
	coin++;
      }
    }
    
    if( n >= 100 ){
      while( n >= 100 ){
	n -= 100;
	coin++;
      }
    }
  
    if( n >= 50 ){
      while( n >= 50 ){
	n -= 50;
	coin++;
	cout << "50"  << endl;
      }
    }
    
    if( n >= 10 ){
      while( n >= 10 ){
	n -= 10;
	coin++;
      }
    }
    
    if( n >= 5 ){
      while( n >= 5 ){
	n -= 5;
	coin++;
      }
    }
    
    if( n >= 1 ){
      while( n >= 1 ){
	n -= 1;
	coin++;
      }
    }
    
    cout << coin << endl;
    coin = 0;
  }
  return 0;
}