I'm trying to get better at C++ so I have been doing some of the challenges at topcoder.
This was the problem
Given an image as a vector <string>, scale it up by factor. That is, if the original image is M x N, the scaled image should be M*factor x N*factor. Each character in the input vector <string> should be represented by factor x factor of the same character in the output (see examples).
example
{"abc",
"def",
"ghi"}
3
Returns:
{ "aaabbbccc",
"aaabbbccc",
"aaabbbccc",
"dddeeefff",
"dddeeefff",
"dddeeefff",
"ggghhhiii",
"ggghhhiii",
"ggghhhiii" }
Here is my solution but 4 loops seems too much. Can anybody show me what I could do better? Thanks
Code:#include <vector> #include <iostream> using namespace std; class ImageEnlarger{ public: vector <string> enlarge(vector <string> image, int factor){ string tempImage="", tempReturn=""; vector <string> returnImage; for(int i=0;i<image.size();i++){ tempImage=image[i]; for(int j=0;j<factor;j++){ for(int k=0;k<tempImage.length();k++){ for(int z=0;z<factor;z++){ tempReturn+=tempImage[k]; }//end for }//end for returnImage.push_back(tempReturn); tempReturn=""; }//end for }//end for return returnImage; }//end enlarge };//end class



LinkBack URL
About LinkBacks



