牛客網刷題 華為機試 C 實現

2021-10-20 19:31:02 字數 3041 閱讀 8263

使用標記陣列的形式來解決,重點是每個n的輸出是單獨的,並不是等待所有輸入完成後才統一進行輸出。

如輸入3 2 2 1 5 6 1 2 5 3,n有2個,分別為n1=3(2 2 1)和 n2=5(6 1 2 5 3 )

所以輸出應該分為兩部分,1 2 和 1 2 3 5 6

最開始我以為是統一輸出,ra了好幾次

#include

using

namespace std;

intmain()

;for

(int i=

0; i)for

(int i=

0; i<

1001

; i++)if

(set[i]

) cout << set[i]

<< endl;

}}

char c = '0';

int t = int( c ); // t!=0,而是0的acsii碼48

cout << t; // t == 48

#include

#include

#include

using

namespace std;

intmain()

cout << result<}}

判斷小數點後第一位是否大於5

int(float x) 會把x小數點後的所有數字直接捨去

#include

#include

using

namespace std;

intmain()

else

return0;

}

求二進位制數中1的個數

#include

using

namespace std;

intmain()

cout << round;

return0;

}

這個沒有寫出來,是學習別人的,鏈結如下

#include

#include

#include

using

namespace std;

bool

include_3char

(const string &s)

// 判斷是否包含三種以上字元

;for

(decltype

(s.size()

) i =

0; i < s.

size()

;++i)

else

// 字元在32 ~126 之間

}int count =0;

for(

int j =

0; j <

4; j++)if

(count >=3)

return

true

;else

return

false;}

intmain()

for(

decltype

(vecs.

size()

) i =

0; i < vecs.

size()

;++i)

else

// 長度超過8位

else

// 包含三種以上字元}if

(able ==1)

break

;// 如果有重複子串,就跳出兩層迴圈}if

(able ==1)

cout <<

"ng"

<< endl;

else

cout <<

"ok"

<< endl;}}

}return0;

}

還剩2個空瓶子的時候記得借一瓶(商店老闆暴怒qwq)

#include

#include

using

namespace std;

intmain()

for(vector<

int>

::iterator iter=vec_i.

begin()

; iter!=vec_i.

end(

); iter++)if

(n ==2)

count++

;

cout << count << endl;

}}

推導公式

大數學家尤拉曾推算出完全數的獲得公式:如果p是質數,且2^ p-1也是質數,那麼(2 z^ p-1 )* 2^(p-1)便是乙個完全數。

例如p=2,是乙個質數,2^ p-1=3 也是質數,(2^ p-1)* 2^(p-1)=3x2=6,是完全數。

例如p=3,是乙個質數,2 ^ p-1=7也是質數,(2 ^ p-1)* 2^(p-1)=7x4=28,是完全數。

例如p=5,是乙個質數,2 ^ p-1=31也是質數,(2 ^ p-1)* 2^(p-1)=31x16=496是完全數。

當2^p-1是質數的時候,稱其為梅森素數。到2023年2月6日為止,人類只發現了48個梅森素數,較小的有3、7、31、127等

//c++

#include

#include

using

namespace std;

bool

is_prime

(int p)

;// 如果p是質數,且2^p-1也是質數,那麼(2^p-1)x2^(p-1)便是乙個完全數

intmain()

} cout << count << endl;

}return0;

}bool

is_prime

(int p)

牛客網 華為機試 020 牛客網

密碼要求 1.長度超過8位 2.包括大小寫字母.數字.其它符號,以上四種至少三種 3.不能有相同長度超2的子串重複 說明 長度超過2的子串 一組或多組長度超過2的子符串。每組佔一行 如果符合要求輸出 ok,否則輸出ng 示例1 021abc9000 021abc9abc1 021abc9000 02...

牛客網 華為機試 009

輸入乙個int型整數,按照從右向左的閱讀順序,返回乙個不含重複數字的新的整數。輸入描述 輸入乙個int型整數 輸出描述 按照從右向左的閱讀順序,返回乙個不含重複數字的新的整數 示例1 9876673 37689 思路 維護乙個陣列或者vector長度為10,下標0 9代表取得數字,值代表是否已經輸出...

牛客網 華為機試 015

輸入乙個int型的正整數,計算出該int型資料在記憶體中儲存時1的個數。輸入乙個整數 int型別 這個數轉換成2進製後,輸出1的個數 示例1 5 2 思路一 利用十進位制轉二進位制的方法,統計1的個數。事實證明這種方法是大錯特錯了,因為效率不高且沒有考慮負數的情況,負數補碼,完全不能這麼計算 inc...