Go: package main

import "fmt"

func main() {
	var n, coins, change int
	for {
		fmt.Scan(&n)
		if n == 0 {
			return
		}
		coins = 0
		change = 1000 - n
		for change > 0 {
			coins++
			switch {
			case change >= 500:
				change -= 500
			case change < 500 && change >= 100:
				change -= 100
			case change < 100 && change >= 50:
				change -= 50
			case change < 50 && change >= 10:
				change -= 10
			case change < 10 && change >= 5:
				change -= 5
			default:
				change--
			}
		}
		fmt.Println(coins)
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cctype>
#define rep(i,a,b) for(int i=(a);i<b;i++)
#define INF 1000000000
#define MAX_N 105
using namespace std;
bool debug=false;

bool check(int n){
  int k=0,x=n;
  while(x){
    k++;
    x/=10;
  }
  if(k==1){
    return true;
  }else if(k==2){
    if(n%10==n/10)return true;
  }else if(k==3){
    if(n%10==n/100)return true;
  }else if(k==4){
    if((n%10==n/1000)&&(n/10%10==n/100%10))return true;
  }
  return false;
}

int main(){
  int n;
  cin>>n;
  for(int i=0;i<1000;i++){
    if(check(n-i)&&n-i>0){
      cout<<n-i<<endl;
      return 0;
    }
    if(check(n+i)&&n<=10000){
      cout<<n+i<<endl;
      return 0;
    }
  }
  return 0;
}

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

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

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

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
#define INF 10000000


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

C++: #include<stdio.h>
int main(void)
{
	int n,a,b,x,y,z,m,i,c,l,k,h,j,r,f;
	scanf("%d",&n);
	a=1000-n;
	b=a/500;
	x=a%500;
	y=x/100;
	z=x%100;
	m=z/50;
	i=z%50;
	l=i/10;
	k=i%10;
	h=k/5;
	j=k%5;
	r=j/1;
	f=j%1;
	c=b+y+m+l+h+r;
	printf("%d\n",c);
	return 0;
}

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

C++: #include <iostream>
using namespace std;
 
int main(){
  int a,cnt,b,c[5]={500,100,50,10,5};
  while(cin>>a&&a){
    cnt=0;
    b=1000-a;
    for(int i=0;i<5;i++) cnt+=b/c[i],b%=c[i];
    cnt+=b;
    cout<<cnt<<endl;
  }
  return 0;
}

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

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int money = 1000;
	int pay, change, i;

	cin >> pay;
	change = money - pay;

	while(1){
	if(change == 0 || change == 1000) break;
	else if(change < 0 || 1000 < change) break;
	while(1){
		if(change - 500 >= 0){
			i += 1;
			change -= 500;
			break;
		}
		else if(change - 100 >= 0){
			i += 1;
			change -= 100;
			break;
		}
		else if(change - 50 >= 0){
                        i += 1;
                        change -= 50;
                        break;
                }
		else if(change - 10 >= 0){
                        i += 1;
                        change -= 10;
                        break;
                }
		else if(change - 5 >= 0){
                        i += 1;
                        change -= 5;
                        break;
                }
		else{
			i += 1;
			change -= 1;
			break;
		}
	}
	}
	
	if(pay == 0 ) return 0;
        else if(pay < 0 || 1000 < pay) return 0;
	else cout << i << "\n";

	
	return 0;
}

C++: #include <iostream>
#include <functional>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <map>
#include <cmath>

using namespace std;

namespace _{struct _{_(){ // initialize
  cin.tie(0); ios::sync_with_stdio(false);
}}_;}
template <typename T, size_t size>
inline size_t len(const T (&)[size]) { return size; }
#define println(s) cout << s << '\n';
#define str to_string
#define rep(i, n) for (int i=0; i<int(n); i++)
#define all(c) (c).begin(), (c).end()


int main() {
  while (1) {
    int in;
    cin >> in;
    if (in == 0) break;
    int n = 1000 - in;

    int c = 0;
    c += n / 500;
    n %= 500;

    c += n / 100;
    n %= 100;

    c += n / 50;
    n %= 50;

    c += n / 10;
    n %= 10;

    c += n / 5;
    n %= 5;

    c += n / 1;
    n %= 1;

    println(str(c));
  }
  return 0;
}

C++: #include <iostream>

using namespace std;

void solve()
{
	int n;
	cin >> n;
	n = 1000 - n;
	int count = 0;
	count += n / 500;
	n %= 500;
	count += n / 100;
	n %= 100;
	count += n / 50;
	n %= 50;
	count += n / 10;
	n %= 10;
	count += n / 5;
	n %= 5;
	count += n;
	cout << count << endl;
}

int main()
{
	solve();
	return(0);
}

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

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

C++: #include <stdio.h>
int main()
{
  int a,b,c,d,e,f,sum,ch ;
  scanf("%d",&sum);
  ch=1000-sum;
  printf("%d\n",ch=1000-sum);
  a=ch/500 ,b=(ch-500*a)/100 , c=(ch-500*a-100*b)/50 , d=(ch-500*a-100*b-50*c)/10 , e=(ch-500*a-100*b-50*c-10*d)/5 , f=(ch-500*a-100*b-50*c-10*d-5*e);
  printf("%d %d %d %d %d\n" ,a,b,c,d,e,f );
    printf("Change=%d\n",a+b+c+d+e+f);

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
#include <array>
#include <complex>
#include <string>
#include <utility>
#include <map>
int main() {
	int a[6], sum[5] = {0};
    int i = 0;
    while(a[i]) {
        std::cin >> a[i];
        if (a[i] == 0) {
            break;
        }
        i++;
        
    }
	i = 0;
    while (a[i] != 0) {
        sum[i] += (1000 - a[i]) / 500;
        a[i] = (1000 - a[i]) % 500;
        sum[i] += a[i] / 100;
        a[i] = a[i] % 100;
        sum[i] += a[i] / 50;
        a[i] = a[i] % 50;
        sum[i] += a[i] / 10;
        a[i] = a[i] % 10;
        sum[i] += a[i] / 5;
        a[i] = a[i] % 5;
        sum[i] += a[i];
        i++;
    }
    for (int j = 0; j<i; j++) {
        std::cout << sum[j] << std::endl;
    }
    return 0;
}