編程比賽記錄 練習題答案

作者:迷途小書童愛讀書


目錄:


  1. 編程比賽記錄

  2. C++語法

  3. 標準輸入庫

  4. vector

  5. list

  6. 內存拷貝

  7. 代碼檢查

  8. map

  9. dev C++

  10. 算法

  11. 揹包算法

  12. 輸入輸出練習

  13. 計算a+b

  14. 計算N行A+B

  15. 編程語言運用

  16. 財務管理

  17. 平均績點

  18. 軟件版本

  19. 花樣輸出

  20. 構建矩陣

  21. 電子鐘

  22. 字符處理

  23. ASCII碼

  24. 詞組縮寫

  25. 枚舉

  26. 當總統

  27. 布爾矩陣

  28. 模擬題

  29. 計算利息

  30. 電梯升降

  31. 日期處理

  32. 日曆本

  33. 18歲生日

  34. 規律題

  35. 石頭剪刀布

  36. 掛鹽水

  37. 進制運算

  38. 匯文數

  39. 八進制數

  40. 大數模擬

  41. 大大的求和

  42. 大菲波拉契數

  43. 位運算

  44. 尋找獨一無二的數

  45. 尋找最低數

  46. 揹包問題

  47. 拔河

  48. 簡單數學題

  49. 字母概率

  50. 討厭的小數點

  51. 冒泡排序

  52. C語言7.2

  53. 貪心

  54. 搬水果

  55. 合唱隊形




編程比賽記錄

C++語法

標準輸入庫

//頭文件

#include

using namespace std;

//接收輸入整數

//實現輸入兩個整數,空格和回車效果是一樣的

int a,b;

cin >> a >> b

cout << a <<b< span=""></b<>

//break後面需要加;

break;

//輸出保留小數點2位 float

printf("¥%.2f\n",bb);

//輸出保留小數點2位 double

printf("¥%.2lf\n",bb);

//獲取一行 在獲取了一個整數之後,會出現一個空字符串需要過濾掉

#include

string aa;

getline(cin,aa)

//while循環記得直接用輸入的,不然無法退出

//cout 有換行符了

//printf 輸出需要增加一個\n的換行符

//定義數組

int aa[9]

//可以把面向對象的思路引入進去,簡化編程邏輯

//定義類,注意最後有一個;

class aa{

};

//字符大小寫轉換

tolower(a) //小寫

toupper(a) //大寫

//整數轉string

to_string(42)

//char 賦值給string 需要一個結束符號表示結尾

//字符串轉整數

string s = "12"; 

int a = atoi(s.c_str()); //參數需要是const char *

char s ='1';

int a = s -'0'; //速度較快 求單個整數的值

//字符串截取

 auto year = strInput.substr(0, 4);


vector

//vector的查找效率比數組底50%


//vector

#include

vector inPut;

//輸入

inPut.push_back(inData);

//Remove the last element

inPut.pop_back(inData);

//迭代

for (auto item : vecAA) {

    cout << item <<endl;< span=""></endl;<>

}

for (auto item = vecAA.begin(); item != vecAA.end(); item++) {

    cout << *item << endl;

}

//從小到大排序

#include

sort(vecAA.begin(), vecAA.end());

//刪除一個,it是迭代器

vecAA.erase(it);

list

#include

//定義鏈表

list listinput;

//插入數據

listinput.push_back(input);

//刪除數據

listInput.erase(min);

//刪除第一個數據

mininput.pop_front();

//遍歷數據

for (auto item : mininput) {

    cout << " " << item ;

}

//迭代器

list ::iterator min = listInput.begin();

//迭代器遍歷

for (auto item = listInput.begin(); item != listInput.end();item++) {

       if ((*min) > (*item)) {

        min = item;

    }

}

//隨機插入,只需要拿到迭代器就可以

auto item = listInput.begin();

listInput.insert(item, 88);

//排序

listInput.sort();


內存拷貝

#include

//內存拷貝

memcpy(s1, s2,2001);

//計算char[]長度

int isize = strlen(s3);

//內存設置

int dp[102][50010] = { 0 };

memset(dp, 0, sizeof dp);

代碼檢查

1、注意前後使用單純的拼寫

2、注意寫完之後的“;”

3、注意頭文件的拼寫

4、注意重複使用的變量

5、標準輸入輸出的箭頭需要注意方向 cin >> a >> b cout << a << b

6、整數除以整數還是一個整數,如果要得到double 需要先轉換一個爲double

7、檢查變量是否前後一致

8、注意輸入數的範圍,大大數的時候需要用字符串

9、注意相等的=號和==號的檢查防止筆誤

10、字符串使用length()求長度 vector和list用size()求長度

11、注意檢查函數是否都有返回值

12、迭代器在刪除之前,需要把值取出來,刪除之後再取就取不到了

13、當有多個測試數據樣本的時候,需要記得還原初始值


map

//頭文件

#include

//定義對象

map mapCount;

//尋找key

if (mapCount.end() != mapCount.find(inPut))

//插入數據

mapCount[inPut] = 1;

//遍歷

for (auto item : mapCount) {

if (1 == item.second) {

    cout << item.first << endl;

 break;

}

}

dev C++

快捷鍵:

F9:編譯

F5:調試

F10:運行



算法

揹包算法



輸入輸出練習

計算a+b

題目描述

你的任務是計算a+b。這是爲了acm初學者專門設計的題目。你肯定發現還有其他題目跟這道題的標題類似,這些問題也都是專門爲初學者提供的。


輸入

輸入包含一系列的a和b對,通過空格隔開。一對a和b佔一行。


輸出

對於輸入的每對a和b,你需要依次輸出a、b的和。

如對於輸入中的第二對a和b,在輸出中它們的和應該也在第二行。


樣例輸入

1 5

10 20

樣例輸出

6 30

解答:

#include

using namespace std;

int main() {

    int a,b;

    while (cin >> a >> b) {

        cout << a+b << endl;

    }

    return 0;

}


計算N行A+B

題目描述

你的任務是計算a+b。


輸入

第一行是一個整數N,表示後面會有N行a和b,通過空格隔開。


輸出

對於輸入的每對a和b,你需要在相應的行輸出a、b的和。

如第二對a和b,對應的和也輸出在第二行。


樣例輸入

2

1 5

10 20

樣例輸出

6 30

解答:

#include

using namespace std;


int main(){

    int n =0;

    cin >> n;

    for(int i=0;i<n;i++){< span=""></n;i++){<>

        int a,b;

        cin >>a>>b;

        cout << a+b <<endl;< span=""></endl;<>

    }

    return 0;

}

編程語言運用

財務管理

題目描述

小明畢業一年了,並且找到了一份好工作。這一年裏他賺了很多錢,現在他想知道他這一年裏的平均月薪是多少,請你寫一個程序幫他計算。


輸入

輸入包括12行。第i行爲第i個月的實際月薪。(i=1,2,3...)


輸出

輸出小明的平均月薪,保留兩位小數,並且最前面輸出一個¥符號。


樣例輸入

100.00

489.12 

12454.12 

1234.10 

823.05 

109.20 

5.27 

1542.25 

839.18 

83.99 

1295.01 

1.75


樣例輸出

¥1581.42

答案:

#include

using namespace std;


int main(){

    double total=0;

    for(int i=0;i<12;i++){< span="">

        double a =0;

        cin >> a;

        total = total +a;

    }

    double bb = total /12;

    printf("¥%.2f\n",bb);   

    return 0;

}

平均績點

題目描述

每門課的成績分爲A、B、C、D、F五個等級,爲了計算平均績點,規定A、B、C、D、F分別代表4分、3分、2分、1分、0分。


輸入

有多組測試樣例。每組輸入數據佔一行,由一個或多個大寫字母組成,字母之間由空格分隔。


輸出

每組輸出結果佔一行。如果輸入的大寫字母都在集合{A,B,C,D,F}中,則輸出對應的平均績點,結果保留兩位小數。否則,輸出“Unknown”。


樣例輸入

A B C D F

B F F C C A

D C E F


樣例輸出

2.00

1.83

Unknown

答案:

#include

#include

using namespace std;

 

int main() {

    string inData;

    while (getline(cin, inData)) {

        double sum = 0;

        bool isOK = true;

        int totalNum = 0;

        for (int i = 0; i < inData.length(); i = i + 1) {

            if(' ' == inData[i]){

                continue;

            }

            totalNum = totalNum + 1;

            if ('A' == inData[i]) {

                sum = sum + 4;

                continue;

            }

            if ('B' == inData[i]) {

                sum = sum + 3;

                continue;

            }

            if ('C' == inData[i]) {

                sum = sum + 2;

                continue;

            }

            if ('D' == inData[i]) {

                sum = sum + 1;

                continue;

            }

            if ('F' == inData[i]) {

                sum = sum + 0;

                continue;

            }

            isOK = false;

            cout << "Unknown" << endl;

            break;

        }

        if (isOK) {

            printf("%.2lf\n", sum / totalNum);

        }

    }

    return 0;

}


軟件版本

題目描述

相信大家一定有過在網上下載軟件而碰到多個不同版本的情況。一般來說,軟件的版本號由三個部分組成,主版本號(Major Version Number),子版本號(Minor Version Number)和修訂號(Revision_Number)。當軟件進行了重大的修改時,主版本號加一;當軟件在原有基礎上增加部分功能時,主版本號不變,子版本號加一;當軟件僅僅修正了部分bug時,主版本號和子版本號都不變,修正號加一。

在我們比較軟件的兩個版本的新舊時,都是先比較主版本號,當主版本號相同時再比較子版本號,前兩者都相同的情況下再比較修正號。版本號越大的軟件越新。

現在,小明在下載軟件的時候碰到了兩個版本,請你告訴他哪個版本更新一些。


輸入

輸入的第一行有一個整數T,代表有T組測試。接下來有T組測試。

每組測試分兩行,第一行有三個整數代表第一個軟件版本的主版本號,子版本號和修訂號。第二行也有三個整數代表第二個軟件版本的主版本號,子版本號和修訂號。

數據中出現的整數都在[0,1000]範圍之內。


輸出

對於每組測試,如果第一個軟件的版本新點,請輸出First,如果第二個軟件的版本新點,請輸出Second,否則輸出Same。


樣例輸入

3

1 1 0

1 1 1

1 1 1

1 1 0

1 1 1

1 1 1


樣例輸出

Second

First

Same

解答:

#include

using namespace std;


int main(){

    int count=0;

    int v1,v2,v3;

    int first,second;

    cin >> count;

    for(int i=0;i<count;i++){< span=""></count;i++){<>

        cin >> v1 >>v2>>v3;

        first = v1*1001*1001+v2*1001+v3;

       

         cin >> v1 >>v2>>v3;

        second = v1*1001*1001+v2*1001+v3;

       

        if(first > second){

            cout << "First" <<endl;< span=""></endl;<>

            continue;

        }

        if(first == second){

             cout << "Same" <<endl;< span=""></endl;<>

            continue;

        }

        if(first < second){

             cout << "Second" <<endl;< span=""></endl;<>

            continue;

        }

    }

    return 0;

}

花樣輸出

構建矩陣

題目描述

現請你構建一個N*N的矩陣,第i行j列的元素爲i與j的乘積。(i,j均從1開始)


輸入

輸入的第一行爲一個正整數C,表示測試樣例的個數。

然後是C行測試樣例,每行爲一個整數N(1<=n<=9),表示矩陣的行列數。< span="">


輸出

對於每一組輸入,輸出構建的矩陣。


樣例輸入

2

1

4


樣例輸出

1

1 2 3 4

2 4 6 8

3 6 9 12

4 8 12 16

答案:

#include

using namespace std;


int main() {

    int count = 0;

    cin >> count;

    for (int i = 0; i < count; i++) {

        int n = 0;

        cin >> n;

        for (int j = 0; j < n; j++) {

            cout << (j + 1) * (1);

            for (int k = 1; k < n; k++) {

                cout << ' ' << (j + 1) * (k+1);

            }

            cout << endl;

        }

    }

    return 0;

}


電子鐘

答案:

#include

#include

using namespace std;


class eleNum {

public:

    string str1;

    string str2;

    string str3;

};


int main() {

    eleNum total[10];

    total[0].str1 = " _ ";

    total[0].str2 = "| |";

    total[0].str3 = "|_|";


    total[1].str1 = "   ";

    total[1].str2 = "  |";

    total[1].str3 = "  |";


    total[2].str1 = " _ ";

    total[2].str2 = " _|";

    total[2].str3 = "|_ ";


    total[3].str1 = " _ ";

    total[3].str2 = " _|";

    total[3].str3 = " _|";


    total[4].str1 = "   ";

    total[4].str2 = "|_|";

    total[4].str3 = "  |";


    total[5].str1 = " _ ";

    total[5].str2 = "|_ ";

    total[5].str3 = " _|";


    total[6].str1 = " _ ";

    total[6].str2 = "|_ ";

    total[6].str3 = "|_|";


    total[7].str1 = " _ ";

    total[7].str2 = "  |";

    total[7].str3 = "  |";


    total[8].str1 = " _ ";

    total[8].str2 = "|_|";

    total[8].str3 = "|_|";


    total[9].str1 = " _ ";

    total[9].str2 = "|_|";

    total[9].str3 = " _|";


    int a1, a2, a3, a4;

    while (cin >> a1 >> a2 >> a3 >> a4) {

        string str1 = total[a1].str1 + total[a2].str1 + total[a3].str1 + total[a4].str1;

        string str2 = total[a1].str2 + total[a2].str2 + total[a3].str2 + total[a4].str2;

        string str3 = total[a1].str3 + total[a2].str3 + total[a3].str3 + total[a4].str3;


        cout << str1 << endl;

        cout << str2 << endl;

        cout << str3 << endl;

    }

    return 0;

}

字符處理

ASCII碼


答案

#include

#include

using namespace std;


int main() {

    int t = 0;

    cin >> t;

    string temp;

    for (int i = 0; i < t; i++) {

        char ca;

        int ia;

        cin >> ia;

        ca = ia;

        temp = temp + ca;

    }

    cout << temp << endl;

    return 0;

}

詞組縮寫


答案

#include

#include

using namespace std;


int main() {

    int t = 0;

    cin >> t;

    string temp;

    getline(cin, temp);

    for (int i = 0; i < t;i++) {

        string strin;

        string strout;

        getline(cin, strin);

        int getindex = 0;

        for (int j = 0; j < strin.length(); j++) {

            if (' ' == strin[j]) {

                strout = strout + (char)toupper(strin[getindex]);

                getindex = j + 1;

            }

        }

        strout = strout + (char)toupper(strin[getindex]);

        string strout2;

        for (int i = 0; i < strout.length(); i++)

        {

            if (' ' == strout[i]) {

                continue;

            }

            strout2 = strout2 + strout[i];

        }

        cout << strout2 << endl;

    }

    return 0;


}

枚舉

當總統


答案

#include

#include

#include

using namespace std;


int main() {

    int N;

    while (cin >> N) {

        if (0 == N) {

            return 0;

        }

        vector  inPut;

        int inData;

        for (int i = 0; i < N; i++) {

            cin >> inData;

            inPut.push_back(inData);

        }

        sort(inPut.begin(), inPut.end());

        int m = inPut.size() / 2 + 1;

        int totalCount = 0;

        for (int i = 0; i < m; i++) {

            totalCount = totalCount + inPut[i] / 2 + 1;

        }

        cout << totalCount << endl;

    }

    return 0;

}

布爾矩陣

答案:

#include

#include


using namespace std;


class MyRow {

public:

    vector  m_row;

};


bool IsOK(vector & tData, int n) {

    bool isOK = true;

    for (int i = 0; i < n; i++) {

        int sum = 0;

        for (auto item : tData[i].m_row) {

            sum = sum + item;

        }

        if (1 == sum % 2) {

            isOK = false;

            return isOK;

        }

        sum = 0;

        for (int j = 0; j < n; j++) {

            sum = sum + tData[j].m_row[i];

        }

        if (1 == sum % 2) {

            isOK = false;

            return isOK;

        }

    }

    return isOK;


}


int main() {

    int n = 0;

    while (cin >> n) {

        if (0 == n) {

            break;

        }

        vector  tData;

        for (int i = 0; i < n; i++) {

            MyRow  row;

            for (int j = 0; j < n; j++) {

                int inData = 0;

                cin >> inData;

                row.m_row.push_back(inData);

            }

            tData.push_back(row);

        }

        auto isOK = IsOK(tData, n);

        if (true == isOK) {

            cout << "OK" << endl;

            continue;

        }

        bool isOK2 = false;

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < n; j++) {

                if (0 == tData[i].m_row[j]) {

                    tData[i].m_row[j] = 1;

                }

                else {

                    tData[i].m_row[j] = 0;

                }

                isOK2 = IsOK(tData, n);

                if (true == isOK2) {

                    cout << "Change bit (" << i + 1 << "," << j + 1 << ")" << endl;

                    break;

                }

                if (0 == tData[i].m_row[j]) {

                    tData[i].m_row[j] = 1;

                }

                else {

                    tData[i].m_row[j] = 0;

                }

            }

            if (true == isOK2) {

                break;

            }

        }

        if (false == isOK2) {

            cout << "Corrupt" << endl;

        }

    }


    return 0;

}


模擬題

計算利息

答案,使用printf的時候記得加上一個換行符

#include

using namespace std;


int main(){

    int t=0;

    cin >>t;

    for(int i=0;i<t;i++){< span=""></t;i++){<>

        double y,q,e,f,g;

        cin >> y >> q>>e>>f>>g;

       

        double out1 = y*(1+e/100*q/365)*(1+g/100);

        double out2 = y*(1+f/100*(q+365)/365);

       

        printf("%.1f\n",out1);

        printf("%.1f\n",out2);

       

    }

   

    return 0;

}

電梯升降

答案:

#include


using namespace std;


int getTime(int from,int to){

    if(from > to){

      return (from -to)*4 + 5;

    }

    if(from <to){< span=""></to){<>

        return (to-from)*6 + 5;

    }

    return 5;

}


int main(){

    int n=0;

    while(cin >> n){

        if(0 == n){

            break;

        }

        int fromfloor =0;

        int sum =0;

        for(int i=0;i<n;i++){< span=""></n;i++){<>

            int tofloor=0;

            cin >> tofloor;

            sum = sum + getTime(fromfloor,tofloor);

            fromfloor = tofloor;

        }

        cout << sum<<endl;< span=""></endl;<>

    }

   

    return 0;

}


日期處理

日曆本


答案:

每行一共20*3+2= 62個字節 

2010 從62-4 = 58 /2 =29字節開始 或者說前面有29個空格

接下來是一個一行

然後輸出月份 3個一組

月份格式也較爲固定

第一行是月份單詞

第二行是星期

第三行是數字


判斷星期幾:

基姆拉爾森計算公式

W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7在公式中d表示日期中的日數,m表示月份數,y表示年數。

把一月和二月看成是上一年的十三月和十四月

    case 0: printf("星期一\n"); break;

    case 1: printf("星期二\n"); break;

    case 2: printf("星期三\n"); break;

    case 3: printf("星期四\n"); break;

    case 4: printf("星期五\n"); break;

    case 5: printf("星期六\n"); break;

    case 6: printf("星期日\n"); break;

日期的位置 = 20/日期長度/2的位置開始

#include

#include

#include


using namespace std;


int getWeek(int year, int month, int day) {

    if (1 == month || 2 == month) {

        month = month + 12;

        year = year - 1;

    }

    int week = 0;

    week = day + month * 2 + (month + 1) * 3 / 5 + year + year / 4 - year / 100 + year / 400;

    return (week + 1) % 7;

}


class Month {

public:

    string name;

    string weekname;

    int dayNum;


    vector  vecDay;

    int maxByte = 20;


    Month(int year, int month) {

        char cPace[21] = { 0 };

        for (int i = 0; i < 20; i++) {

            cPace[i] = ' ';

        }

        string strSpace = cPace;

        vecDay.push_back(strSpace);

        vecDay.push_back(strSpace);

        vecDay.push_back(strSpace);

        vecDay.push_back(strSpace);

        vecDay.push_back(strSpace);

        vecDay.push_back(strSpace);


        weekname = "Su Mo Tu We Th Fr Sa";

        if (1 == month) {

            name = "January";

            dayNum = 31;

        }

        else if (2 == month) {

            name = "February";

            dayNum = 28;

            if ((0 == year % 4) && (0 != year % 100)) {

                dayNum = 29;

            }

            else if ((0 == year % 100) && (0 == year % 400)) {

                dayNum = 29;

            }

        }

        else if (3 == month) {

            name = "March";

            dayNum = 31;

        }

        else if (4 == month) {

            name = "April";

            dayNum = 30;

        }

        else if (5 == month) {

            name = "May";

            dayNum = 31;

        }

        else if (6 == month) {

            name = "June";

            dayNum = 30;

        }

        else if (7 == month) {

            name = "July";

            dayNum = 31;

        }

        else if (8 == month) {

            name = "August";

            dayNum = 31;

        }

        else if (9 == month) {

            name = "September";

            dayNum = 30;

        }

        else if (10 == month) {

            name = "October";

            dayNum = 31;

        }

        else if (11 == month) {

            name = "November";

            dayNum = 30;

        }

        else if (12 == month) {

            name = "December";

            dayNum = 31;

        }


        string strName = cPace;

        int index = (20 - name.length()) / 2;

        for (int i = 0; i < name.length(); i++) {

            strName[index + i] = name[i];

        }

        name = strName;


        int weekIndex = getWeek(year, month, 1);

        for (int i = 1; i < dayNum + 1; i++) {

            int lineIndex = weekIndex / 7;

            int lineWeek = weekIndex % 7;


            string stri = to_string(i);

            if (1 == stri.length()) {

                vecDay[lineIndex][lineWeek * 3 + 1] = stri[0];

            }

            else {

                vecDay[lineIndex][lineWeek * 3] = stri[0];

                vecDay[lineIndex][lineWeek * 3 + 1] = stri[1];

            }

            weekIndex = weekIndex + 1;

        }


    }



};




int main() {

    int y = 0;

    while (cin >> y) {

        vector  totalMonth;

        for (int i = 1; i < 13; i++) {

            Month m(y, i);

            totalMonth.push_back(m);

        }

        char temp[65] = { 0 };

        for (int i = 0; i < 64; i++) {

            temp[i] = ' ';

        }

        string strYear = to_string(y);

        int index = (64 - strYear.length()) / 2;

        for (int i = 0; i < strYear.length(); i++) {

            temp[index + i] = strYear[i];

        }

        strYear = temp;

        cout << strYear << endl;

        cout << "" << endl;


        for (int i = 0; i < 12; i = i + 3) {

            auto month1 = totalMonth[i];

            auto month2 = totalMonth[i + 1];

            auto month3 = totalMonth[i + 2];


            string str1 = month1.name + "  " + month2.name + "  " + month3.name;

            cout << str1 << endl;


            string str2 = month1.weekname + "  " + month2.weekname + "  " + month3.weekname;

            cout << str2 << endl;


            for (int j = 0; j < 6; j++) {

                string str3 = month1.vecDay[j] + "  " + month2.vecDay[j] + "  " + month3.vecDay[j];

                cout << str3 << endl;

            }

        }

    }

    return 0;

}

18歲生日

規律題

石頭剪刀布


#include


using namespace std;



int winNumber(char cmd1, char cmd2) {

    if (cmd1 == cmd2) {

        return 0;

    }

    if ('R' == cmd1) {

        if ('S' == cmd2) {

            return 1;

        }

        else {

            return -1;

        }

    }

    if ('S' == cmd1) {

        if ('P' == cmd2) {

            return 1;

        }

        else {

            return -1;

        }

    }

    if ('P' == cmd1) {

        if ('R' == cmd2) {

            return 1;

        }

        else {

            return -1;

        }

    }

    return 0;

}


class Player {

public:

    int m_winCount = 0;


};


int main() {

    int t = 0;

    cin >> t;

    for (int i = 0; i < t; i++) {

        int lineCount = 0;

        Player player1, player2;

        cin >> lineCount;

        for (int j = 0; j < lineCount; j++) {

            char P1, P2;

            cin >> P1 >> P2;

            auto myCount = winNumber(P1, P2);

            player1.m_winCount = player1.m_winCount + myCount;

            player2.m_winCount = player2.m_winCount - myCount;

        }

        if (player1.m_winCount == player2.m_winCount) {

            cout << "TIE" << endl;

            continue;

        }

        if (player1.m_winCount > player2.m_winCount) {

            cout << "Player 1" << endl;

            continue;

        }

        if (player1.m_winCount < player2.m_winCount) {

            cout << "Player 2" << endl;

            continue;

        }

    }

    return 0;


}

掛鹽水

#include

using namespace std;


int main() {

    double VUL, D;

    double minZero = 0.00000001;

    while (cin >> VUL >> D) {

        int i = 1;

        int t = 0;

        while (true) {

            auto left1 = VUL - D * i;

            if (left1 > minZero) {

                t = t + i + 1;

                i = i + 1;

                VUL = left1;

                continue;

            }

            int j = 1;

            while (true) {

                auto letf2 = VUL - D * j;

                if (letf2 > minZero) {

                    j = j + 1;

                    continue;

                }

                break;

            }

            t = t + j;

            break;

        }

        cout << t << endl;

    }

    return 0;

}

進制運算

匯文數

#include

#include


using namespace std;


bool isPalindrom(int input,int cmd){

    auto myinput = input;

    vector  vecTemp;

    while(true){

        if(myinput <cmd){< span=""></cmd){<>

            vecTemp.push_back(myinput);

            break;

        }

        auto mode = myinput % cmd ;

        vecTemp.push_back(mode);

       

        myinput = (myinput - mode)/cmd;

    }

    int length = vecTemp.size();

    myinput = 0;

    int param =1;

    for(int i=0;i<length;i++){< span=""></length;i++){<>

        myinput =myinput + vecTemp[length-i-1]*param;

        param = param * cmd;

    }

    if(myinput == input){

        return true;

    }

    return false;

}

int main(){

    int T =0;

    while(cin >> T){

        if(0 == T){

            break;

        }

        vector  jz;

        for(int i=2;i<17;i++){< span="">

           auto isTrue = isPalindrom(T,i);

           if(true == isTrue){

               jz.push_back(i);

           }

        }

        if(0 == jz.size()){

            cout << "Number " <<T<<" is not a palindrom"<<endl;< span=""></endl;<>

            continue;

        }

       

        cout << "Number " <<T<<" is palindrom in basis";

        for(auto item:jz){

            cout<<" "<<item;< span=""></item;<>

        }

        cout << endl;

    } 

    return 0;

}

八進制數

#include

#include


using namespace std;


int main(){

    int input =0;

    while(cin >> input){

        vector  vec;

        while(true){

            if(input <8){< span="">

                vec.push_back(input);

                break;

            }

            auto mode = input%8;

            vec.push_back(mode);

            input = (input -mode)/8;

        }

        int length = vec.size();

        for(int i=0;i<length;i++){< span=""></length;i++){<>

            cout << vec[length-1-i];

        }

        cout <<endl;< span=""></endl;<>

    }

   

   

    return 0;

}

大數模擬

大大的求和

#include

#include

#include


using namespace std;


int main() {

    int N = 0;

    cin >> N;

    for (int i = 0; i < N; i++) {

        vector  vecStr;

        string strInput;

        while (cin >> strInput) {

            if ("0" == strInput) {

                break;

            }

            vecStr.push_back(strInput);

        }

        int ilength = vecStr[0].length();

        int isize = vecStr.size();


        vector  vecInt;

        int ijin = 0;

        for (int j = 0; j < ilength; j++) {

            int iAdd = 0 + ijin;

            for (int k = 0; k < isize; k++) {

                string cItem = vecStr[k].substr(ilength - 1 - j, 1);

                int iItem = atoi(cItem.c_str());

                iAdd = iAdd + iItem;

            }

            ijin = iAdd / 10;

            iAdd = iAdd % 10;

            vecInt.push_back(iAdd);

        }

        if (0 != ijin) {

            vecInt.push_back(ijin);

        }

        if (i != 0) {

            cout << "" << endl;

        }

        auto totalSize = vecInt.size();

        for (int j = 0; j < totalSize; j++) {

            cout << vecInt[totalSize - 1 - j];

        }

        cout << endl;

    }

    return 0;

}

大菲波拉契數

這裏提供兩種解決方案,一個是使用vector的方案,一種是使用數組的方案。在高性能計算的時候,儘量不要使用遞歸,儘量使用循環來替代,儘量使用數組來操作。

#include

#include

#include


using namespace std;



vector vecAdd(vector & vec1, vector & vec2) {

    int length1 = vec1.size();

    int length2 = vec2.size();


    if (length1 > length2) {

        auto zeroCount = length1 - length2;

        while (zeroCount > 0) {

            vec2.push_back(0);

            zeroCount = zeroCount - 1;

        }

    }

    if (length2 > length1) {

        auto zeroCount = length2 - length1;

        while (zeroCount > 0) {

            vec1.push_back(0);

            zeroCount = zeroCount - 1;

        }

    }


    int length = vec1.size();


    vector  vec3;


    int ijin = 0;

    for (int i = 0; i < length; i++) {


        int ia1 = vec1[i];

        int ia2 = vec2[i];


        int sum = ia1 + ia2 + ijin;

        ijin = sum / 10;

        int imode = sum % 10;


        vec3.push_back(imode);

    }

    if (0 != ijin) {

        while (true) {

            int imode = ijin % 10;

            vec3.push_back(imode);

            ijin = (ijin - imode) / 10;

            if(0 == ijin){

                break;

            }

        }

    }

    return vec3;

}



int main() {

    int N = 0;

    while (cin >> N) {

        if(1 == N || 2 == N){

            cout << 1 <<endl;< span=""></endl;<>

            continue;

        }

        vector  s1;

        vector  s2;

       

        s1.push_back(1);

        s2.push_back(1);

       

        for (int i = 3; i < N; i++) {

            auto s3 = vecAdd(s1,s2);

            s1 = s2;

            s2 = s3;

        }

        int isize = s2.size();

        for(int i=0;i<isize;i++){< span=""></isize;i++){<>

            cout << s2[isize -1 -i] ;

        }

        cout << endl;

    }

    return 0;

}

#include

#include

#include

#include


using namespace std;



void vecAdd(char* s1, char* s2, char* s3) {

    int length1 = strlen(s1);

    int length2 = strlen(s2);


    if (length1 > length2) {

        auto zeroCount = length1 - length2;

        s2[length1] = '\0';

        while (zeroCount > 0) {

            s2[length1 - zeroCount] = '0';

            zeroCount = zeroCount - 1;

        }

    }

    if (length2 > length1) {

        auto zeroCount = length2 - length1;

        s1[length2] = '\0';

        while (zeroCount > 0) {

            s1[length2 - zeroCount] = '0';

            zeroCount = zeroCount - 1;

        }

    }


    int length = strlen(s1);


    int ijin = 0;

    for (int i = 0; i < length; i++) {


        int ia1 = s1[i] - '0';

        int ia2 = s2[i] - '0';


        int sum = ia1 + ia2 + ijin;

        ijin = sum / 10;

        int imode = sum % 10;


        s3[i] = imode + '0';

    }

    if (0 == ijin) {

        s3[length] = '\0';

    }

    if (0 != ijin) {

        int i = 0;

        while (true) {

            int imode = ijin % 10;

            s3[length + i] = imode + '0';

            i = i + 1;

            ijin = (ijin - imode) / 10;

            if (0 == ijin) {

                s3[length + i] = '\0';

                break;

            }

        }

    }


    return ;

}



int main() {

    int N = 0;

    char s1[2001], s2[2001], s3[2001];

    while (cin >> N) {

        if (1 == N || 2 == N) {

            cout << 1 << endl;

            continue;

        }

        s1[0] = '1';

        s1[1] = '\0';


        s2[0] = '1';

        s2[1] = '\0';


        for (int i = 3; i <= N; i++) {

            vecAdd(s1, s2, s3);

            memcpy(s1, s2,2001);

            memcpy(s2, s3, 2001);

        }

        int isize = strlen(s3);

        for (int i = 0; i < isize; i++) {

            cout << s3[isize - 1 - i];

        }

        cout << endl;

    }

    return 0;

}

位運算

尋找獨一無二的數


#include

#include


using namespace std;


int main() {

    int n = 0;

    while (cin >> n) {

        if (0 == n) {

            break;

        }

        map<int, </int,int> mapCount;

        for (int i = 0; i < n; i++) {

            int inPut = 0;

            cin >> inPut;

            if (mapCount.end() != mapCount.find(inPut)) {

                auto value = mapCount[inPut];

                mapCount[inPut] = value + 1;

                continue;

            }

            mapCount[inPut] = 1;

        }

        for (auto item : mapCount) {

            if (1 == item.second) {

                cout << item.first << endl;

                break;

            }

        }


    }




    return 0;

}

尋找最低數


#include

#include


using namespace std;


vector get2jinzhi(int input) {

    vector  vec;

    while (true) {

        if (input < 2) {

            vec.push_back(input);

            return vec;

        }

        auto mode = input % 2;

        vec.push_back(mode);

        input = (input - mode) / 2;

    }

    return vec;

}


int main() {

    int a = 0;

    while (cin >> a) {

        if (0 == a) {

            break;

        }

        auto vec = get2jinzhi(a);

        int output = 1;

        for (auto item : vec) {

            if (1 == item) {

                break;

            }

            output = output * 2;

        }

        cout << output << endl;

    }

    return 0;

}

揹包問題

拔河

這一題是一道二維費用的動態規劃問題,我們可以這樣轉化這個問題:

將n個物品放到一個容量爲n/2的揹包,每個物品佔一個空間,這個揹包所能承受的重量是(所有物品總和)/2 ,然後求這個揹包最多能放多重的物品,這樣就成功轉化成一個揹包問題了,狀態轉移方程爲 f[j][k]= f[j][k] || f[j-a[i]][k-1];

這個函數可以這麼理解,定義f[j][k] :爲1表示存在重量爲J的值,由K個物品組成。我們的重量一定不會超過總和的一半,而且由於重量都是整數,所以我們通過遞歸遞減的方式,來找到最終可以到達的重量。f[j-a[i]][k-1] 表示如果減掉一個物體,是否存在k-1個物體可以滿足當前的重量。f[0][0]=1 表示存在0個物體的質量爲0。


#include

#include

using namespace std;

int f[50010][102],a[1000];

int main()

{

    int n;

    while(cin >> n)

    {

        int sum = 0;

        memset(f,0,sizeof(f));

        for(int i=1;i<=n;i++)< span="">

        {

            cin >> a[i];

            sum=sum+a[i];

        }

        f[0][0]=1;

        for(int i=1;i<=n;i++)< span="">

        {

            for(int j=sum/2;j>=a[i];j--)

            {

                for(int k=(n+1)/2;k>=1;k--)

                {

                    f[j][k] = f[j][k] || f[j-a[i]][k-1];

                }

            }

        }

        for(int i=sum/2;i>=0;i--)

            if(f[i][(n+1)/2])

            {

                cout << i << " " << sum-i << endl;

                break;

            }

    }

    return 0;

}


簡單數學題

字母概率

#include

#include


using namespace std;


int main() {

    char first;

    while (cin >> first) {

        string next;

        cin >> next;

        int count = 0;

        for (int i = 0; i < next.length(); i++) {

            if (tolower(first) == tolower(next[i])) {

                count = count + 1;

            }

        }

        double d = (double)count / next.length();

        printf("%.5lf\n", d);

    }




    return 0;

}

討厭的小數點


#include

#include

using namespace std;



int main() {

    int t = 0;

    cin >> t;

    for (int i = 0; i < t; i++) {

        string input;

        int number;


        cin >> input >> number;


        int index;

        for (int j = 0; j < input.length(); j++) {

            if (input[j] == '.') {

                index = j;

            }

        }

        if (index + number >= input.length()) {

            cout << 0 << endl;

            continue;

        }


        cout << input[index + number] << endl;



    }



    return 0;

}

冒泡排序

C語言7.2


#include

#include

using namespace std;



int getMinAndRemove(list & listInput) {

    list ::iterator  min = listInput.begin();

    for (auto item = listInput.begin(); item != listInput.end();item++) {

        if ((*min) > (*item)) {

            min = item;

        }

    }

    listInput.erase(min);

    return *min;

}


int main() {

    int n = 0;

    cin >> n;


    list  listinput;

    for (int i = 0; i < n; i++) {

        int input = 0;

        cin >> input;

        listinput.push_back(input);

    }


    list  mininput;


    while (listinput.size()>0)

    {

      auto min =  getMinAndRemove(listinput);

      mininput.push_back(min);

    }


    auto first = mininput.begin();

    mininput.pop_front();


    cout << *first;


    for (auto item : mininput) {

        cout << " " << item ;

    }

    cout << endl;


    return 0;

}


貪心

搬水果


#include

#include

using namespace std;


bool compute(list & listInput, int& value) {

    if (listInput.size() < 2) {

        return false;

    }

    auto value1 = *(listInput.begin());

    listInput.pop_front();

    auto value2 = *(listInput.begin());

    listInput.pop_front();


    value = value1 + value2;


    bool insertOk = false;

    for (auto item = listInput.begin(); item != listInput.end();item++) {

        if (value < (*item)) {

            listInput.insert(item, value);

            insertOk = true;

            break;

        }

    }


    if (false == insertOk) {

        listInput.push_back(value);

    }


    return true;

}

int main() {

    int n = 0;

    while (cin >> n) {

        if (0 == n) {

            break;

        }

        list  listInput;

        for (int i = 0; i < n; i++) {

            int input = 0;

            cin >> input;

            listInput.push_back(input);

        }

        listInput.sort();


        int sum = 0;

        int value;

        while (true == compute(listInput, value)) {

            sum = sum + value;

        }

        cout << sum << endl;

    }

    return 0;

}

合唱隊形

解題思路:

由於隊列的順序是不變的,而我們不知道中間點Ti到底在哪裏。但是我們一定知道Ti的左邊一定是逐漸升高的,Ti的右邊一定是逐漸降低的。然後我們可以把這個問題變成,求規定i的左邊逐漸升高的最大值 和求規定i的右邊的逐漸降低的最大值。最後在遍歷每個i將兩邊的最大值求出來,那麼這個i就是我們要算出來的值。

接下來,求出逐步變大的最大數量,也需要構造一個狀態函數,假設爲dp[i]中保存的選擇i點後,最大的數量。那麼我們遍歷之前所有的dp點,如果當前點當前點比其大的話,需要更新一下dp[i]。


#include

#include

using namespace std;



int a[101] ={0};

int dp_left[101] = {0};

int dp_right[101] = {0};



int main(int argc, char** argv) {

       int n =0;

       while(cin >> n){

              if(0 == n){

                     break;

              }

              memset(a,0,sizeof(a));

              memset(dp_left,0,sizeof(dp_left));

              memset(dp_right,0,sizeof(dp_right));

             

              for(int i =0;i<n;i++){< span=""></n;i++){<>

                     int input=0;

                     cin >> input;

                     a[i] = input;

              }

             

              for(int i =0;i<n;i++){< span=""></n;i++){<>

                     for(int j = 0;j<i;j++){< span=""></i;j++){<>

                            if(a[i]>a[j]){

                                   if(dp_left[i] < dp_left[j] +1){

                                          dp_left[i]  = dp_left[j] +1;

                                   }

                            }

                     }

              }

             

              for(int i =n-1;i>-1;i--){

                     for(int j = n-1;j> i;j--){

                            if(a[i]>a[j]){

                                   if(dp_right[i] < dp_right[j] +1){

                                          dp_right[i]  = dp_right[j] +1;

                                   }

                            }

                     }

              }

             

              int max =0;

              for(int i =0;i<n;i++){< span=""></n;i++){<>

                     if((dp_left[i] + dp_right[i]) > max)

                     {

                            max = dp_left[i] + dp_right[i];

                     }

              }

             

              cout << n- max-1 << endl;

       }

      

       return 0;

}



本文分享自微信公衆號 - WebHub(myWebHub)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章