[英]C++ - how to find the length of an integer
我正在嘗試找到一種方法來查找 integer (位數)的長度,然后將其放入 integer 數組中。 該作業還要求在不使用 STL 的類的情況下執行此操作,盡管程序規范確實說我們可以使用“常見的 C 庫”(我會問我的教授我是否可以使用 cmath,因為我假設 log10(num) + 1 是最簡單的方法,但我想知道是否還有其他方法)。
啊,這不必處理負數。 僅非負數。
我正在嘗試創建一個變體“MyInt”class,它可以使用動態數組處理更廣泛的值。 任何提示將不勝感激! 謝謝!
任何基數中整數n
的位數可以通過除法輕松獲得,直到完成:
unsigned int number_of_digits = 0;
do {
++number_of_digits;
n /= base;
} while (n);
不一定是最有效的,但使用 C++ 時最短和最易讀的方法之一:
std::to_string(num).length()
有一個更好的方法來做到這一點
#include<cmath>
...
int size = trunc(log10(num)) + 1
....
適用於 int 和 decimal
如果您可以使用 C 庫,那么一種方法是使用sprintf ,例如
#include <cstdio>
char s[32];
int len = sprintf(s, "%d", i);
“我的意思是整數中的位數,即“123”的長度為 3”
int i = 123;
// the "length" of 0 is 1:
int len = 1;
// and for numbers greater than 0:
if (i > 0) {
// we count how many times it can be divided by 10:
// (how many times we can cut off the last digit until we end up with 0)
for (len = 0; i > 0; len++) {
i = i / 10;
}
}
// and that's our "length":
std::cout << len;
輸出3
最長int
封閉公式(我在這里使用了int
,但適用於任何有符號整數類型):
1 + (int) ceil((8*sizeof(int)-1) * log10(2))
解釋:
sizeof(int) // number bytes in int
8*sizeof(int) // number of binary digits (bits)
8*sizeof(int)-1 // discount one bit for the negatives
(8*sizeof(int)-1) * log10(2) // convert to decimal, because:
// 1 bit == log10(2) decimal digits
(int) ceil((8*sizeof(int)-1) * log10(2)) // round up to whole digits
1 + (int) ceil((8*sizeof(int)-1) * log10(2)) // make room for the minus sign
對於 4 字節的int
類型,結果為 11。4 字節int
與 11 位十進制數字的示例是:“-2147483648”。
如果您想要某個int
值的十進制位數,可以使用以下函數:
unsigned base10_size(int value)
{
if(value == 0) {
return 1u;
}
unsigned ret;
double dval;
if(value > 0) {
ret = 0;
dval = value;
} else {
// Make room for the minus sign, and proceed as if positive.
ret = 1;
dval = -double(value);
}
ret += ceil(log10(dval+1.0));
return ret;
}
我在 x86-64 的 g++ 9.3.0 中針對整個int
范圍測試了這個函數。
作為一個計算機書呆子而不是一個數學書呆子,我會這樣做:
char buffer[64];
int len = sprintf(buffer, "%d", theNum);
int intLength(int i) {
int l=0;
for(;i;i/=10) l++;
return l==0 ? 1 : l;
}
這是一個很小的高效的
這會是一種有效的方法嗎? 轉換為字符串並找到長度屬性?
int num = 123
string strNum = to_string(num); // 123 becomes "123"
int length = strNum.length(); // length = 3
char array[3]; // or whatever you want to do with the length
怎么樣(也適用於 0 和負數):
int digits( int x ) {
return ( (bool) x * (int) log10( abs( x ) ) + 1 );
}
最好的方法是使用日志查找,它始終有效
int len = ceil(log10(num))+1;
查找整數長度和十進制數的代碼:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int len,num;
cin >> num;
len = log10(num) + 1;
cout << len << endl;
return 0;
}
//sample input output
/*45566
5
Process returned 0 (0x0) execution time : 3.292 s
Press any key to continue.
*/
簡單而簡單的功能
int intlen(int i)
{
int j=1;
while(i>10){i=i/10;j++;}
return j;
}
C/C++ 和 STL 中都沒有用於查找整數長度的內置函數,但是可以通過幾種方法找到它
這是一個用於查找整數長度的示例 C++ 代碼,它可以寫在一個函數中以供重用。
#include<iostream>
using namespace std;
int main()
{
long long int n;
cin>>n;
unsigned long int integer_length = 0;
while(n>0)
{
integer_length++;
n = n/10;
}
cout<<integer_length<<endl;
return 0;
}
這是另一種方法,將整數轉換為字符串並找到長度,它用一行來完成:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
long long int n;
cin>>n;
unsigned long int integer_length = 0;
// convert to string
integer_length = to_string(n).length();
cout<<integer_length<<endl;
return 0;
}
注意:一定要包含cstring
頭文件
最簡單的方法是:
#include <string>
int int_length = to_string(42).length();
在 c++ 中不使用任何庫的最簡單方法是
#include <iostream>
using namespace std;
int main()
{
int num, length = 0;
cin >> num;
while(num){
num /= 10;
length++;
}
cout << length;
}
您也可以使用這個 function:
int countlength(int number)
{
static int count = 0;
if (number > 0)
{
count++;
number /= 10;
countlength(number);
}
return count;
}
查找數字長度的最有效代碼......也計算零,注意“n”是要給出的數字。
#include <iostream>
using namespace std;
int main()
{
int n,len= 0;
cin>>n;
while(n!=0)
{
len++;
n=n/10;
}
cout<<len<<endl;
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.