開始前的話
今天微軟筆試難度直接把我嚇蒙了··· 之前的騰訊網(wǎng)易筆試這些都還好,算法題能寫出來,微軟的4個算法題,一個沒讀懂,一個不會做;寫完的兩個則是一個通過測試一個沒通過···
算是第一次遇到算法上的重大打擊,我決定開始刷acm。隨手百度到浙大的acm題庫,感覺難度還可以就做了幾個。(別問我為啥不用自己川大的acm題庫,tm我進(jìn)到藍(lán)色星空就來個403,這玩毛···)
進(jìn)入正題,打算寫c++/c和java兩個版本的代碼,遇到oo好做的就用java,遇到操作內(nèi)存啥的就用c/c++
1001
很簡單的第一道題,就當(dāng)是回顧io操作了。
Calculate a + bInput
The input will consist of a series of pairs of integers a and b,separated by a space, one pair of integers per line.Output
For each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in input.Sample Input
1 5
Sample Output
6
Hint
Use + operator
c寫的
#include "stdafx.h"
int main()
{
int a, b;
while (scanf("%d%d",&a,&b)!=EOF)
{
printf("%d",a+b);
}
return 0;
}
這里提一下遇到的兩個問題:
1.visual studio 中編譯 C 語言項(xiàng)目,如果使用了 scanf 函數(shù),編譯時(shí)便會提示如下錯誤:
error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
原因是Visual C++ 2012 使用了更加安全的 run-time library routines 。新的Security CRT functions(就是那些帶有“_s”后綴的函數(shù))。改進(jìn)方法鏈接如下:http://www.cnblogs.com/gb2013/archive/2013/03/05/SecurityEnhancementsInTheCRT.html
2.int的范圍:
測試時(shí)如果數(shù)據(jù)太大會出現(xiàn)負(fù)數(shù)等等,這個在數(shù)字邏輯課上解釋過,超過了int的范圍。至于這個范圍是多少,知乎這里給了一個解釋:
c標(biāo)準(zhǔn)里面只定義了int的最小寬度,所以這個范圍就完全依賴于編譯器的實(shí)現(xiàn)了。
請看c99的 5.2.4.2.1
Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.
minimum value for an object of type int
INT_MIN -32767 // -(2^15 - 1)
作者:Pagefault
鏈接:https://www.zhihu.com/question/19580654/answer/12283278
來源:知乎
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。