[問題] ltoa的使用

看板C_and_CPP作者 (丁丁)時間14年前 (2009/11/18 23:53), 編輯推噓1(105)
留言6則, 5人參與, 最新討論串1/1
遇到的問題: (題意請描述清楚) 在寫ACM 10018,自己run可以,但上傳後顯示Compilation error 自己用g++要編譯,也顯示: error: 'ltoa' was not declared in this scope 程式跑出來的錯誤結果: error: 'ltoa' was not declared in this scope 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) 用Code::Blocks可以編譯,但g++和acm 都不給過 有問題的code: (請善用置底文標色功能) #include <iostream> #include<cstdlib> #include<cstring> using namespace std; int main() { int j,n,reverse,count,add; char str[999],tmp[999],sadd[999]; bool ispalindrome=false; scanf("%d",&n); for(int i=0;i<n;i++) { ispalindrome=false; count=0; scanf("%s",str); while(!ispalindrome) { for(j=strlen(str)-1;j>=0;j--) { tmp[strlen(str)-j-1]=str[j]; } tmp[strlen(str)-j-1]='\0'; reverse=atoi(tmp); if(reverse==atoi(str)) { ispalindrome=true; break; } add=reverse+atoi(str); count++; ltoa(add,sadd,10); strncpy(str,sadd,sizeof(sadd)); } printf("%d %s\n",count,str); } return 0; } 補充說明: 去cppreference好像只有atoi,沒有ltoa 那請問現在比較好的"整數轉字串"是什麼方法呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.230.240

11/18 23:55, , 1F
sprintf?
11/18 23:55, 1F

11/18 23:56, , 2F
不是itoa嗎?
11/18 23:56, 2F

11/19 00:01, , 3F
This function is not defined in ANSI-C
11/19 00:01, 3F

11/19 00:01, , 4F
and is not part of C++, but is supported by some compile
11/19 00:01, 4F

11/19 11:16, , 5F
itoa()的formal way 當然是sprintf()啊
11/19 11:16, 5F

11/20 09:55, , 6F
謝謝 原來這不是ansi-C的函數 來用看看sprintf
11/20 09:55, 6F
文章代碼(AID): #1B11XbYI (C_and_CPP)