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>
using namespace std;

int main() {
	int n,m,cnt;
	
	while(cin>>n){
		if(n==0)break;
		m=1000-n;
		
		for(;;){
			if(m>=500){
				m=m-500;
				//cout<<500<<endl;
				cnt++;
			}else if(m>=100&&500>m){
				m=m-100;
				//cout<<100<<endl;
				cnt++;
			}else if(m>=50){
				m=m-50;
				//cout<<50<<endl;
				cnt++;
			}else if(m>=10&&50>m){
				m=m-10;
				//cout<<10<<endl;
				cnt++;
			}else if(5<m){
				m=m-5;
				//cout<<5<<endl;
				cnt++;
			}else if(m<10){
				m=m-1;
				//cout<<1<<endl;
				cnt++;
			}
			if(m==0)break;
		}
		cout<<cnt<<endl;
		cnt=0;
	}
	
	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+=a;
}
return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

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

    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        n = 1000 - n;
        int ret = 0;
        for(int i=0; i<6; ++i){
            while(n >= coin[i]){
                n -= coin[i];
                ++ ret;
            }
        }
        cout << ret << endl;
    }
}

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

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};

int N;

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

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

int main() {
	int n;
	while(1){
		cin >> n;
		if(n == 0) break;
		int a = 1000-n, count = 0;
		if(a>=500){
			a -= 500;
			count++;
		}
		while(a>=100){
			a -= 100;
			count++;
		}
		if(a>=50){
			a -= 50;
			count++;
		}
		while(a>=10){
			a -= 10;
			count++;
		}
		if(a>=5){
			a -= 5;
			count++;
		}
		while(a>0){
			a--;
			count++;
		}
		cout << count << endl;
	}
	return 0;
}