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<bits/stdc++.h>
using namespace std;
const int V[6]={1,5,10,50,100,500};
int C[6],A;

void main(){
  int ans=0;
  for(int i=5;i>=0;i--){
    int t=min(A/V[i],C[i]);
    A-=t*V[i];
    ans+=t;
  }
  printf("%d\n",ans);
  return 0;
}

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

int main(){
int n;
int a[6]={500,100,50,10,5,1};
while(cin>>n){
if(n==0)break;
n=1000-n;
for(int i=0;i<20;i++){
if(n==0){cout<<i<<endl; break;}
for(int j=0;j<6;j++){
if(n>=a[j]){n-=a[j]; break;}}}}}

C++: #include <iostream>
#define add(coinvalue) changecoins+=(int)change/coinvalue;change%=coinvalue;
using namespace std;

int main() {
	// your code goes here
	for(;;){
		int fee;
		cin >> fee;
		if(fee==0)
			break;
		int change=1000-fee;
		int changecoins=0;
		add(500)
		add(100)
		add(50)
		add(10)
		add(5)
		add(1)
		cout << changecoins << endl;
	}
	return 0;
}

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

using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define np string::npos
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define repc(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto n :(array))

double eps = 1e-10 ;

  template <typename T>
void out(vector < T > v)
{
  for(size_t i = 0; i < v.size(); i++)
  {
    debug(v[i]);
  }
}

template<typename T>
string ntos( T i ) {
  ostringstream s ;
  s << i ;
  return s.str() ;
}

template<typename T>
T ston(string str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n,ans = 0;
  while(cin >> n){
    n = 1000-n;
    while(n >= 500){
      n -= 500;
      ++ans;
    }
    while(n >= 100){
      ++ans;
      n -= 100;
    }
    while(n >= 50){
      n -= 50;
      ++ans;
    }
    while(n >= 10){
      ++ans;
      n -= 10;
    }
    while(n >= 5){
      n -= 5;
      ++ans;
    }
    while(n >= 1){
      n -= 1;
      ++ans;
    }
    cout << ans<< endl;
  }
  return 0;
}

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

int main(void)
{
	int much;
	int c = 0;

	while(cin >> much, much > 0){
		much = 1000 - much;

		if(much >= 500){
			much %= 500;
			c++;
		}
		if(much >= 100){
			c += (int)(much / 100);
			much %= 100;
		}
		if(much >= 50){
			c += (int)(much / 50);
			much %= 50;
		}
		if(much >= 10){
			c += (int)(much / 10);
			much %= 10;
		}
		if(much >= 5){
			c += (int)(much / 5);
			much %= 5;
		}
		c += much;

		cout << c << endl;

	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <limits>
typedef long long ll;
#define REP(i,n)   for(int i=0; i<(int)(n); ++i)
#define FOR(i,b,e) for(int i=(b); i<=(int)(e); ++i)

#define in cin
#define out cout
#define pb push_back
using namespace std;

int main(){
	int n;
	while (in >> n) {
		if (!n)break;
		int a = 1000 - n,ans=0;
		int c[5] = { 500, 100, 50, 10, 5 };
		REP(i, 5) {
			ans += a / c[i];
			a %= c[i];
		}
		out << ans + a << endl;
	}
	return 0;

}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#define MAX 3000
using namespace std;

int C[6]={1,5,10,50,100,500};
int T[MAX+1];
void douteki();

int main(){
  int n;
 

  while(1){
    cin >> n;
    if(n == 0)break; 
    douteki();
    cout << T[1000-n] << endl;
  }



}

void douteki(){
  for(int j=0;j<=MAX;j++)T[j]=10000;
  T[0]=0;
  for(int i=0;i<6;i++){
    for(int j=C[i];j<=MAX;j++){
      T[j]=min(T[j],T[j-C[i]]+1);
    }
  }

}

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

C++: #include<bits/stdc++.h>
using namespace std;
int coin[]={500,100,50,10,5,1};
int main(){
    int N;
    while(scanf("%d",&N),N){
        N=1000-N;
        int ans=0;
        for(int i=0;i<6;i++){
            ans+=N/coin[i];
            N%=coin[i];
        }
        printf("%d\n",ans);
    }
    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <array>
#include <vector>
#include <iostream>

using namespace std;

int main() {

  const array<int, 6> coins = { 500, 100, 50, 10, 5, 1 };

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

    n = 1000 - n;
    int count = 0;
    for ( auto coin : coins ) {
      if ( n > coin ) {
    	count += (n / coin);
    	n %= coin;
      }
    }
    cout << count << endl;
  }
  return 0;  
}

C++: #include<iostream>

using namespace std;

int main(void){
    
    int n,otsuri,count;
    const int money[6]={1,5,10,50,100,500};

    while(cin>>n,n){
            
        otsuri=1000-n;
        count=0;
        for(int i=5;i>=0;i--){
            while(otsuri>=money[i]){
                otsuri=otsuri-money[i];
                count++;
            }   
        }   
        cout<<count<<endl;
    }   


}

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


using namespace std;
int main() {
    int V[6] =  {1, 5, 10, 50, 100, 500};
    int C[6];  //枚数
    int A;  //合計金額
    int t = 0;

    while(1) {
        cin >> A;
        if(A == 0){
            break;
        }
        A = 1000 - A;
        int ans = 0;
        for(int i = 5; i >= 0; i--){ //Vは500円から使う
            t = A / V[i]; //コインを使う枚数
            A -= t * V[i]; //A(合計金額)から支払われた分だけ引く
            ans += t;
            }
        cout << ans << '\n';
        }
    return 0;
    }

C++: #include<stdio.h>

int main(){
	int i=0,a,b[6]={500,100,50,10,5,1},c=0;
	scanf("%d",&a);
	a=1000-a;
	while(a){
		if((a-b[i])>=0){
			a-=b[i];
			c++;
		}
		else i++;
	}
	printf("%d\n",c);
}

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

int change[6]={1,5,10,50,100,500};
int cnt;
int n;

int main(){
	while(cin >> n){
		if(!n)break;
		cnt = 0;
		n = 1000-n;
		for( int i = 5; i >= 0; i-- ){
			while(n>=change[i]){
				n-=change[i];
				cnt++;
			}
		}
		cout << cnt << endl;
	}
	return 0;
}