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>
using namespace std;

int main() {
	int all, rem, count = 0, tmp;
	while (1) {
		cin >> all;
		if (all == 0)
			break;
		rem = 1000 - all;
		if (rem >= 500) {
			count = count + 1;
			rem = rem - 500;
		}
		if (rem >= 100) {
			tmp = rem / 100;
			count = count + tmp;
			rem = rem - (100 * tmp);
		}
		if (rem >= 50) {
			tmp = rem / 50;
			count = count + tmp;
			rem = rem - 50 * tmp;
		}
		if (rem >= 10) {
			tmp = rem / 10;
			count = count + tmp;
			rem = rem - 10 * tmp;
		}
		if (rem >= 5) {
			tmp = rem / 5;
			count = count + tmp;
			rem = rem - 5 * tmp;
		}
		count = count + rem;
		cout << count << 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 fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int,int> pii;

int main(){
	while(1){
		int a,ans=0;
		cin>>a;
		if(a==0) break;
		a=1000-a;
		while(1){
			if(a==0) break;
			if(a>=500) a-=500;
			else if(a>=100) a-=100;
			else if(a>=50) a-=50;
			else if(a>=10) a-=10;
			else if(a>=5) a-=5;
			else if(a>=1) a-=1;
			ans++;
		}
		o(ans);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9+7;
const int INF = 1e9;
const ll INFll = 1e18;
#define put(n) cout<<(n)<<endl
#define Cout(n) cout<<(n)
#define rep(i,num,N) for(int(i)=(num);(i)<(N);++(i))
#define rrep(i,num,N) for(int (i)=(num);(i)>(N);--(i))
#define all(v) (v).begin() , (v).end()
#define rall(v) (v).rbegin() , (v).rend()
#define MP make_pair
#define pb(q) push_back(q)
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,coin[6] = {500,100,50,10,5,1};
    
    while(cin>>N,N!=0){
        N = 1000 - N;
        int ans = 0;
        for(int i=0;i<6&&N!=0;){
            if(coin[i] <= N){
                N -= coin[i];
                ++ans;
            }
            else ++i;
        }
        put(ans);
    }
}

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

int main(){
while(1){
	int a[6]={500,100,50,10,5,1};
	int n,ans=0;
	scanf("%d",&n);
	if(n==0)break;
	n=1000-n;
	for(int i=0;i<6;i++){
		ans+=n/a[i];
		n%=a[i];
	}
	printf("%d\n",ans);
}
}

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

int main(){
int n;
int ca=0;
while(cin>>n&&n!=0){
int m=1000-n;
if(m>=500){
	m-=500;
	ca++;
		}
		if(m>=100){
			int i=m/100;
			ca+=i;
			m-=i*100;
			}
			if(m>=50){
				ca++;
				m-=50;
				}
				if(m>=10){
					int i=m/10;
					ca+=i;
					m-=i*10;
					}
					if(m>=5){
						ca++;
						m-=5;
						}
						if(m>=1){
							int i=m;
							ca+=i;
							}
						
		cout<<ca<<endl;
		ca=0;
		}
		return 0;					
		}

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

int main(
	int n,m;
	cin >> n;

	n = 1000 - n;
	m = 0;

	if ( n >= 500) {
		m = m + 1;
	}
	if ( n >= 100) {
		m = m + (n / 100);
	}
	if (n >= 50) {
		m = m + (n / 50);
	}
	if (n >= 10) {
		m = m + (n / 10);
	}
	if (n >= 5) {
		m = m + (n / 5);
	}
	if (n >= 1) {
		m = m + (n / 1);
	}

	cout << m << endl;

	return 0;
)

C++: #include <iostream>
using namespace std;
int main(){
    int a;
    while(cin>>a){
        if(a==0)return 0;
    int b=1000-a;
    int count=0;
    if(b/500==1){
        b=b-500;
        count++;
    }
    if(b/100!=0){
            int c=b/100;
            count+=c;
            b=b-100*c;
        }
    if(b/50==1){
            b=b-50;
            count++;
        }
     if(b/10!=0){
            int d=b/10;
            count+=d;
            b=b-10*d;
        }
     if(b/5==1){
            b=b-5;
            count++;
        }
    count+=b%5;
    cout<<count<<endl;
    }
}

C++: #include <stdio.h>

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

int main(void) {
	while (true) {
		scanf("%d", &N);
		if (N == 0) { break; }

		int res = 1000 - N;
		while (res != 0) {
			if (res >= 500) {
				ans += res / 500;
				res -= 500 * (res / 500);
			}
			else if (res >= 100) {
				ans += res / 100;
				res -= 100 * (res / 100);
			}
			else if (res >= 50) {
				ans += res / 50;
				res -= 50 * (res / 50);
			}
			else if (res >= 10) {
				ans += res / 10;
				res -= 10 * (res / 10);
			}
			else if (res >= 5) {
				ans += res / 5;
				res -= 5 * (res / 5);
			}
			else { ans += res; res = 0; }
		}
	}
	printf("%d\n", ans);
	return 0;
}

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


int main(){

  while(1){
    int n;
    cin >> n;
    if(n==0)break;
    n = 1000 - n;
    int ans=0;
    if(n >= 500){
      n -= 500;
      ans++;
    }
    while(n >= 100){
      n-=100;
      ans++;
    }
    if(n >= 50){
      n-=50;
      ans++;
    }
    while(n >= 10){
      n-=10;
      ans++;
    }
    if(n >= 5){
      n-=5;
      ans++;
    }
    ans += n;
 
  cout << ans <<endl;
  }


}

C++: #include <iostream>

using namespace std;

int main(){
  int money;
  while(cin >> money && money !=0){
    int count =0;
    int res = 1000 - money;
    for(int i=0;i<3;i++){
      //      cout << res << endl;
      count +=res %10;
      if(res % 10 >5){ count -= 4;}
      res /=10;
    }
    cout << count << endl;
  }
}

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;
	*/
	
	//おつり
	int onum;
	int i = 0;
	while(true){
		int N;
		cin >> N;
		if(N == 0) break;
				
		int oturi = 1000-N;
		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;
		}
		
		i++;
	}
    cout << onum << endl;
	
	return 0;
}

C++: //
//  main.cpp
//  hoge
//
//  Created by ???????????? on 2017/11/09.
//  Copyright ?? 2017??´ ????????????. All rights reserved.
//

#include <iostream>

using namespace std;

int main(int argc, const char * argv[]) {
    int d[5];
    for (int i=0; i<5; i++) {
        cin >> d[i];
    }
    
    for (int i=0; i<5; i++) {
        int n=1000-d[i],count=0;
        
        if(d[i] == 0) break;
        
        while (n % 5 == 0) {
            if(n % 500 == 0) {
                count++;
                n-=500;
                continue;
            }
            if(n % 100 == 0) {
                count++;
                n-=100;
                continue;
            }
            if(n % 50 == 0) {
                count++;
                n-=50;
                continue;
            }
            if(n % 10 == 0) {
                count++;
                n-=10;
                continue;
            }
            if(n % 5 == 0) {
                count++;
                n-=5;
                continue;
            }
        }
        count+=n;
        cout << n << endl;
    }
    
    return 0;
}