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<sstream>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<numeric>
#include<chrono>

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 timenow std::chrono::system_clock::now()
#define timecal(start,end) std::chrono::duration_cast<std::chrono::milliseconds>((end)-(start)).count()

#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))

using ll = long long ;
using vi = vector<int> ;
using vl = vector<ll> ;
//template<class Value>
//using dict = std::map< string, Value >;
using dict =  map<string,int>;
using timep = std::chrono::system_clock::time_point ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
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;
  cin >> n;
  n = 1000-n;
  if(n >= 500){
    n -= 500;
    ++ans;
  }
  while(n >= 100){
    ++ans;
    n -= 100;
  }
  if(n >= 50){
    n -= 50;
    ++ans;
  }
  while(n >= 10){
    ++ans;
    n -= 10;
  }
  if(n >= 5){
    n -=5;
    ++ans;
  }
  cout << ans + n << endl;
  return 0;
}

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

int memo[1111];

int solve(int n){
  
  if(!n) return 0;
  if(n<0) return 1<<28;
  if(memo[n]) return memo[n];
  
  int ans = 1<<28;
  ans = min(solve(n-500), min(solve(n-100), min(solve(n-50), min(solve(n-10), min(solve(n-5), solve(n-1))))))+1;
  
  return memo[n] = ans;
}

int main(){
  
  for(int i=0;i<1111;i++) solve(i);

  int n;
  while(cin >> n, n) cout << solve(1000-n) << endl;
  
}

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

int main(){

	int c[6] = {500,100,50,10,5,1};
	int money, pay, change;

	scanf("%d", &pay);

	//if(pay == 0) return 0;

	change = 0;
	money = 1000 - pay;

	for(int i = 0; i < 6; i++){

		change += money / c[i];
		money = money % c[i];
	}

	if(pay != 0) printf("%d\n", change);

	
	return 0;
}

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

int main(){
  int n;
  while(cin>>n,n!=0){
    n = 1000-n;
    const vector<int> list = {500,100,50,10,5,1};
    int ans = 0;
    for(int i=0;i<list.size();i++){
      ans += n / list[i];
      n = n % list[i];
    }
    cout<<ans<<endl;
  }
}

C++: #include<iostream>
using namespace std;
int main(){
  int n;
  for(int i = 1 ; i < 1000 ; i++ ){
    for(int j = 1; j < 1000 ; j++){
      cin >> n;
      cout << n << endl;
    }
  }
}

C++: // ???#include <bits/stdc++.h>
#include <iostream>

// #define MP make_pair
// #define PB push_back
// #define ALL(x)  (x).begin(),(x).end()
// #define RALL(x) (x).rbegin(),(x).rend()
// #define RANGEBOX(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
// #define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
// #define rep(i, N)  for (int i = 0; i < (int)(N); i++)
// #define rrep(i, N) for (int i = N - 1; i >= 0; i--)
// #define REP(i, init, N)  for (int i = (init); i < (int)(N); i++)
// #define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
// #define PB push_back

using namespace std;

// typedef long long LL;
// typedef unsigned long long ULL;
// typedef vector<int> VI;
// typedef vector<VI > VVi;
// typedef vector<string> VS;
// typedef vector<LL> VLL;
// typedef pair<int,int> PII;

const int    INF  = 0x3f3f3f3f;
const LL     INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;

int main(){
	ios::sync_with_stdio(false);
    int sum=0;
    int x;
    int cnt=0;
    while(1){
        cin>>x;
        if(!x)
            return 0;
        sum=1000-x;
        while(sum>0){
            if(sum>=500){
                cnt++;
                sum-=500;
            }
            else if(sum>=100){
                cnt++;
                sum-=100;
            }
            else if(sum>=50){
                cnt++;
                sum-=50;
            }
            else if(sum>=10){
                cnt++;
                sum-=10;
            }
            else if(sum>=5){
                cnt++;
                sum-=5;
            }
            else{
                cnt++;
                sum-=1;
            }
        }
        cout<<cnt<<endl;
        cnt=0;
    }


    return 0;
}

C++: #include<stdio.h>

int main(void){
	int n,k;

	while(0==0){
		scanf("%d",&n);
		if(n==0)break;
		n=1000-n;
		k=0;
		k += n /500;  
		n =n%500;
		k += n/100;   
		n =n%100;
		k += n/50;    
		n =n%50;
		k += n/10;    
		n =n%10;
		k += n/5;     
		k += n%5;
		printf("%d\n",k);
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<math.h>

using namespace std;

int kane;
int oturi[6]={500,100,50,10,5,1};
int mai;

int main(){
	while(1){
		mai=0;
		cin>>kane;
		if(kane==0) break;
		kane=1000-kane;
		for(int i=0;i<6;i++){
			mai+=(kane/oturi[i]);
			kane%=oturi[i];
		}
		cout<<mai<<endl;
	}
		
	return 0;
}

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

C++: #include <iostream>

using namespace std;

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