当前位置:   article > 正文

c++ noj记录[已更新至40题]_noj习题风寒指数

noj习题风寒指数

用于记录完成noj时遇到的问题和对自己来说需要注意的事项,可供参考,不建议抄袭。 

1~10

 Hello World

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout << "Hello World";
  6. }

A+B

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a, b;
  6. cin >> a >> b;
  7. cout << a + b;
  8. }

数据类型大小及范围

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<limits.h>
  4. using namespace std;
  5. int main()
  6. {
  7. int d;
  8. cin >> d;
  9. switch (d)
  10. {
  11. case 1:
  12. cout << sizeof(char) << ',' << CHAR_MIN << ',' << CHAR_MAX;
  13. break;
  14. case 2:
  15. cout << sizeof( unsigned char) << ',' << 0 << ',' << UCHAR_MAX;
  16. break;
  17. case 3:
  18. cout << sizeof(short) << ',' << SHRT_MIN << ',' << SHRT_MAX;
  19. break;
  20. case 4:
  21. cout << sizeof(unsigned short) << ',' << 0 << ',' << USHRT_MAX;
  22. break;
  23. case 5:
  24. cout << sizeof(int) << ',' << INT_MIN << ',' << INT_MAX;
  25. break;
  26. case 6:
  27. cout << sizeof(unsigned int) << ',' << 0 << ',' << UINT_MAX;
  28. break;
  29. case 7:
  30. cout << sizeof(long) << ',' << LONG_MIN << ',' << LONG_MAX;
  31. break;
  32. case 8:
  33. cout << sizeof(unsigned long) << ',' << 0 << ',' << ULONG_MAX;
  34. break;
  35. case 9:
  36. cout << sizeof(long long) << ',' << LLONG_MIN << ',' << LLONG_MAX;
  37. break;
  38. case 10:
  39. cout << sizeof(unsigned long long) << ',' << 0 << ',' << ULLONG_MAX;
  40. break;
  41. default:;
  42. }
  43. }

 使用XXX_MAX/MIN要记得用<limits.h>库

平均值

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a, b;
  6. cin >> a >> b;
  7. cout << a + ((b - a) >>1);
  8. }

注意不能直接使用(a+b)/2,会造成数据溢出

进制转换

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a;
  6. cin >> a;
  7. cout << uppercase << hex << a << "," << oct << a;
  8. }

浮点数输出

  1. #include<iostream>
  2. #include<stdio.h>
  3. using namespace std;
  4. int main()
  5. {
  6. double a;
  7. cin >> a;
  8. printf("%.6f,%.2f,%.8f", a, a, a);
  9. }

动态宽度输出

  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. int main()
  5. {
  6. int a, b;
  7. cin >> a >> b;
  8. cout << setw(b)<<setfill('0') << a;
  9. }

引用<iomanip>库后,可以使用setw(int n)设置域宽为n,使用setfill(char c)填充字符

计算地球上两点之间的距离

  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4. double lon1, lat1, lon2, lat2;
  5. const double r = 6371.0, pi = 3.1415926535;
  6. double hav(double x)
  7. {
  8. return (1 - cos(x)) / 2;
  9. }
  10. double rad(double *x)
  11. {
  12. return *x = *x * pi / 180;
  13. }
  14. int main()
  15. {
  16. double h,d;
  17. cin >> lat1 >> lon1 >>lat2 >> lon2;
  18. rad(&lat1); rad(&lon1); rad(&lat2); rad(&lon2);
  19. h = hav(lat2 - lat1) + cos(lat1) * cos(lat2) * hav(lon2 - lon1);
  20. d = acos(1 - (2 * h)) * r;
  21. printf("%.4fkm", d);
  22. return 0;
  23. }

注意看题别把经纬度搞错了,变量和意义要对应上。

风寒指数

  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. int main()
  5. {
  6. double v, t, c,v1;
  7. cin >> v >> t;
  8. v1 = pow(v, 0.16);
  9. c = 13.12 + 0.6215 * t - (11.37 * v1) + (0.3965 * t * v1);
  10. printf("%.0f", c);
  11. }

 引用<cmath>后可以使用pow(x,y)函数进行指数运算,结果为x的y次方

格式输出可以把浮点型转换为整型输出并四舍五入

颜色模型转换

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<stdlib.h>
  4. int max(int R, int G, int B)
  5. {
  6. if (R >= G && R >= B) return R;
  7. if (G >= R && G >= B) return G;
  8. if (B >= R && B >= G) return B;
  9. }
  10. int min(int R, int G, int B)
  11. {
  12. if (R <= G && R <= B) return R;
  13. if (G <= R && G <= B) return G;
  14. if (B <= R && B <= G) return B;
  15. }
  16. int main()
  17. {
  18. int R, G, B;
  19. double V, S, H, n;
  20. scanf("%d%d%d", &R, &G, &B);
  21. V = max(R, G, B);
  22. n = min(R, G, B);
  23. S = (V - n) / V;
  24. if (V == R)
  25. H = 60 * (G - B) / (V - n);
  26. if (V == G)
  27. H = 120 + 60 * (B - R) / (V - n);
  28. if (V == B)
  29. H = 240 + 60*(R - G) / (V - n);
  30. if (V == 0)
  31. S=0;
  32. if (H < 0)
  33. H = H + 360;
  34. printf("%.4lf,%.4lf%%,%.4lf%% ", H, S * 100, V * 100 / 255);
  35. return 0;
  36. }

评论区提供的 

11~20

分数的加、减、乘、除法

  1. #include<stdio.h>
  2. using namespace std;
  3. int a_up, a_down, b_up, b_down;
  4. int c_up, c_down;
  5. //分数化简
  6. void simplify(int* up, int* down)
  7. {
  8. //化简
  9. int i;
  10. if (*up > 0)
  11. {
  12. for (i = *up; i >= 1; i--)
  13. {
  14. if ((*up % i == 0) && (*down % i == 0))
  15. {
  16. *up = *up / i;
  17. *down = *down / i;
  18. }
  19. }
  20. }
  21. if (*up < 0)
  22. {
  23. for (i = *up; i <=- 1; i++)
  24. {
  25. if ((*up % i == 0) && (*down % i == 0))
  26. {
  27. *up = *up / i;
  28. *down = *down / i;
  29. }
  30. }
  31. }
  32. //使分母恒正,分子决定分数负
  33. if (*down < 0)
  34. {
  35. *up = -*up;
  36. *down = -*down;
  37. }
  38. }
  39. //输出结果
  40. void output(char c)
  41. {
  42. if (c_down == 0)
  43. printf("error\n");
  44. else if (c_down==1)
  45. printf("(%d/%d)%c(%d/%d)=%d\n", a_up, a_down, c, b_up, b_down, c_up);
  46. else if(c_up==0)
  47. printf("(%d/%d)%c(%d/%d)=0\n", a_up, a_down, c, b_up, b_down);
  48. else
  49. printf("(%d/%d)%c(%d/%d)=%d/%d\n", a_up, a_down, c, b_up, b_down, c_up, c_down);
  50. }
  51. int main()
  52. {
  53. //输入分数并化简
  54. scanf("%d/%d", &a_up, &a_down);
  55. scanf("%d/%d", &b_up, &b_down);
  56. simplify(&a_up, &a_down);
  57. simplify(&b_up, &b_down);
  58. //加法运算并化简输出
  59. c_up = a_up * b_down + a_down * b_up;
  60. c_down = a_down * b_down;
  61. simplify(&c_up, &c_down);
  62. output('+');
  63. //减法运算并化简输出
  64. c_up = a_up * b_down - a_down * b_up;
  65. c_down = a_down * b_down;
  66. simplify(&c_up, &c_down);
  67. output('-');
  68. //乘法运算并化简输出
  69. c_up = a_up * b_up;
  70. c_down = a_down * b_down;
  71. simplify(&c_up, &c_down);
  72. output('*');
  73. //除法运算并化简输出
  74. c_up = a_up * b_down;
  75. c_down = a_down * b_up;
  76. simplify(&c_up, &c_down);
  77. output('/');
  78. }

注意审题,输入的数字再输出时要带括号,计算结果不带

注意考虑结果中的整数和等于0的情况

通过for循环时注意正负数寻找最大公因数方式不同,可以分情况或用绝对值解决

定义新函数可以使代码简洁一点

倍数和

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int line,i,t,sum;
  6. int n[100000];
  7. cin >> line;
  8. for (i = 1; i <= line; i++)cin >> n[i];
  9. for (i = 1; i <= line; i++)
  10. {
  11. sum = 0;
  12. for (t = 3; t <n[i]; t=t+3)
  13. sum = sum + t;
  14. for (t = 5; t < n[i]; t =t+5)
  15. sum = sum + t;
  16. for (t = 15; t < n[i]; t=t+15)
  17. sum = sum - t;
  18. cout << sum << endl;
  19. }
  20. return 0;
  21. }

动态数组没弄明白,数组直接上取值范围个吧 

幂数模

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. long long a, b, m;
  6. long long ans = 1;
  7. cin >> a >> b >> m;
  8. while (b)
  9. {
  10. if (b & 1ull)ans = ans* a % m;
  11. b >>= 1ull;
  12. a = a * a % m;
  13. }
  14. cout << ans;
  15. }

递推快速幂

级数和

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n, i;
  6. int integer = 1, dec = 2,t=10;
  7. double result=0;
  8. cin >> n;
  9. for (i = 1; i <= n; i++)
  10. {
  11. if (i == 9)
  12. t *= 10;
  13. if (i == 99)
  14. t *= 10;
  15. result = result+ (double)integer + (double)dec / t;
  16. switch (dec)
  17. {
  18. case 10:
  19. cout << integer << '.' << dec / 10;
  20. break;
  21. case 100:
  22. cout << integer << '.' << dec / 100;
  23. break;
  24. default:
  25. cout << integer << '.' << dec;
  26. }
  27. if(i<n)
  28. cout << '+';
  29. else
  30. cout << '=' << result;
  31. integer++;
  32. dec++;
  33. }
  34. return 0;
  35. }

小数点后无意义的0记得去掉 

组合数

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a, b, c, d, n, t=0;
  6. cin >> n;
  7. for (a=0;a<=9;a++)
  8. {
  9. for (b = 0; b<=9; b++)
  10. {
  11. for (c = 0; c<=9; c++)
  12. {
  13. for (d = 0; d<=9; d++)
  14. {
  15. if ((a + b + c + d) == n)
  16. t++;
  17. }
  18. }
  19. }
  20. }
  21. cout << t;
  22. }

 穷举即可

方阵

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int row, col, i,n;
  6. cin >> n;
  7. for (row = 1; row <= n; row++)
  8. {
  9. i = row - 1;
  10. for (col = 1; col<= n; col++)
  11. {
  12. cout << i;
  13. if (col == n)
  14. cout << endl;
  15. else
  16. cout << ' ';
  17. if ((row - col) > 0)
  18. i--;
  19. if ((row - col) <= 0)
  20. i++;
  21. }
  22. }
  23. }

乘数模

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. long long a, b, m, result;
  6. cin >> a >> b >> m;
  7. result = ((a % m) * (b % m)) % m;
  8. cout << result;
  9. }

直接运算会导致数据溢出,可以根据(a*b)%m=(a%m)*(b%m)%m运算

比率

  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. #define PRECISION 0.000001
  5. void simplify(int* up, int* down)//化简分数
  6. {
  7. //化简
  8. int i;
  9. if (*up > 0)
  10. {
  11. for (i = *up; i >= 1; i--)
  12. {
  13. if ((*up % i == 0) && (*down % i == 0))
  14. {
  15. *up = *up / i;
  16. *down = *down / i;
  17. }
  18. }
  19. }
  20. if (*up < 0)
  21. {
  22. for (i = *up; i <= -1; i++)
  23. {
  24. if ((*up % i == 0) && (*down % i == 0))
  25. {
  26. *up = *up / i;
  27. *down = *down / i;
  28. }
  29. }
  30. }
  31. //使分母恒正,分子决定分数负
  32. if (*down < 0)
  33. {
  34. *up = -*up;
  35. *down = -*down;
  36. }
  37. }
  38. int main()
  39. {
  40. double n;
  41. int up, down;
  42. int tens;//用于计算小数部分位数
  43. cin >> n;
  44. double decimal = n - (int)n;//获取小数部分
  45. for (tens=1; abs(round(decimal) - decimal) > PRECISION; tens *= 10)
  46. decimal *= 10;
  47. up = n * tens;
  48. down = tens;//把小数乘10的小数位数次并除以十的小数位数次,转化为分数
  49. simplify(&up, &down);// 化简
  50. cout << up << "/" << down;
  51. return 0;
  52. }

操作数

  1. #include<iostream>
  2. using namespace std;
  3. //计算位数
  4. int wide(int a)
  5. {
  6. int i;
  7. for (i=0;a!=0;i++)
  8. {
  9. a = a /10;
  10. }
  11. return i;
  12. }
  13. //进行一次减去各位数字和的操作
  14. int operate(int a)
  15. {
  16. int times, i, add, res;
  17. res = a;
  18. times = wide(a);
  19. for (i = 1; i <= times; i++)
  20. {
  21. add = a % 10;
  22. a = a / 10;
  23. res = res - add;//每次循环减去一位数字的值
  24. }
  25. return res;
  26. }
  27. int main()
  28. {
  29. int n, i, add,res;
  30. cin >> n;
  31. res = n;
  32. //计录操作次数
  33. for (i = 0; res >0;i++)
  34. {
  35. res = operate(res);
  36. }
  37. cout << i;
  38. return 0;
  39. }

这个写法复杂了,其实可以两个for循环解决的

对称数

  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. int wide(int a)
  5. {
  6. int i;
  7. for (i = 0; a != 0; i++)
  8. {
  9. a = a / 10;
  10. }
  11. return i;
  12. }
  13. int main()
  14. {
  15. int n, times,i,res;
  16. cin >> n;
  17. times = wide(n)/2;
  18. res = 1;
  19. for (i = 1; i <= times; i++)
  20. {
  21. int left =( n / (int)pow(10.0, wide(n)-i))%10;
  22. int right = (n % (int)pow(10.0, i)) / (int)pow(10.0, i-1);
  23. if (left == 1 && right == 1 || left == 9 && right == 6 || left == 6 && right == 9 || left == 8 && right == 8)
  24. res = res;
  25. else
  26. res = 0;
  27. }
  28. if (res == 1)
  29. cout << "Yes";
  30. else
  31. cout<<"No";
  32. }

 Yes和No别忘了大写

21~30

最大数字

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int N, result, left, right,tens,wrong;
  6. cin >> N;
  7. if (N < 10)cout << N;
  8. else left = N / 10 % 10;
  9. for (result = N; result >= 10; result--)
  10. {
  11. for (tens = 10,wrong=0; wrong==0&&result/tens>0; tens *= 10)
  12. {
  13. left = result / tens % 10;
  14. right = (result * 10 / tens) % 10;
  15. if (left > right)
  16. wrong ++;
  17. }
  18. if (right == 0) wrong++;
  19. if (wrong == 0)
  20. {
  21. cout << result;
  22. break;
  23. }
  24. }
  25. return 0;
  26. }

余数和

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n, k, sum=0;
  6. cin >> n >> k;
  7. int i;
  8. for (i = 1; i <= n; i++)
  9. {
  10. sum += k % i;
  11. }
  12. cout << sum;
  13. }

 竖式乘法

  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. int widenum(int a)
  5. {
  6. int i;
  7. for (i = 0; a != 0; i++)
  8. {
  9. a = a / 10;
  10. }
  11. return i;
  12. }
  13. int ten(int a)
  14. {
  15. int i,ten=1;
  16. for (i = 1; i <= a; i++)
  17. ten *= 10;
  18. return ten;
  19. }
  20. int main()
  21. {
  22. int n1, n2, wide;
  23. int i;
  24. cin >> n1 >> n2;
  25. wide = widenum(n1 * n2);
  26. cout << setw(wide+1) << right << n1 << endl;
  27. cout << 'x' << setw(wide) << right << n2 << endl;
  28. for (i = 0; i <= wide; i++)
  29. cout << '-';
  30. cout << endl;
  31. for (i = 0; i < widenum(n2)-1; i++)
  32. cout << setw(wide+1 - i) << right << (n2 / ten(i) % 10) * n1 << endl;
  33. cout<<'+'<<setw(wide - i) << right << (n2 / ten(i) % 10) * n1 << endl;
  34. for (i = 0; i <= wide; i++)
  35. cout << '-';
  36. cout << endl;
  37. cout <<setw(wide+1)<<right<< n1 * n2;
  38. }

阶乘倍数

倒水

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5. int Pour(int m, int n, int d)
  6. {
  7. int mreal= 0, nreal= 0, step1 = 0, step2 = 0;
  8. while (mreal != d && nreal != d) {
  9. if (mreal == 0) {
  10. mreal = m;
  11. }
  12. else if (nreal == n) {
  13. nreal = 0;
  14. }
  15. else {
  16. int total = nreal + mreal > n ? n - nreal : mreal;
  17. mreal -= total;
  18. nreal += total;
  19. }
  20. step1++;
  21. }
  22. mreal = 0, nreal = 0;
  23. while (mreal != d && nreal != d) {
  24. if (nreal == 0) {
  25. nreal = n;
  26. }
  27. else if (mreal == m) {
  28. mreal = 0;
  29. }
  30. else {
  31. int total = mreal + nreal > m ? m - mreal : nreal;
  32. nreal -= total;
  33. mreal += total;
  34. }
  35. step2++;
  36. }
  37. int step = step1 > step2 ? step2 : step1;
  38. return step;
  39. }
  40. int main() {
  41. int m, n, d;
  42. cin >> m >> n >> d;
  43. cout << Pour(m, n, d);
  44. return 0;
  45. }

毕达哥拉斯三元组

  1. #include<iostream>
  2. using namespace std;
  3. int main() {
  4. int n;
  5. cin >> n;
  6. for (int a = 1; a < n; a++) {
  7. for (int b = a; b < n; b++) {
  8. int c = n - a - b;
  9. if (a * a + b * b == c * c) {
  10. cout << a * b * c << endl;
  11. return 0;
  12. }
  13. }
  14. }
  15. cout << "No solution" << endl;
  16. return 0;
  17. }

俄罗斯农夫乘法

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n1, n2,res=0;
  6. cin >> n1 >> n2;
  7. while (n1 != 0)
  8. {
  9. if (n1 % 2 != 0)res += n2;
  10. cout << n1 << ' ' << n2 << endl;
  11. n1 >>=1;
  12. n2 <<=1;
  13. }
  14. cout << res;
  15. return 0;
  16. }

 查找数列

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int i,j, a,n;
  6. cin >> n;
  7. for (i = 1, a = 1; a<n; i++)
  8. {
  9. for (j = 1; j <= i; j++, a++)
  10. if (a == n)break;
  11. }
  12. cout << j;
  13. return 0;
  14. }

方案数

  1. #include<iostream>
  2. using namespace std;
  3. int iter(int n,int i)
  4. {
  5. int tmp=0;
  6. while (i > 0)
  7. {
  8. tmp += i;
  9. i--;
  10. if (tmp == n)return 1;
  11. else if(tmp > n)break;
  12. }
  13. return 0;
  14. }
  15. int solution(int n)
  16. {
  17. int i, res=1;
  18. for (i = (n / 2) + 1; i >= 2; i--)
  19. res += iter(n, i);
  20. return res;
  21. }
  22. int main()
  23. {
  24. int n;
  25. cin >> n;
  26. switch (n)
  27. {
  28. case 1:
  29. case 2:
  30. cout << '1';
  31. break;
  32. default:
  33. cout << solution(n);
  34. }
  35. return 0;
  36. }

 好数字

  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. long goodNumber(long N, long i) {
  5. if (i == N) return 1;
  6. else if ((i % 2) == 0) {
  7. return (5 * goodNumber(N, i + 1)) % ((int)pow(10, 9) + 7);
  8. }
  9. else if ((i % 2) == 1) {
  10. return (4 * goodNumber(N, i + 1)) % ((int)pow(10, 9) + 7);
  11. }
  12. }
  13. int main()
  14. {
  15. long N, i = 0;
  16. cin >> N;
  17. cout << goodNumber(N, i);
  18. return 0;
  19. }

 31~40

可变参数累加

  1. #include<iostream>
  2. #include <initializer_list>//用来实现可变参数
  3. //关于可变参数的实现可以参考https://blog.csdn.net/qq_32534441/article/details/103495144?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169871084216777224498801%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=169871084216777224498801&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-103495144-null-null.142^v96^pc_search_result_base6&utm_term=%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0&spm=1018.2226.3001.4187
  4. // 此处选择 initializer_list标准库类型
  5. //1.initializer_list在C++11中才被引入,这意味着在编译时可能需要加上这个选项 - std = c++11 才能成功编译。上述代码中的auto关键字也是C++11的一部分;
  6. //2. 参数必须放在一组‘{}’(大括号)内,编译器通过大括号来将这组参数转化为initializer_list.大括号的的一组实参与initializer_list形参对应;
  7. //3. 函数原型initializer_list与普通形参无异。这表明形参列表中可以包含其他类型参数且位置不限
  8. using namespace std;
  9. int sum(initializer_list<int> il)//函数原型用int实例化initializer_list作为形参
  10. {
  11. int sum = 0;
  12. for (auto p = il.begin(); p != il.end(); p++) //使用迭代器访问参数
  13. sum += *p;
  14. return sum;
  15. }
  16. int main()
  17. {
  18. int a, b, c, d, e, f;
  19. cin >> a >> b >> c >> d >> e >> f;
  20. cout << sum({ a,b,0 }) - sum({ c,d,e,f,0 });//想向initializer_list形参中传递一个值的序列,必须把序列放在一对花括号内
  21. return 0;
  22. }

哈沙德数 

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5. int HarshadNumber(int n){
  6. int t=n,s=0;
  7. while(t){
  8. s=s+t%10;
  9. t=t/10;
  10. }
  11. if(s!=0 && n%s==0) return n/s;
  12. return 0;
  13. }
  14. int main(){
  15. int n,i=0;
  16. cin >> n;
  17. while(n>=2){
  18. if(HarshadNumber(n)!=0){
  19. n = HarshadNumber(n);
  20. i++;
  21. }
  22. else break;
  23. }
  24. cout << i << endl;
  25. return 0;
  26. }

幂函数

  1. #include<iostream>
  2. using namespace std;
  3. double pow(double base,int exp=2)
  4. {
  5. double res=1;
  6. for (int i = 1; i <= exp; i++)
  7. {
  8. res*=base;
  9. }
  10. return res;
  11. }
  12. int main()
  13. {
  14. double base;
  15. int exp;
  16. cin >> base >> exp;
  17. printf("%.6f\n", pow(base));
  18. printf("%.6f", pow(base, exp));
  19. return 0;
  20. }

素数

  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. int judge(int n)
  5. {
  6. int i;
  7. for (i = 2; i < 6; i++)
  8. if (n % i == 0)return 0;
  9. for (i = 1; 6 * i + 5 <n/i; i++)
  10. if (n % (6 * i + 1) == 0 || n % (6 * i + 5) == 0)return 0;
  11. return 1;
  12. }
  13. int main()
  14. {
  15. int left, right, res=0;
  16. cin >> left >> right;
  17. for (int i = left; i <= right; i++)
  18. if (judge(i)==1)res++;
  19. cout << res;
  20. }

 光线追踪

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. long long n , x;
  5. cin >> n >> x;
  6. if (n % 2 == 1){
  7. cout << (n - 1) * 3 << endl;
  8. }
  9. else{
  10. cout << 3 * ((n / 2) + abs(x - (n / 2))) << endl;
  11. }
  12. return 0;
  13. }

 基斯数

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. inline bool IsKeith(int n){
  5. int tmp=n,nums_i=99,lens=0;
  6. int nums[200]={0};
  7. while(tmp>0){
  8. nums[nums_i]=tmp%10;
  9. tmp/=10;
  10. nums_i++;
  11. }
  12. lens=nums_i-99;
  13. while(nums[nums_i-lens-1]!=n){
  14. for(int i=nums_i-1;i>=nums_i-lens;i--){
  15. nums[nums_i-lens-1]+=nums[i];
  16. }
  17. if(nums[nums_i-lens-1]==n) return true;
  18. else if(nums[nums_i-lens-1]>n||(nums_i-lens-1)<0) break;
  19. nums_i--;
  20. }
  21. return false;
  22. }
  23. int main() {
  24. int n;
  25. std::cin >> n;
  26. if(IsKeith(n)) std::cout<<"Yes"<<std::endl;
  27. else std::cout << "No"<<std::endl;
  28. return 0;
  29. }

可变参数平均 

  1. #include<iostream>
  2. #include<stdarg.h>
  3. using namespace std;
  4. double avg(int n,...)
  5. {
  6. int i, val;
  7. double sum=0;
  8. va_list v1;
  9. va_start(v1, n);
  10. for (i = 0; i < n; i++)
  11. {
  12. val = va_arg(v1, double);
  13. sum += val;
  14. }
  15. va_end(v1);
  16. return sum / i;
  17. }
  18. int main()
  19. {
  20. double a, b, c, d, e;
  21. cin >> a >> b >> c >> d >> e;
  22. printf("%.4f", avg(2, a, b) - avg(3, c, d, e));
  23. }

可变参数平均 

  1. #include<iostream>
  2. using namespace std;
  3. int PA(int n)
  4. {
  5. if (n==0)return 0;
  6. else if (n == 1)return 1;
  7. return 2 * PA(n - 1) + PA(n - 2);
  8. }
  9. int PB(int n)
  10. {
  11. int p0 = 0, p1 = 1, pn, i;
  12. for (i = 0; i <= n; i++)
  13. {
  14. if (i == 0)pn = p0;
  15. else if (i == 1)pn = p1;
  16. else
  17. {
  18. pn = 2 * p1 + p0;
  19. p0 = p1;
  20. p1 = pn;
  21. }
  22. }
  23. return pn;
  24. }
  25. int main()
  26. {
  27. int n;
  28. cin >> n;
  29. if (n % 2 != 0)cout << PA(n);
  30. else cout << PB(n);
  31. return 0;
  32. }

运动会 

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N = 50000;
  4. int vis[N];
  5. int prime[N];
  6. int phi[N];
  7. int sum[N];
  8. void get_phi() {
  9. phi[1] = 1;
  10. int cnt = 0;
  11. for (int i = 2; i < N; i++) {
  12. if (!vis[i]) {
  13. vis[i] = i;
  14. prime[cnt++] = i;
  15. phi[i] = i - 1;
  16. }
  17. for (int j = 0; j < cnt; j++) {
  18. if (i * prime[j] > N) break;
  19. vis[i * prime[j]] = prime[j];
  20. int m = i * prime[j];
  21. if (i % prime[j] == 0) { // m = prime[i] * i * ````
  22. phi[m] = phi[i] * prime[j];
  23. break;
  24. }
  25. phi[m] = phi[i] * phi[prime[j]];
  26. }
  27. }
  28. }
  29. int main() {
  30. get_phi();
  31. sum[1] = 1;
  32. for (int i = 2; i <= N; i++)
  33. sum[i] = sum[i - 1] + phi[i];
  34. int n;
  35. cin >> n;
  36. if (n == 1) cout << "0" << endl;
  37. else {
  38. printf("%d\n", 2 * sum[n - 1] + 1);
  39. }
  40. return 0;
  41. }

欧拉筛法解决

后续写完更新

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/545372
推荐阅读
相关标签
  

闽ICP备14008679号