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>

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

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;
  }
  while(n >= 50){
    n -= 50;
    ++ans;
  }
  while(n >= 10){
    ++ans;
    n -= 10;
  }
  while(n >= 5){
    n -=5;
    ++ans;
  }
  cout << ans + n << endl;
  return 0;
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



int n;

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

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

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

int main(){
  int n;
  while(cin >> n,n){
    n = 1000-n;
    int pos = 0, ans = 0;
    while(n){
      if(n>=coin[pos]){
	n -= coin[pos];
	ans++;
      }else{
	pos++;
      }
    }
    cout << ans << endl;
  }
}

C++: #include<iostream>
using namespace std;
int main(){
    int a[5];
    int n;
    int k = 0;
    int s[6] = {500,100,50,10,5,1};
    while(cin >> n){
        if(n == 0)break;
        a[k] = 0;
        int c = 1000 - n;
        for(int i = 0;i < 6;i++){
            if(c >= s[i]){
                int m = c/s[i];
                //cout << m << " ";
                a[k] += m;
                c -= m * s[i];
            }
        }
        k++;
    }
    for(int i = 0;i < k; i++){
        cout << a[i] <<endl;
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;

static const double EPS = 1e-8;


int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;
		int money = 1000 - n;
		int res = 100000;
		for(int i=0;i<=2;i++){
			for(int j=0;j<=10;j++){
				for(int k=0;k<=20;k++){
					for(int l=0;l<=100;l++){
						for(int m=0;m<=200;m++){
							if(500*i+100*j+50*k+10*l+5*m > money) continue;
							else res = min(i+j+k+l+m + (money - (500*i+100*j+50*k+10*l+5*m)),res);
						}
					}
				}
			}
		}

		printf("%d\n",res);
	}

}

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, a){
		b = 1000 - a;
		int ret = 0;
		while(b != 0){
			if(b >= 500){
				ret+= b/500;
				b %= 500;
			}else if(b >= 100){
				ret += b/100;
				b %= 100;
			}else if(b >= 50){
				ret += b/50;
				b %= 50;
			}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 <iostream>
#include <cstdio>

using namespace std;

#define print(a) printf("%d\n", a)

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)

#define MAX(a,b) ((a)>(b)?(a):(b))


int main(){
	int co, mod, ch = 0;
	
	while (cin >> co != EOF){
		
		mod = 1000 - co;
		
		ch += mod/500;
		mod = mod%500;
		
		ch += mod/100;
		mod = mod%100;
		
		ch += mod/50;
		mod = mod%50;
		
		ch += mod/10;
		mod = mod%10;
		
		ch += mod/5;
		mod = mod%5;
		
		ch += mod;
		
		cout << ch << endl;
	}
	
	return 0;
}

C++: // JOI - Prelim  0521
// おつり
#include <bits/stdc++.h>

using namespace std;

using UInt = uint_fast64_t;

template <typename T>
using vec1 = vector<T>;

template <typename T>
T cin2var()
{
    T val;
    cin >> val;
    return val;
}

const vec1<UInt> coins{500, 100, 50, 10, 5, 1};

bool sub()
{
    const auto price(cin2var<UInt>());

    if (!price) return false;

    UInt pay          = 1000 - price;
    UInt pay_out_coin = 0;
    for (const auto& coin : coins) {
        pay_out_coin += pay / coin;
        pay = pay % coin;
    }
    cout << pay_out_coin << endl;
    return true;
}

int main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    while (sub());
    return 0;
}

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

int main() {
        const vector<int> coin{500,100,50,10,5,1};
        while (1) {
                int n, z, c=0;
                cin >> n;
                if (n == 0) break;
                z = 1000 - n;
                rep(i, coin.size()) {
                        c += z / coin.at(i);
                        z =  z % coin.at(i);
                }
                cout << c << endl;
        }
        return 0;
}