Go: ///
// File:  0521.go
// Author: ymiyamoto
//
// Created on Wed Mar  7 22:11:28 2018
//
package main

import (
	"fmt"
)

var money int

func main() {
	for {
		fmt.Scan(&money)
		if money == 0 {
			break
		}
		rem := 1000 - money

		count := 0
		for _, coin := range []int{500, 100, 50, 10, 5, 1} {
			count += rem / coin
			rem %= coin
		}
		fmt.Println(count)
	}
}

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

#define llong long long
#define inf 999999999
#define mp make_pair
#define pb push_back
#define fn fill_n
typedef pair<int, int> P;
typedef pair<P, int> PP;
typedef pair<PP, int> PPP;
typedef struct data{
	int now;
	int cost;
}data;
int nh[4] = {1, 0, -1, 0};
int nw[4] = {0, 1, 0, -1};





int main(){
	int a, b;
	while(cin >> a){
		b = 1000 - a;
		int ret = 0;
		while(b != 0){
			if(b >= 500){
				ret++;
				b -= 500;
			}else if(b >= 100){
				ret += b/100;
				b %= 100;
			}else if(b >= 10){
				ret += b/10;
				b %= 10;
			}else if(b >= 5){
				ret += b/5;
				b %= 5;
			}else{
				ret += b;
				b = 0;
			}
		}
		cout << ret << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main(){
	while(1){
		int a,ans=0,m[6]={500,100,50,10,5,1};
		cin>>a;
		if(a==0) break;
		a=1000-a;
		while(1){
			if(a==0) break;
			rep(i,0,6){
				if(a>=m[i]){
					ans++; a-=m[i]; break;
				}
			}
		}
		o(ans);
	}
}

C++: #include<iostream>
#include<vector>
using namespace std;
int main() {
	vector<int>l;
	int n;
	while (cin >> n) {
		if (n == 0)break;
		int sum = 0;
		n = 1000 - n;
		if (n >= 100) {
			int a = n % 10;//1??????
			n -= a;
			n /= 10;
			int b = n % 10;//10??????
			n -= b;
			n /= 10;
			int c = n;//100??????
			if (a >= 5) {
				sum++;
				a -= 5;
			}
			sum += a;
			if (b >= 5) {
				sum++;
				b -= 5;
			}
			sum += b;
			if (c >= 5) {
				sum++;
				c -= 5;
			}
			sum += c;
		}
		else if (n >= 10){
			int a = n % 10;
			n -= a;
			n /= 10;
			int b = n;
			if (a >= 5) {
				sum++;
				a -= 5;
			}
			sum += a;
			if (b >= 5) {
				sum++;
				b -= 5;
			}
			sum += b;
		}
		else{
			if (n >= 5) {
				sum++;
				n -= 5;
			}
			sum += n;
		}
		l.push_back(sum);
	}
	for (int i : l) {
		cout << i << endl;
	}
}

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

int main() {
	int a,b,c,d,e,f,g;
	cin >> a;
	b=1000-a;
	c=b%500;
	d=c%100;
	e=d%50;
	f=e%10;
	g=f%5;
	cout<<b/500+c/100+d/50+e/10+f/5+g<<endl;
	return 0;
}

C++: #include <iostream>

using std::cout;
using std::cin;

int main(int argc, const char * argv[]) {
    std::ios::sync_with_stdio(false);
    
    int en;
    int ans;
    int set[]={500,100,50,10,1};
    
    cin>>en;
    
    en=1000-en;
    
    for (int i=0;i<sizeof(set)/sizeof(set[0]); ++i)
    {
        while(true)
        {
            if(set[i]<=en)
            {
                en-=set[i];
                ans++;
            }
            else
            {
                break;
            }
        }
    }

    cout<< ans <<"\n";
    
    return 0;
}

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

using namespace std;

int main(){
	/*
	//数あて
	int n;
	cin >> n;
	int p[200][3];

	for(int i=0;i<n;i++)
	{
		cin >> p[i][0] >> p[i][1] >> p[i][2];
	}

	int _p;
	for(int k=0;k<3;k++)
	{
		for(int i=0;i<n;i++)
		{
			_p = p[i][k];
			for(int j=i+1;j<n;j++)
			{
				if(_p == p[j][k])
				{
					p[i][k] = 0;
					p[j][k] = 0;
				}
			}
		}
	}

	for(int i=0;i<n;i++)
	{
		cout << p[i][0] + p[i][1] + p[i][2] << endl;
	}
	*/

	/*
	//得点
	int ai,am,as,ae,bi,bm,bs,be;
	cin >> ai >> am >> as >> ae >> bi >> bm >> bs >> be;
	int answer=ai+am+as+ae;
	if(answer < bi+bm+bs+be)
	{
		answer=bi+bm+bs+be;
	}
	cout << answer << endl;
	*/

	//おつり
	while(true){
		int N;
		if(N == 0) break;
		
		int nedan;
		cin >> nedan;
		int oturi = 1000-nedan;
		int onum= 0;
		if(oturi >= 500)
		{
			onum++;
			oturi -= 500;
		}
		while(oturi>=100)
		{
			onum++;
			oturi -= 100;
		}
		if(oturi >= 50)
		{
			onum++;
			oturi -= 50;
		}
		while(oturi>=10)
		{
			onum++;
			oturi -= 10;
		}
		if(oturi >= 5)
		{
			onum++;
			oturi -= 5;
		}
		while(oturi>=1)
		{
			onum++;
			oturi -= 1;
		}
		cout << onum << endl;

	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (a); v < (b); ++v)
#define FORE(v, a, b) for(int v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define RS resize
#define CLR clear
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
using namespace std;

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

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
using namespace std;
int main()
{
	int a=1;
	int b;
	int c;
	
	while (1)
	{
		cin >> a;
		if (a = 0)
		{
			break;
		}
		b = a;
		if (b >= 500)
		{
			b = b - 500;
			c++;
		}
		else if (b >= 100)
		{
			b = b - 100;
			c++;
		}
		else if (b >= 50)
		{
			b = b - 50;
			c++;
		}
		else if (b >= 10)
		{
			b = b - 10;
			c++;
		}
		else if (b >= 5)
		{
			b = b - 5;
			c++;
		}
		else if (b >= 1)
		{
			b = b - 1;
			c++;
		}
		cout << c << endl;
	}
	getchar();
	getchar();
	return 0;
}

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

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
	int money=0,money1=0,all=0;
	while(1){
	cin>>money;
	if(money==0){break;}
	money1=1000-money;
	all+=(money1/500);
	all+=(money1%500/100);
	all+=(money1%500%100/50);
	all+=(money1%500%100%50/10);
	all+=(money1%500%100%50%10/5);
	all+=(money1%500%100%50%10%5/1);
	cout<<all<<endl;
	all=0;
	}
	return 0;
}
//9:18.853クリア

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

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

using namespace std;
int c[6] = {500,100,50,10,5,1};
int main(){

	int n;
	scanf("%d",&n);

	n = 1000 - n;
	int ans = 0;

	for(int i = 0;i < 6;i++){
		ans += (n / c[i]);

		n = n % c[i];
	}

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

	return 0;

}

C++: #include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 100000005
#define MAX 5001
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int a[6] = {500, 100, 50, 10, 5, 1};
int solve(int n){
	int ans = 0;
	while(1){
		for(int i = 0;i < 6;i++){
			if(n >= a[i]){
				n -= a[i];
				ans++;
				break;
			}
		}
		if(n == 0)break;
	}
	return ans;
}

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

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

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