Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"math"
	"sort"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)
const IDEAL = 22

type Person struct {
	Id int
	Weight float64
	Height float64
	BMI float64
}

type LessFunc func(p1, p2 *Person) bool
type multiSorter struct {
	People []Person
	less []LessFunc
}
func (ms *multiSorter) Sort(people []Person) {
	ms.People = people
	sort.Sort(ms)
}
func OrderBy(less ...LessFunc) *multiSorter {
	return &multiSorter{
		less: less,
	}
}
func (ms *multiSorter) Len() int {
	return len(ms.People)
}
func (ms *multiSorter) Swap(i, j int) {
	ms.People[i], ms.People[j] = ms.People[j], ms.People[i]
}
func (ms *multiSorter) Less(i, j int) bool {
	p, q := &ms.People[i], &ms.People[j]
	var k int
	for k = 0; k < len(ms.less); k++ {
		less := ms.less[k]
		switch {
		case less(p, q):
			return true
		case less(q, p):
			return false
		}
	}
	return ms.less[k](p, q)
}

func main() {
	id := func(p1, p2 *Person) bool {
		return p1.Id < p2.Id
	}
	bmi := func(p1, p2 *Person) bool {
		return math.Abs(p1.BMI-22) < math.Abs(p2.BMI-22)
	}
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		People := make([]Person, 0)
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			tmpid, _ := strconv.Atoi(buf[0])
			tmpheight, _ := strconv.ParseFloat(buf[1], 64)
			tmpweight, _ := strconv.ParseFloat(buf[2], 64)
			People = append(People,
			 		Person{tmpid,
			 		tmpheight/100,
			 		tmpweight,
			 		tmpweight/(tmpheight/100*tmpheight/100)})
		}
		OrderBy(bmi, id).Sort(People)
		// bestID := People[0].Id
		bestBMI := People[0].BMI
		for _, p := range People {
			if p.BMI == bestBMI {
				fmt.Println(p.Id)
			}
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

int n, ans, h, w, number;
double ans_BMI, BMI;

int main() {
	while ( ~scanf( "%d", &n ) && n ) {
		ans_BMI = INF;
		ans = INF;
		for ( int i = 0; i < n; i++ ) {
			scanf( "%d %d %d", &number, &h, &w );
			BMI = ( 1. * w / ( sq( 1. * h / 100 ) ) );
			if ( fabs( ans_BMI - 22 ) > fabs( BMI - 22 ) || ( fabs( ans_BMI - 22 ) == fabs( BMI - 22 ) && ans > number ) ) {
				ans_BMI = BMI;
				ans = number;
			}
		}
		printf( "%d\n", ans );
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
 while(true){

      int n;
      int a, h , w;
      double BMI = 0;
      double H = 0;
      double max = 0;
      double Max = 0;
      int x=0,y=0,z=0;
     
	  cin >> n ;

      if(n==0)
         break;

      for(int i=0 ;i<n ;i++){

            cin >> a >> h >> w;
            H = h / 100;

            BMI = w / (H*H);

            if(BMI < 22){
                if(max == 0){
                     max = BMI;
					 x = a;
				}
                else if(BMI < max){
					max = BMI;
                    x = a;
				}
            }
            if(22 < BMI){
                if(Max == 0){
                  Max = BMI;
				  y = a;
				}
				else if(BMI > Max){
                       Max = BMI;
                       y = a;
				}

             }
			if(22 == BMI)
				z = a;


       }
	  if(z != 0)
		  cout << z << endl;
	  else if(max == 0)
		  cout << y << endl;
	  else if(Max == 0)
		  cout << x << endl;

	  else if((22 - max) > (Max - 22))
               cout << y << endl;
           
	  else if((22 - max) < (Max - 22))
		  cout << x << endl;

          
	  else if((22 - max) == (Max - 22) && x > y)
      	  cout << y << endl;

          
	  else if((22 - max) == (Max - 22) && x < y)     
		  cout << x << endl;
		  

    }
 return 0;
}

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

using namespace std;

struct person{ int no; double diff; };

const double EPS = 1e-10;

bool operator<(const person& l, const person& r) 
{
    if(fabs(l.diff - r.diff) < EPS) {
        return l.no < r.no;
    } else {
        return l.diff < r.diff;
    }
}

bool operator>(const person& l, const person& r) 
{
    if(fabs(l.diff - r.diff) < EPS) {
        return l.no > r.no;
    } else {
        return l.diff > r.diff;
    }
}

bool operator==(const person& l, const person& r) 
{
    return (fabs(l.diff - r.diff) < EPS && l.no == r.no);
}

int n;
int main()
{
    while(cin >> n && n) {
        vector<person> ps(n);
        int i, h, w;
        double bmi;
        for(int k = 0; k < n; k++) {
            cin >> i >> h >> w;
            bmi = ((double)w / (h * h)) * 10000;
            ps[k] = (person){i, fabs(22.0 - bmi)};
        }
        sort(ps.begin(), ps.end());
        cout << ps[0].no << endl;
    }
}

C++: #include<iostream>
#include<utility>
#include<cstdlib>

typedef std::pair<double, int> P;

int n;

int main()
{
	while( std::cin >> n, n )
	{
		P ans( 201, 1000001 );
		
		while( n-- )
		{
			int i; 
			double h, w;
			std::cin >> i >> h >> w;
			h /= 100.0;
			ans = std::min( ans, P( abs( w / ( h * h ) - 22 ), i ) );
		}

		std::cout << ans.second << std::endl;
	}

	return 0;
}

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


class person{
public:
	int num;
	double w;
	double h;
	double bmi;
	person(int n,double _w,double _h,double _bmi){
		num=n;w=_w;h=_h;bmi=_bmi;
	}
	double getBMIPoint(){
		if(h!=0)bmi=w/((h/100)*(h/100));
		if(bmi<22){
			return 22-bmi;
		}
		else {
			return bmi-22;
		}
	}
	void print(){
		cout<<num<<" "<<w<<" "<<h<<""<<bmi<<endl;
	}
};
int
main(){
	
	for(int d;cin>>d;){
		person stable=person(0,0,0,100);
		for(int i=0;i<d;i++){
		//	stable.print();
			int num;
			double w,h;
			cin>>num>>h>>w;
			person p=person(num,w,h,0);
		//	p.print();
		//	cout<<p.getBMIPoint()<<endl;
			if(stable.getBMIPoint()>=p.getBMIPoint()){
					if(stable.getBMIPoint()==p.getBMIPoint()){
						if(stable.num<p.num)continue;
					}
					stable=p;
			}
			
		}
		cout<<stable.num<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
	int n;
	while(true){
	cin>>n;
	if(n==0)
		break;
	double best=10000.0;
	int ans;
	int i,h,w;
	for(int u=0;u<n;u++){
		cin>>i>>h>>w;
		double r=w*10000/h/h;
		r-=22;
		if(r<0)
			r=-r;
		if(best>r||(best==r&&ans>i)){
			best=r;
			ans=i;
		}
	}
	cout<<ans<<endl;
	}
	return 0;
}