loops - c++ mathematical calculations -
i have solve problem using c++, , can't come solution.
the condition is. in skyscraper there lot of offices, on every office door there must put plate number 1 number of last office. plate can contain 1 digit, if office number contains 2 digits there must 2 plates. how many plates total needed office doors.
the text file duom.txt. value of "16". means theres 16 offices.
this managed come with:
# include <iostream> # include <cmath> # include <iomanip> # include <fstream> using namespace std; const char cdfv [] = ("duom.txt"); const char crfv [] = ("rez.txt"); int main() { int n; int m; int o; int z; ifstream fd (cdfv); ofstream fr (crfv); fd>>n; (int i=1;i<10;i++) { m=n+1; (int j=1;j>=10;j++) { o=n+2; } } z=m+o; fr<<z<<" "; } fd.close(); fr.close(); return 0; }
i know not correct, please me this. glad if tried compiling it, because codeblocks refuses work compiler if installed seperately manually. :/ thats problem.
-----
seems solution has been found, helped me.
# include <iostream> # include <cmath> # include <iomanip> # include <fstream> using namespace std; const char cdfv [] = ("duom.txt"); const char crfv [] = ("rez.txt"); int main() { int limit = 10; int = 1; int viso = 0; ifstream fd (cdfv); ofstream fr (crfv); fd>>n; ( int = 1; i<=n ;++i ){ if (i==limit){ limit*=10; up+=1; } viso += up; } fr<<viso<<" "; fd.close(); fr.close(); return 0; }
this should work non text file variant works charm, here: http://ideone.com/xofqwn
simply iterate 1 maximum number of offices n
, add number of required plates total
. 1 9 need 1 plate, 10 99 need 2 , on. implement using limit , step. limit indicates when need more plates. , step indicates how many plate need office.
int limit = 10; int step = 1; int total = 0; ( int = 1; i<=n ;++i ){ if (i==limit){ limit*=10; step+=1; } total += step; } cout << total << endl;
Comments
Post a Comment