[問題] 一個bitset問題
前輩好
目前小弟遇到一個關於bitset的問題
但是執行結果卻跟小弟想的不太一樣
能反請前輩們給予一些指點
題目是:隨機輸入一個由0和1組成的string
將此1 2 3 5 8 13 21位置的bit改為1(如果不是1的話)
以下是小弟的程式碼
#include<iostream>
#include<bitset>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::bitset;
int F( int n );
bool main( int argc , char *argv[] )
{
string x;
int y;
cin >> x;
bitset<32> a(x);
for( int i =1 ; i <= 7 ; ++i )
{
y = F(i);
a.set(y);
}
cout << "a: ";
for( int i =0 ; i != 32 ; ++i )
{
cout << a[i];
}
cout << " ";
return true;
}
int F( int n )
{
if( n==0 )
return false;
else if( n==1 )
return 1;
else if( n==2 )
return 2;
else
return ( F( n-1 ) + F( n-2 ) );
}
問題來了....例如我輸入32個0
跑出來的卻是 0111010010000100000001000....
但是我本想要的是 .......001000000010000100101110
字串整個顛倒
可是不是 string的編號跟bitset的編號相反
一個是由左至右 一個是由右至左
這樣我用for迴圈cout bitset<32> a時...怎麼還是由左至右的?
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.20.178.6
→
11/12 12:18, , 1F
11/12 12:18, 1F
→
11/12 12:25, , 2F
11/12 12:25, 2F
→
11/12 12:26, , 3F
11/12 12:26, 3F
→
11/12 12:27, , 4F
11/12 12:27, 4F
→
11/12 12:29, , 5F
11/12 12:29, 5F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章