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

C++: #include<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(n,a) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265

int dp[1100]={};
int a[10]={1,5,10,50,100,500};

void f(int n){
  int k = 1000-n;
  cout<<dp[k]<<endl;
}

int main(){
  int n;
  fill(dp,dp+1100,inf);
  dp[0]=0;
  rep(i,1100){
    rep(j,6){
      if(i-a[j]<0) continue;
      dp[i]=min(dp[i],dp[i-a[j]]+1);
    }
  }
  while(1){
    cin>>n;
    if(n==0) break;
    f(n);
  }
}

C++: #include<iostream>
using namespace std;
int main()
{
  int money,oturi,maisu = 0;
  
  cin >> money;
  oturi = 1000 - money;
  for(int i = 1000;oturi > 0;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;
  }