我正在撰寫一個簡單的 C 程式,它應該將整數陣列的所有元素組合成一個數字。例如。{4,5,6} --> 應該是 456。但是我的輸出比原始數字少一。即不是 456,而是 455。有時我的程式運行良好,有時不運行。有人可以向我解釋是什么導致了這種不可預測的行為嗎?謝謝!!
請看一下我的代碼:
#include <bits/stdc .h>
#include <cmath>
using namespace std;
int main()
{
int A[5] = {4,5,6,7,8};
int lengthA = 5;
int num = 0;
for(int x = 0; x < lengthA; x )
{
num = A[x]*pow(10,lengthA-1-x) num;
}
printf("%d\n", num ); // My O/P is 45677
}
uj5u.com熱心網友回復:
正如 Bob__ 所提到的,pow是一個用于雙精度和其他浮點型別的函式。對于這個特定的演算法,我們可以這樣做:
int A[5] = {4,5,6,7,8};
int lengthA = 5;
int num = 0;
for(int x = 0; x < lengthA; x )
{
num = num*10 A[x];
}
在每一步,這會將前一個數字乘以 10,并使該位置的數字正確。例如
Step 1: num = 0*10 4 == 4
Step 2: num = 4 * 10 5 == 40 5 == 45
Step 3: num = 45 * 10 6 == 450 6 == 456
Step 4: num = 456 * 10 7 == 4560 7 == 4567
Step 5: num == 4567 * 10 8 == 45670 8 == 45678
uj5u.com熱心網友回復:
從這個簡單的問題中,您已經可以學到很多東西來改進您的 C 代碼。例子 :
// #include <bits/stdc .h> // NO : https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h
// using namespace std // NO : https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
#include <iostream> // include only what you need for std::cout
int main()
{
int values[]{ 4,5,6,7,8 }; // no need for an =
int num{ 0 };
// prefer range based for loops
// they will not run out of bounds
// https://en.cppreference.com/w/cpp/language/range-for
for (const int value : values)
{
num *= 10;
num = value;
}
// avoid printf, use std::cout with C 20 std::format for formatting
// https://stackoverflow.com/questions/64042652/is-printf-unsafe-to-use-in-c
// https://en.cppreference.com/w/cpp/utility/format/format
std::cout << "num = " << num << "\n";
return 0;
}
uj5u.com熱心網友回復:
這是解決此問題的另一種方法。您可以string根據需要使用此數字進行轉換。
通過這個回圈,我們將每個數字轉換為string并將其粘貼到 num 的末尾string。最后,您將獲得所需的編號為string。如果您需要該數字作為integer,您可以在回圈結束時將其轉換回。為了CONVERstring到int你可以檢查此:轉換字串到數字
#include <iostream> //include to use cout
#include <string> // include to use string
using namespace std;
int main() {
int A[5] = {4,5,6,7,8}; // input array
int lengthA = sizeof(A) / sizeof(A[0]); // size of array
std::string num = "";
for(int i=0; i<lengthA; i ){
num = std::to_string(A[i]);
}
std::cout << "Number : " << num;
}
uj5u.com熱心網友回復:
除了jh316的解決方案;
#include <iostream>
using namespace std;
int A[] = {4,5,6,7,8};
int num = 0;
int main()
{
for(int i: A){
num = num * 10 i;
}
cout << num;
}
代碼說明:
Initial state of the variable: num = 0
For each iteration the num variable is:
1. num = 0 * 10 4 = 4
2. num = 4 * 10 5 = 45
3. num = 45 * 10 6 = 456
4. num = 456 * 10 7 = 4567
5. num = 4567 * 10 8 = 45678
uj5u.com熱心網友回復:
在這里,當你打電話pow;
pow(10,lengthA-1-x)
您的代碼可能正在呼叫以下多載std::pow:
double pow ( double base, int iexp );
可以看出,它回傳一個可能有舍入錯誤的浮點值。我在我的系統上運行了你的代碼,結果是正確的。但是,您的代碼可能會在不同平臺上生成不同的結果。在您的系統中似乎就是這種情況。
相反,您可以這樣做:
#include <cstdio>
#include <array>
#include <span>
constexpr int convertDigitsToNumber( const std::span<const int> digits )
{
int resultNum { };
for ( const auto digit : digits )
{
resultNum = resultNum * 10 digit;
}
return resultNum;
}
int main( )
{
constexpr std::size_t arraySize { 5 };
// use std::array instead of raw arrays
constexpr std::array<int, arraySize> arrayOfDigits { 4, 5, 6, 7, 8 };
constexpr int num { convertDigitsToNumber( arrayOfDigits ) };
std::printf( "%d\n", num );
return 0;
}
作為使用constexpr關鍵字的結果,上面的函式將在編譯時進行評估(只要有可能,上面的代碼就是這種情況)。
關于constexpr:盡可能使用const和constexpr關鍵字的注意事項。這是一個非常好的做法。在此處閱讀constexpr (C )。
注意:如果您不熟悉,std::span請在此處查看。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/401016.html
上一篇:回圈嵌套問題,未正確參考專案
下一篇:for回圈的時間復雜度
