8.3常用函数专题总结

· · 个人记录

很(不)温馨提示:这不是提示。

这我乱写的。

常有函数:

右对齐占x个字符宽度:

setw(x)

向下取整函数:

floor();

向上取整函数:

ceil();

开根号函数:

sqrt();

取a,b两者的最大值:

max(a,b);

取a,b两者的最小值:

min(a,b);

保留两位小数:

cout << fixed << setprecision(2) << a;

交换a,b两者的值:

swap(a,b);

四舍五入函数:

round();

取x的绝对值:

abs(x);

🍬终结:

B2066 救援(题目作者:信息学奥赛一本通 题目难度:入门):

  1. ❌原因:没看题。

  2. ❌代码:没写。

  3. 靠差只是·:开根号函数,向上取整函数。

  4. 解题死路:大本营距离每个房顶的距离是\sqrt{a \times a+s \times s},速度是50km/h,要来回,\times 2,每个人上下船的时间1.5 \times d。

  5. ✔️代码:

    #include<bits/stdc++.h>
    using namespace std;
    int a[105][105];
    int main(){
    
     double sum = 0;
     int n;
     cin >> n;
     for (int i = 1;i <= n;i++){
         double a,s;
         int d;
         cin >> a >> s;
         cin >> d;
         sum += sqrt(a*a+s*s)/50*2+1.5*d; 
     }
     cout << (int)ceil(sum);
    
     return 0;
    }

    P8813 [CSP-J 2022] 乘方(题目作者:CCF_NOI 题目难度:入门):

  6. ❌原因:没判断a = 1的情况。

  7. ❌代码:

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    
     long long a,b;
     cin >> a >> b;
     long long sum = a;
     for (long long i = 2;i <= b;i++){
         sum *= a;
         if (sum > 1e9){
             cout << "-1";
             return 0;
         }
     }
     cout << sum;
    
     return 0;
    }
  8. 考察知识点:I don't no.

  9. 解题死路:先判断a是否=1的情况,否则按照for循环来做。

  10. ✔️代码:

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    
     long long a,b,sum=1;
     cin >> a >> b;
     if (a == 1){ //判断a等于1的情况(因为1乘以什么数都等于1)。
         cout << "1"; //是就直接输出1.
         return 0;
     }
     for (long long i = 1;i <= b;i++){
         sum *= a;
         if (sum > 1e9){ //判断sum是否大于1e9。
             cout << "-1"; //大于1e9直接输出-1。
             return 0;
         }
     }
     cout << sum;
    
     return 0;
    }

    B2108 图像模糊处理(题目作者:信息学奥赛一本通 题目难度:入门):

  11. ❌原因:不会四舍五入。

  12. ❌代码:不知道。

  13. 考察知识点:四舍五入函数。

  14. 解题死路:看代码。

  15. ✔️代码:

    #include<bits/stdc++.h>
    using namespace std;
    double a[105][105];
    int main(){
    
     int n,m;
     cin >> n >> m;
     for (int i = 1;i <= n;i++){
         for (int j = 1;j <= m;j++){
             cin >> a[i][j];
         }
     }
     for (int i = 1;i <= n;i++){
         for (int j = 1;j <= m;j++){
             if (i == 1 || i == n || j == 1 || j == m){//判断是否为最外侧。
                 cout << a[i][j] << " "; //是就直接输出。
             }else { //否则。
                 cout << round((a[i-1][j]+a[i+1][j]+a[i][j-1]+a[i][j+1]+a[i][j])/5) << " "; //那上下左右和自己的和➗5,并用四舍五入函数。
             }
    
         }
         cout << endl;
     }
    
     return 0;
    }

    P9581 「MXOI Round 1」宝箱(题目作者:Coffee_zzz 题目难度:入门):

  16. ❌原因:代码错了。

  17. ❌代码:不想放出来。

  18. 考察知识点:最小值函数。

  19. 解题死路:先移到最近的宝箱,在移到另外一个宝箱的位置。

  20. ✔️代码:

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    
     int a,b;
     cin >> a >> b;
     cout << min(abs(a),abs(b))+abs(a-b);
    
     return 0;
    }

    P1090 [NOIP 2004 提高组] 合并果子(题目作者CCF_NOI 题目难度:普及/提高-):

  21. ❌原因:新知识点。

  22. ❌代码:不知道。

  23. 考察知识点:优先队列(堆)。

  24. 解题死路:先取队列的第一项,在弹出,在取出第一项,在弹出,答案+=a+b,再输入a+b。

  25. ✔️代码:

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    
     int n,sum=0,a,b;
     cin >> n;
     priority_queue<int,vector<int>,greater<int>>q;
     while(n--){
         int x;
         cin >> x;
         q.push(x);
     }
     while(q.size() > 1){
         a = q.top();
         q.pop();
         b = q.top();
         q.pop();
         sum += a+b;
         q.push(a+b);
     }
     cout << sum;
    
     return 0;
    }

  26. d

  27. d

  28. d

  29. d

  30. d

  31. d

  32. s

  33. sd

  34. a

  35. b

  36. c

  37. d

  38. e

  39. f

  40. g

  41. h

  42. i

  43. j

  44. k

  45. l

  46. m

  47. n

  48. o

  49. p

  50. q

  51. r

  52. s

  53. t

  54. u

  55. v

  56. w

  57. x

  58. y

  59. z

  60. n

  61. f

  62. d

  63. d

  64. s

  65. g

  66. g

  67. h

  68. gd

  69. s

  70. th

  71. fd

  72. onej

  73. j

  74. h

  75. g

  76. g

  77. y

  78. y

  79. y

  80. y

  81. y

  82. y

  83. y

  84. y

  85. y

  86. y

  87. y

  88. y

  89. y

  90. y

  91. y

  92. y

  93. y

  94. y

  95. y

  96. y

  97. y

  98. y

  99. jj

  100. j

  101. j

  102. j

  103. j

  104. j

  105. jj

  106. j

  107. f

  108. f

  109. c

  110. vg

  111. g

  112. f

  113. e

  114. r

  115. y

  116. v

  117. s

  118. fg

  119. d

  120. f

  121. g

  122. c

  123. d

  124. f

  125. f

    你是个肝帝。