Go: package main

import (
	"fmt"
	"os"
	"strconv"
	"strings"
	"bufio"
)

var (
	sc = bufio.NewScanner(os.Stdin)
	buf = make([]byte, 1000)
)

func scanText() string{
	sc.Scan()
	return sc.Text()
}

func scanTextArray(n int) []string {
	out := make([]string, n)
	for i, v := range(strings.Split(scanText(), "")) {
		out[i] = v
	}
	return out
}

func scanInt() int {
	out, e := strconv.Atoi(scanText())
	if e != nil {
        panic(e)
    }
	return out
}

func scanIntArray(n int) []int {
	out := make([]int, n)
	for i, v := range(strings.Split(scanText(), " ")) {
		num, e := strconv.Atoi(v)
		if e != nil {
			panic(e)
		}
		out[i] = num
	}
	return out
}
//  template end

var (
	coins = [6]int{500, 100, 50, 10, 5, 1}
)

func f(data int) {
	ans := 0
	change := 1000 - data
	for _, v := range coins {
		for ;v <= change; {
			change -= v
			ans++
		}
	}
	fmt.Println(ans)
}

func main() {
	sc.Buffer(buf, 10000)
	for{
		data := scanInt()
		if data == 0 {
			break
		}
		f(data)
	}
}

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

int main(){
	
	int c[6]={500,100,50,10,5,1};
	int n;
	
	
	while(cin>>n){
		if(n!=0){
		int k=0;
		int s=0;
		n=1000-n;
	
	while(n>0){
		if(n-c[k]>=0){
			n=n-c[k];
			s++;
		}
		else
			k++;
	}
			cout<<s<<endl;
	}
	
	else
		break;
	}
	
}

C++: #include<iostream>
using namespace std;
int main(){
int n,b;while(true){cin>>b;n=1000-b;;if(n==0){break;}
cout<<n/500+(n/100)%5+(n/50)%2+(n/10)%5+(n/5)%2+n%5<<endl;}}

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

C++: #include <iostream>
using namespace std;
int main(void)
{
	for (;;)
	{
		int a,b,c,d,e,f,g,h,i,j,k,l,n,m;
		d=0;
		cin>>a;
		if (a==0)
		{
			break;
		}
		b=1000-a;
		c=b/500;
		d=d+c;
		e=b%500;
		c=e/100;
		d=d+c;
		e=b%100;
		c=e/50;
		d=d+c;
		e=b%50;
		c=e/10;
		d=d+c;
		e=b%10;
		c=e/5;
		d=d+c;
		e=b%5;
		d=d+e;
		cout<<d<<endl;
	}
	return 0;
}

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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
int main()
{
  int n;cin>>n;
  while(n!=0)
  {
    int now=1000;
    now-=n;
    int ans=0;
    while(now>=500)
    {
      now-=500;
      ans++;
    }
    while(now>=100)
    {
      now-=100;
      ans++;
    }
    while(now>=50)
    {
      now-=50;
      ans++;
    }
    while(now>=10)
    {
      now-=10;
      ans++;
    }
    while(now>=5)
    {
      now-=5;
      ans++;
    }
    while(now>=1)
    {
      now--;
      ans++;
    }
    cout<<ans<<endl;
    cin>>n;
  }
}

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

// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
#define PI (2*acos(0.0))
#define EPS (1e-9)
#define MOD (int)(1e9+7)
#define INF numeric_limits<int>::infinity()
#pragma endregion


int main()
{
    while(true) {
        int n;
        cin >> n;
        if (n==0) return 0;
        int ret = 1000 - n;

        int ans = 0;

        ans += ret / 500;
        ret = ret % 500;

        ans += ret / 100;
        ret = ret % 100;

        ans += ret / 50;
        ret = ret % 50;

        ans += ret / 10;
        ret = ret % 10;

        ans += ret / 5;
        ret = ret % 5;

        ans += ret;

        P(ans);
    }

    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;


int main(){
    int coin[6]={1,5,10,50,100,500},in,ans;
    

 while(1){
    ans=0;
    scanf("%d",&in);
    
    if(in==0)
      break;

    in=1000-in;
    
    for(int i=5;i>=0;i--){
        ans+=in/coin[i];
        in%=coin[i];
    }
    
    printf("%d\n",ans);
    
  }
    return 0;
}

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

int main(){
	int x;
	int change[6] = {500, 100, 50, 10, 5, 1};
	while( cin >> x, x ){
		x = 1000 - x;
		int ans = 0;
		/*while( x > 0 ){
			ans++;
			if( x >= 500 ){
				x -= 500;
			}else if( x >= 100 ){
				x -= 100;
			}else if( x >= 50 ){
				x -= 50;
			}else if( x >= 10 ){
				x -= 10;
			}else if( x >= 5 ){
				x -= 5;
			}else{
				x--;
			}
		}*/
		while( x > 0 ){
			ans++;
			for(int i=0 ; i < 6 ; i++ ){
				if( x >= change[i] ){
					x -= change[i];
					break;
				}
			}
		}
		cout << ans << endl;
	}
}

C++: 
#include <stdio.h>
#include <algorithm>

using namespace std;

int main() {

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

	for (;;) {

		int N, ans = 0;

		scanf("%d", &N);
		if (!N) break;

		N = 1000 - N;

		for (int i = 0; N > 0; i++) {
			while (N >= coin[i]) {
				N -= coin[i];
				ans++;
			}
		}

		printf("%d\n", ans);
	}

	return 0;
}