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

	printf("%d\n", change);

	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int n;
  while(cin>>n,n){
    n=1000-n;
    int a[]={500,100,50,10,5,1};
    int ans=0;
    for(int i=0;i<6;i++){
      while(n>=a[i]) n-=a[i],ans++;
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include <algorithm>
#include <string>
using namespace std;
#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(int)(n); i++)
typedef long long ll;

int main()
{int n, A, ans=0;
while(cin >> n, n){ans=0; A = 1000 - n;
int C[6]={500, 100, 50, 10, 5, 1};
rep(i, 6)
{ans += A/C[i]; A %= C[i];}
cout << ans << endl;}

       
 }

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

int main(){

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

	return 0;

}

C++: #include<iostream>
using namespace std;
int main(){
  int turi;
  int hara;
  int count;
  while(1){
    count=0;
    cin>>hara;
    if(hara==0) break;
    turi=1000-hara;
    while(turi!=0){
      if(turi>=500){
	turi=turi-500;
	count++;
      }
      else if(turi>=100){
	turi=turi-100;
	count++;
      }
      else if(turi>=50){
	turi=turi-50;
	count++;
      }

      else if(turi>=10){
	turi=turi-10;
	count++;
      }
      else if(turi>=5){
	turi=turi-5;
	count++;
      }
      else if(turi>=1){
	turi=turi-1;
	count++;
      }
    }
    cout<<count<<endl;
  }
}

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

using namespace std;

#define REP(i,m) for(int i=0; i<m; i++)
#define FOR(i,n,m) for(int i=n; i<m; i++)
#define INF 1000000007
#define INFL (1LL << 60)
#define MOD 998244353
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define ll long long int
#define P pair<ll, ll>

int main(){
	while(true){
		int x;
		cin >> x;
		if(x==0) break;
		x = 1000-x;
		int ans = 0;
		ans += x/500;
		x %= 500;
		ans += x/100;
		x %= 100;
		ans += x/50;
		x %= 50;
		ans += x/10;
		x %= 10;
		ans += x/5;
		x %= 5;
		ans += x;
		cout << ans << endl;
	}
	return 0;
}

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

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <vector>
#include <stack>

using namespace std;
////////////////*My library*////////////////////////
#define rep_to(a,i,n) for(int i=a;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define rep_r(i,n) for(int i=n-1;i>=0;i--)
#define debug(x) cout<<x<<endl
#define s_1(x) cin>>x
#define s_2(x,y) cin>>x>>y
////////////////////////////////////////////////////

////////////////////////
/*prime()*/
/*primecount()*/
/*sort()*/
/*imos???*/
/*????????¢?´¢*/
/*??±???????????¢?´¢*/
/*???????¨???????*/
///////////////////////
int main()
{
	int N;
	int coin[7]={500,100,50,10,5,1};

	while(1)
	{
		int count=0;
		int j=0;

		s_1(N);
		if(N==0)break;
		int max_c=1000-N;
		
		while(max_c>0)
		{
			rep_to(j,i,6)
			{
				if(coin[i]<=max_c)
				{
					max_c-=coin[i];
					count++;
					break;
				}
				else j++;
			}
		}
		cout<<count<<endl;
	}
	
	return 0;
}

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

 
int main () 
{
  int m1;
  while(1)
  {
   cin>>m1; 
   if(m1<0||m1<=1000)
   {
       break;
   }
   else
    {
    int m2,m3,m4,m5,m6,m7;

   m2=1000-m1;
   m3=m2/500;
   m4=m2%500/100;
   m5=m2%500%100/50;
   m6=m2%500%100%50/10;
   m7=m2%500%100%50%10/1;
  
   cout<<m3+m4+m5+m6+m7<<'\n';
   }
 }
   
  return 0;
}

C++: #include<stdio.h>
int main(void){
	int oturi;
	int mon,i,ans=0;
	int a[5]={500,100,50,10,5};
	while(1){
		scanf("%d",&mon);
		if(mon==0)break;
		oturi=1000-mon;
		for(i=0;i<5;i++){
			ans+=oturi/a[i];
			oturi%=a[i];
		}
		ans+=oturi;
		printf("%d\n",ans);
	}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int n,m,a,b,c,d,e,f;
	cin>>n;
	m=1000-n;
	for(a=0;a<2;a++){
	    for(b=0;b<5;b++){
			for(c=0;c<2;c++){
				for(d=0;d<5;d++){
					for(e=0;e<2;e++){
						for(f=0;f<5;f++){
							if(a*500+b*100+c*50+d*10+e*5+f*1==m){
								cout<<a+b+c+d+e+f<<endl;
								break;
                            }
							}
						}
					}
				}
			}
	    }
	return 0;
	}

C++: #include<stdio.h>
int main()
{
	int m,r;
	while(1){
		r=0;
		scanf("%d",&m);
		if(m==0)return 0;
		m=1000-m;
		if(m>=500){
			m-=500;
			r++;
		}
		while(m>=100){
			m-=100;
			r++;
		}
		if(m>=50){
			m-=50;
			r++;
		}
		while(m>=10){
			m-=10;
			r++;
		}
		if(m>=5){
			m-=5;
			r++;
		}
		while(m>=1){
			m-=1;
			r++;
		}
		printf("%d\n",r);
	}
	return 0;
}

C++: #include <stdio.h>

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

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

int main(){
	int m,t;
	int p=0;
	cin >> m;
	if(m==0){
		break;
	}
	t=1000-m;
	while(1){
	if(t>=500){
	t-=500;	
	p++;
	}else if(t>=100){
	t-=100;
	p++;
	}else if(t>=50){
	t-=50;
	p++;
	}else if(t>=10){
	t-=10;
	p++;
	}else if(t>=5){
	t-=5;
	p++;
	}else{
	t--;
	p++;
		}
	if(t==0){
		break;
	}	
	}
	cout << p << '\n';
	count=0;
	
	return 0;
}

C++: #include <stdio.h>

void main(){
	int okane;
	int oturi;
	int count = 0;
	int a[] = { 1, 5, 10, 50, 100, 500 };

	scanf("%d", &okane);
	oturi = 1000 - okane;

	for (int i = 5; i >= 0; i--){
		if (okane >= a[i]){
			okane = okane - a[i];
			count = count + 1;
		}
	}
	printf("%d\n",count);

}

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

int main(){
int total;
int change;
int count=0;
while(true){
 cin>>total;
 if(total==0){
    break;
  } 
 change=1000-total;
 while(true){
   if(change>=500){
     change-=500;
     count++;
   }else if(change>=100){
     change-=100;
     count++;
   }else if(change>=50){
     change-=50;
     count++;
   }else if(change>=10){
     change-=10;
     count++;
   }else if(change>=5){
     change-=5;
     count++;
   }else{
     change--;
     count++;
    }
  if(change==0){
    break;
  }
 }
 cout<<count<<"\n";
 count=0;
}
return 0;
}