Jestem na etapie rozwiązywania zadań na SPOJu, któryś wieczór z kolei siedzę nad problemem konwertowania liczby na tekst (http://pl.spoj.pl/problems/JLITOSL/), dzisiaj - po długich godzinach ślęczenia nad kodem, moje zabawy przyniosły efekt - program działa, przynajmniej wg. mojego kompilatora oraz ideone (http://ideone.com/dgXU6).
Jednak podczas zgłaszania rozwiązania na SPOJ pojawia się problem:
prog.cpp:146:2: warning: no newline at end of file
prog.cpp: In function 'int main()':
prog.cpp:85: error: call of overloaded 'pow(int, int&)' is ambiguous
/usr/include/bits/mathcalls.h:154: note: candidates are: double pow(double, double)
/usr/lib/gcc/i486-linux/4.0.1/../../../../include/c++/4.0.1/cmath:360: note: long double std::pow(long double, int)
/usr/lib/gcc/i486-linux/4.0.1/../../../../include/c++/4.0.1/cmath:356: note: float std::pow(float, int)
/usr/lib/gcc/i486-linux/4.0.1/../../../../include/c++/4.0.1/cmath:352: note: double std::pow(double, int)
/usr/lib/gcc/i486-linux/4.0.1/../../../../include/c++/4.0.1/cmath:348: note: long double std::pow(long double, long double)
/usr/lib/gcc/i486-linux/4.0.1/../../../../include/c++/4.0.1/cmath:344: note: float std::pow(float, float)
Kod programu wygląda tak:
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef int KeyType;
typedef pair<const KeyType, string> Pair;
typedef multimap<KeyType, string> MapCode;
int main()
{
// obsługa liczebników za pomoca multimap
MapCode dane;
MapCode wyjatki;
MapCode liczebniki;
dane.insert(Pair(1,"jeden"));
dane.insert(Pair(2,"dwa"));
dane.insert(Pair(3,"trzy"));
dane.insert(Pair(4,"cztery"));
dane.insert(Pair(5,"piec"));
dane.insert(Pair(6,"szesc"));
dane.insert(Pair(7,"siedem"));
dane.insert(Pair(8,"osiem"));
dane.insert(Pair(9,"dziewiec"));
dane.insert(Pair(20,"dwadziescia"));
dane.insert(Pair(30,"trzydziesci"));
dane.insert(Pair(40,"czterdziesci"));
dane.insert(Pair(50,"piecdziesiat"));
dane.insert(Pair(60,"szescdziesiat"));
dane.insert(Pair(70,"siedemdziesiat"));
dane.insert(Pair(80,"osiemdziesiat"));
dane.insert(Pair(90,"dziewiecdziesiat"));
dane.insert(Pair(100,"sto"));
dane.insert(Pair(200,"dwiescie"));
dane.insert(Pair(300,"trzysta"));
dane.insert(Pair(400,"czterysta"));
dane.insert(Pair(500,"piecset"));
dane.insert(Pair(600,"szescset"));
dane.insert(Pair(700,"siedemset"));
dane.insert(Pair(800,"osiemset"));
dane.insert(Pair(900,"dziewiecset"));
wyjatki.insert(Pair(10,"dziesiec"));
wyjatki.insert(Pair(11,"jedenascie"));
wyjatki.insert(Pair(12,"dwanascie"));
wyjatki.insert(Pair(13,"trzynascie"));
wyjatki.insert(Pair(14,"czternascie"));
wyjatki.insert(Pair(15,"pietnascie"));
wyjatki.insert(Pair(16,"szesnascie"));
wyjatki.insert(Pair(17,"siedemnascie"));
wyjatki.insert(Pair(18,"osiemnascie"));
wyjatki.insert(Pair(19,"dziewietnascie"));
liczebniki.insert(Pair(3,"tys."));
liczebniki.insert(Pair(6,"mln."));
liczebniki.insert(Pair(9,"mld."));
liczebniki.insert(Pair(12,"bln."));
// deklaracja vectora przechowywujacego liczbe do konwersji na slownie
vector <long int> liczba;
long int x,minus,test,pom=10;
int j,i=0,t;
cin>>t;
for (;t>0;t--) {
cin>>x;
//przerzucenie liczby do wektora
if (x>0) {
do {
minus=x%pom;
x=x-minus;
minus=minus/pow(10,i);
liczba.push_back(minus);
i++;
pom=10*pom;
} while(x);
} else return 0;
//rozpoznanie długości rozmiaru liczby
i=liczba.size()-1;
//pętla konwertująca liczbę na tekst z obsługą nazw jednostek (bln,mln,tys)
do {
if (i!=0) j=i%3;
else j=0;
switch (j)
{
case 2:
cout<<dane.find(liczba.at(i)*100)->second<<" ";
break;
case 1:
test=(liczba.at(i-1))+(liczba.at(i)*10);
if (test>=10 && test<20) {
cout<<wyjatki.find(test)->second<<" ";
j=0;
i--;
if (i>1){
cout<<liczebniki.find(i)->second<<" ";}
break;
}
else cout<<dane.find(liczba.at(i)*10)->second<<" ";
break;
case 0:
if (i>0) {
cout<<dane.find(liczba.at(i))->second<<" ";
cout<<liczebniki.find(i)->second<<" ";
}
else cout<<dane.find(liczba.at(i))->second;
break;
default: cout<<"test";
break;
}
j--;
i--;
} while (i>=0);
cout<<endl;
liczba.erase(liczba.begin(),liczba.end());
i=0;
j=0;
pom=10;
}
return 0;
}
ps. będę wdzięczny za pomoc w rozwiązaniu problemu, a także za wszystkie uwagi odnośnie sposobu rozwiązania zadania (czy dobre funkcje zastosowane, czy dobrze zostały użyte?). Dzięki.