Skip to content

Instantly share code, notes, and snippets.

@zhouji
Created November 22, 2012 10:20
Show Gist options
  • Save zhouji/4130415 to your computer and use it in GitHub Desktop.
Save zhouji/4130415 to your computer and use it in GitHub Desktop.
Function to generate char arraies according to the ration defination.
#include <iostream>
#include <string>
using namespace std;
int min_idx(float * ratio, int * idx,int size){
float min_weight = 10000000.0f;
int min_idx = 0;
for( int i = 0 ; i < size ; i++){
float weight = idx[i]/ratio[i];
cout << weight << endl;
if( weight < min_weight){
min_weight = weight;
min_idx = i;
}
}
return min_idx;
}
int main(){
float ratio[] = {4.34,0.67,0.11};
char marks[] = {'a','b','c'};
char tgt[100];
int idx[] = {0,0,0};
for( int i = 0 ; i < 100; i++){
int cur_idx = min_idx(ratio,idx,3);
tgt[i] = marks[cur_idx];
idx[cur_idx]++;
}
for( int i = 0 ; i < 100; i++){
cout << tgt[i];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment