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 <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;
}

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

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

int main(){
	int cnt = 0,i = 5;
	cin >> X;
	while( X > 0 ){
		while( X >= change[i] ){
			X -= change[i];
			cnt++;
		}
	}
	cout << cnt << endl;
	return 0;
}

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

int main(){
	int x, y;
	while(scanf("%d", &x) != EOF){
		x = 1000 - x;
		y = 0;
		while (x >= 500){
			x -= 500;
			y++;
		}
		while (x >= 100){
			x -= 100;
			y++;
		}
		while (x >= 50){
			x -= 50;
			y++;
		}
		while (x >= 10){
			x -= 10;
			y++;
		}
		while (x >= 5){
			x -= 5;
			y++;
		}
		while (x >= 1){
			x -= 1;
			y++;
		}
		printf("%d\n", y);
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

int main(){
	int pay=0,coin=0;
	while(cin>>pay,pay){
		coin=0;
	pay=1000-pay;
	if(pay/500>=1){
		pay-=500;
		coin++;
	}
	if(pay>=100){
		coin+=pay/100;
		pay-=100*(pay/100);
	}
	if(pay>=50){
		coin+=pay/50;
		pay-=50*(pay/50);
	}
	if(pay>=10){
		coin+=pay/10;
		pay-=10*(pay/10);
	}
	if(pay>=5){
		coin+=pay/5;
		pay-=5*(pay/5);
	}
	if(pay>=1){
		coin+=pay;
	}
	cout<<coin<<endl;
}
}

C++: #include<iostream>
using namespace std;
int main(){
  int mon,c;
  while(1){
    cin>>mon;
    if(mon==0){
      break;
    }
    mon=1000-mon;
    c=0;
    for(int i=0;i<15;i++){
      if(mon==0){
	break;
      }
      if(mon>=500){
	mon-=500;
	c++;
      }if(mon<500&&mon>=100){
	mon-=100;
	c++;
      }if(mon<100&&mon>=50){
	mon-=50;
	c++;
      }if(mon<50&&mon>=10){
	mon-=10;
	c++;
      }if(mon<10&&mon>=5){
	mon-=5;
	c++;
      }if(mon<5){
	c+=mon;
	mon=0;
      }
    }
  cout<<c<<endl;
  }
  return 0;
}