superhbin wrote:
最小公倍數七個 ra...(恕刪)
請問能分出七個區段的意思是 (rand5()+rand5()+rand5()+rand5()+rand5()+rand5()+rand5())/7 嗎?我想我應該誤解你的意思了?那樣出來的結果還是 1~5...
bluesystem wrote:
讓我想到前兩天在跟以...(恕刪)
如果是資工相關畢業的第一題不會實在不應該...
第二題單一迴圈不用遞迴一樣可以解
其實就是把迴圈打平去算換行符號的位置即可
int n=5;
int cn=1; //counter
int break_point = 0;
for(int i=0 ; i<n*n ; i++ )
{
if( i == break_point )
{
printf("\n");
if( cn <= n )
break_point = break_point + cn;
else
break_point = break_point + 2*n - cn;
cn++;
}
printf("*");
}
例 > {1,3,5,7,9,2,4,6,8} 答案會是5
oh, sorting 不會是最佳解!
size = 9, 所以中間數就是幹掉四個最大數後,剩下最大那個
1,3,5,7,9 => 留著
2<9 => 幹掉9 => 剩 1,2,3,5,7
4<7 => 幹掉7 => 剩 1,2,3,4,5
6>5 => 不留
8>5 => 不留
答案 => 5
2, 同樣是亂數 array, 不過裡面的數值都是成對的, 除了特別的一個. 找出特別那個
例 > {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,8,9} 答案會是 7
出來的 algorithm 會是 O很遜
xor binary 運算, 成對的結果都會是 0, result = 0
result = 1^2^3^4^5^6^7^8^9^1^2^3^4^5^6^8^9 = 7
3, 找出字串中最長的重複字
例 > "yzabcdefghijhlabcdefiuabc" 答案會是 "abcdef"
repeat_max = 0
string_max = ""
yzabcdefghij 當比較基礎(因為後面是h , 有在前面出現過)
h => repeat = 1
l => 斷了 => repeat_max=1, string_max = "h"
a => repeat = 1
b => repeat = 2
c => repeat = 3
d => repeat = 4
e => repeat = 5
f => repeat = 6
i => 斷了 => repeat = 1, repeat_max=6, string_max = "abcdef"
u => 斷了
a => 斷了 => repeat = 1
b => repeat = 2
c => repeat = 3
沒了 => repeat < repeat_max => 答案是 string_max = "abcdef"
內文搜尋

X