当前位置:   article > 正文

2023 西北工业大学 NOJ 程序设计基础(C++) 100道完结版

2023 西北工业大学 NOJ 程序设计基础(C++) 100道完结版

极其感谢TA的帮助,所以下面是,爱门!

2023西工大NOJ (C语言版) 完结!!!-CSDN博客

前言

对于瓜的学生来说,noj是选了实验课之后必做的作业,占20%的分数;

但希望不要为了功利的目的写noj,理由如下:

1.编程永远是实践的过程,真正的知识只有自己打出来才能获得;更别说瓜的教材比较老,和新的C++标准有不少出入,尤其是习题册。

2.实验课也就1学分,不挂就行,你为了这点绩点功利啥。

3.瓜的实验课考试为随机抽题,最后答成什么样子主要看脸,你再功利也没啥用。

对于noj,自2023年秋开始启用新题,可以说,难度大幅提升。但质量存在极大参差,(◎_◎;);

C的NOJ教程有很多,但是C++的这可能还是第一篇,希望对有需要的人能有所帮助;

希望是有参考、引导的作用,帮助同学能更加高效地学习C++;

必看

对于代码

本人初学者,能力有限,有幸获得一位大佬很多帮助;

如若代码粗陋,请谅解;

因为本人水平实在有限,部分代码来自各个大佬,如不希望转载,可以联系我删除;

尽量使用较新的C++17标准,而不是教材和习题册上的包浆标准(食答辩了);

代码当然是尽可能AC的,但是由于noj的评阅比较雾,有的完全正确的也是WA,可以试试其他version,要是都不能我也没办法,饶了我罢;

代码格式

每题的代码,前两行会是 难度,重要性 评估,然后是该程序的一些注意点。

然后会是1+个版本,最后输入输出样例。(多版本时,后续的版本为了可读性不会注释掉,复制的时候注意)

考虑到做题方便,我会给出额外样例,以供同学检验自己的程序。

同时,考虑到中文复制之后会产生字符集问题,所以程序内部注释为纯英文(Chiglish)。

推荐工具

1.建议使用CLion作为编程工具,别惦记那CodeBlocks,Dev-C++了,一个太简陋,一个太老,不具有现代化工具的特点,都4202年啦,大人,时代变啦!

   实在不行也得用Visual Studio,community版本免费,官网链接如下

   Visual Studio 2022 IDE - 适用于软件开发人员的编程工具 (microsoft.com)

   你说你只会CodeBlocks?甚至CodeBlocks都配置不明白?能不能有点学习意识,

   B站上手把手教你安装配置Clion,Visual Studio的视频一抓一大把,学不会就洗洗睡吧。

   你说Visual Studio下载太慢?我滴IDM可是一眨眼就下好了,你也可以试试梯子。

   至于最推荐的CLion的安装、配置、使用,我就直接放大佬的文章了

   CLion安装、配置、使用、调试(完全小白向)-CSDN博客

2.不会吧,不会吧,不会有人写noj是网页看一眼,切个界面再打一句代码吧,或者是每次一忘就要切回头看一眼,烦不烦呐。

   什么,你说你是小机灵鬼,直接分屏,可是如果你不是外接显示器,那么点大的屏幕还分屏,折磨自己捏。

   所以,在下隆重推荐Snipatse,可以直接把noj上面的题目截下来,悬浮显示在最上层,而且还可以白嫖!

   官网链接Snipaste - 截图 + 贴图,不过建议直接在微软商店下载,体验更友好。至于使用方法,当然是自己去B站大学自学啦。

注:上述建议均基于Windows电脑,MacOS我真不道啊。

建议

1.学C++,就不要觉得自己只学C++,C的很多操作也得会。很多人C++学到最后,scanf()和printf()都不会。

NOJ攻略

1.写noj必须具有的意识:考虑极端情况,题目肯定是尽可能的恶心(划掉),考验你啦.

2.noj提交后的各种状态:

        pending        :处理中,重新刷新或者重新点击NOJ作业有概率得到结果;

                                如果一直pending,大概率是noj服务器又崩了罢(不是你的锅);

                                等服务器好了之后,可以在提交的程序上打一个空行再提交。

        AC                :你醒啦,恭喜你,你已经是个女孩子了(幻视,划掉);恭喜你,这道题通过了。

        WA               :程序错误,不能在输入后台样例的情况下得到正确结果。

        TE                :超时,也就是你的程序运行时间太长;

                                noj有的题目对时间卡的很紧,出现这种情况说明你又没动脑子 : (

                                解决方法很简单,抛弃你的暴力算法,用好的算法好好优化一下你的程序。

                            (注:对于noj来说,有的时候你的程序其实是TE,却给你显示WA,别给我犟,叉腰.jpg)

        CE                :编译错误。

                               如果你想:我本地运行的好好的,为什么提交上去就CE呢,不是可以编译吗?

                               一般有两种原因:

                                        1.头文件写漏了,本地没毛病是因为正常编译器会给你自动添加部分头

                                           文件,而noj的编译器就像薛定谔的猫,没人知道jxf到底塞的什么。

                                        (经典用cpp的string类不加<string>,加<cstring>)

                                        2.孔乙己,你是不是又用了什么STL特性?

                                           什么?你说C++如果不用STL,和C有什么区别?

                                           可是我们noj的编译器似乎不能完全支持STL捏(有时这种情况报WA)

3.代码输出:

        有人发现,很多一次输入很多组数据的题目,我提供的代码不能输出对应的样例,凭什么说能AC?

        这和noj后台的评阅机制有关,你别管了,长篇大论懒得扯,反正能,就是能;(叉腰.jpg)

        (实际上很多程序你直接复制粘贴我给的Input,然后敲个回车,输出会有问题,但是你自己一个数据一个数据地敲,或者分行粘贴,每行回车就好了);

良好的编程习惯

下面是我的编程习惯(我是菜鸡,大佬轻点骂)

第一部分:预处理命令,#include,#define等

空一行

第二部分: using namespace std;

空一行

第三部分:各类函数,彼此空行

空一行

第四部分:主函数 int main()

下面是示例

合理的空行和缩进不仅美观,还可以有效提升程序可读性!

  1. #include <iostream>
  2. using namespace std;
  3. void fun(){
  4. }
  5. int main() {
  6. return 0;//new C++ standard can ignore this
  7. }

夹带私货

关于电脑相关的外设,建议整一个机械键盘(一定要有素质,买线性静音轴,比如水蜜桃V2);

更重要的是,整个无线鼠标,尤其是带两个侧键的,比如罗技G304,一个侧键定义Ctrl+C,一个定义Ctrl+V,这会极大提升你的编程体验!

如果想买一个外接显示器的话(可以显著提升电脑使用体验),建议买23.8寸左右的,27寸你瓜宿舍桌面真塞的下,但是太大了,观感很不好。最重要的是分辨率至少2k,刷新率75hz就够用,色准要好,基础HDR要有,要是支持Type-C一线连接就更好啦。(我滴SANC N50PLUS 3代完美符合捏)在这之后就可以买个侧立支架把笔记本合上了,(如果你不是很追求双屏体验,双屏瓜的桌子也不怎么塞得下吧)。(十分建议显示器在京东上买)

对了,显示器最好搭配支架使用,NB f80就挺好的,便宜好用质量好。(为了应对挡板,可以搭配某宝定制小实木木块使用)

免责声明:看了私货部分,决定要买什么是自己的事,自己的决定,在下的话只是建议,仅供参考,不负责任,请自己思考判断自己是否需要,以及是否自愿承担相应风险。

声明

note只会在前几季有,用于引导新手,才不是因为懒呢;

但是放心,后面的难题,或者复杂的题一般都会带注释,甚至可能中文注释;

后期会逐渐添加题目截图,有的可能不含Sample Input和Sample Output部分,但是代码最后肯定有;

如果Additional Input和Additional Output 是NULL,说明这题没必要搞额外样例,或者是太麻烦,我已经懒死了;(要是实在想要额外样例,拿我的代码随便跑几个不就行了)

有的题目,我说无人AC,是基于我的认知,可能有真的大佬,可能题目又被修改过了,反正是我这个小垃圾肯定AC不了酱紫;

严正声明:该篇Blog仅作引导作用,绝不鼓励抄袭,若因其他用途产生不良后果与本人无关。

总结

2023年秋-2024年春的C++ noj 一共100道,应该是最多AC97道的亚子;

其中   042.【专业融合:航空】飞机起飞速度

          057.字符串替换

          074.【专业融合:天文】日出日落时间

三道不能AC;

第1季(001-010)

001.Hello World

  1. //Difficulty: 0/10
  2. //Importance:10/10
  3. //The place our dream begin!
  4. //What you need:
  5. //a good coding style(tab,enter,naming...)
  6. //clear coding logics
  7. #include <iostream>
  8. using namespace std;
  9. int main()
  10. {
  11. cout << "Hello World"<<endl;//printf("hello world\n");
  12. return 0;
  13. }//tip:no "!" here
  14. /*
  15. Sample input:
  16. null
  17. Sample output:
  18. Hello World
  19. */
  20. /*
  21. Additional input:
  22. null
  23. Additional output:
  24. Hello World
  25. */

002.A+B

  1. //Difficulty: 0/10
  2. //Importance: 0/10
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. int A, B;
  8. cin >> A >> B ;
  9. cout <<A+B<<endl;
  10. return 0;
  11. }
  12. /*
  13. Sample input:
  14. 3 4
  15. Sample output:
  16. 7
  17. */
  18. /*
  19. Additional input:
  20. 5 6
  21. Additional output:
  22. 11
  23. */

003.数据类型大小及范围

note:

1.sizeof()函数的学习;

2.<climits>的基本运用。

  1. //Difficulty: 1/10
  2. //Importance: 1/10
  3. //make use of <climits> (<limits.h>) to get min and max sizes
  4. //sizeof(),return memory size(bit)
  5. #include <iostream>
  6. #include <climits>
  7. using namespace std;
  8. int main()
  9. {
  10. int n;
  11. cin >> n;
  12. switch (n) {
  13. case 1: cout << sizeof(char) << "," << CHAR_MIN << "," << CHAR_MAX; break;
  14. case 2: cout << sizeof(unsigned char) << "," << 0<< "," << UCHAR_MAX; break;
  15. case 3: cout << sizeof(short) << "," << SHRT_MIN << "," << SHRT_MAX; break;
  16. case 4: cout << sizeof(unsigned short) << "," << 0<< "," << USHRT_MAX; break;
  17. case 5: cout << sizeof(int) << "," << INT_MIN << "," << INT_MAX; break;
  18. case 6: cout << sizeof(unsigned int) << "," << 0<< "," << UINT_MAX; break;
  19. case 7: cout << sizeof(long) << "," << LONG_MIN << "," << LONG_MAX; break;
  20. case 8: cout << sizeof(unsigned long) << "," << 0 << "," << ULONG_MAX; break;
  21. case 9: cout << sizeof(long long) << "," << LLONG_MIN << "," << LLONG_MAX; break;
  22. case 10: cout << sizeof(unsigned long long) << "," << 0 << "," << ULLONG_MAX; break;
  23. }
  24. return 0;
  25. }
  26. /*
  27. Sample input:
  28. 5
  29. Sample output:
  30. 4,-2147483648,2147483647
  31. */
  32. /*
  33. Additional input:
  34. 1
  35. Additional output:
  36. 1,-128,127
  37. */

004.平均值

note:

1.数据容量“溢出”的概念;

2.基础位运算;

        a>>i        为:将二进制数据a右移i位,即a/(2^i);

        a<<i        为:将二进制数据a左移i位,即a*(2^i);

  1. //Difficulty: 3/10
  2. //Importance:10/10
  3. //Note that the data range corresponding to the type
  4. //may be exceeded during the calculation,
  5. //resulting in overflow
  6. //version 1:Bitwise operations(better)
  7. #include <iostream>
  8. using namespace std;
  9. int main()
  10. {
  11. int A, B;
  12. cin >>A >>B;
  13. int avg = ((A - B) >> 1) + B;
  14. cout<<avg;
  15. return 0;
  16. }
  17. //version 2:Split
  18. #include <iostream>
  19. using namespace std;
  20. int main()
  21. {
  22. int A, B;
  23. cin >>A >>B;
  24. int avg = (A - B)/2 + B;
  25. cout<<avg;
  26. return 0;
  27. }
  28. /*
  29. Sample input:
  30. 3 4
  31. Sample output:
  32. 3
  33. */
  34. /*
  35. Additional input:
  36. 5 6
  37. Additional output:
  38. 5
  39. */

005.进制转换

note:

1.printf()的基础运用(数字输出):

        %d        :即为dec,十进制;

        %o        :即为oct, 八进制;

        %x        :即为hex,十六进制(小写);

        %X       :即为hex,十六进制(大写);

2.cout与格式输入输出库<iomanip>基础:

     对于cout,默认输出dec,每次使用hex等修改时,此后均为hex,除非重新修改。

     在<iomanip>中:

  • uppercase :十六进制格式字母变大写

  • showpos :在正数前显示+号

  • showbase :十六进制前显示 0x, 八进制前显示0

  • boolalpha :逻辑值1和0用ture和false 输出

  • left :输出内容靠左

  • right :输出内容靠右

  • scientific :科学记数法

  • showpoint : 即使小数后面都是0,也输出小数点。

  1. //Difficulty: 2/10
  2. //Importance:10/10
  3. //make use of C's good features
  4. //printf() is very useful when you need special outputs
  5. //version 1:printf(recommended)
  6. #include<iostream>
  7. using namespace std;
  8. int main()
  9. {
  10. int a;
  11. cin >> a;
  12. printf("%X,%o",a,a);
  13. return 0;
  14. }
  15. //version 2:cout
  16. #include <iostream>
  17. #include <iomanip>
  18. using namespace std;
  19. int main()
  20. {
  21. int a;
  22. cin>>a;
  23. cout<<setiosflags(ios::uppercase)<<hex<<x<<endl;
  24. }
  25. /*
  26. Sample input:
  27. 10
  28. Sample output:
  29. A,12
  30. */
  31. /*
  32. Additional input:
  33. 12
  34. Additional output:
  35. C,14
  36. */

006.浮点数输出

note:

1:在cin或cout中指明数制后,该数制将一直有效,直到重新指明使用其他数制

2:为什么很多时候我喜欢用printf:不用头文件,使用简洁方便。

  1. //Difficulty: 1/10
  2. //Importance:10/10
  3. //version 1: printf(recommended)
  4. #include<iostream>
  5. using namespace std;
  6. int main()
  7. {
  8. double a;
  9. cin >> a;
  10. printf("%.6f,%.2f,%.8f",a,a,a);
  11. return 0;
  12. }
  13. //version 2: cout
  14. #include<iostream>
  15. #include<iomanip>
  16. using namespace std;
  17. int main()
  18. {
  19. double a;
  20. cin >> a;
  21. cout <<fixed<< setprecision(6) << a << "," << fixed <<setprecision(2) << a << "," << fixed <<setprecision(8) << a << endl;
  22. //or: cout <<fixed<< setprecision(6) << a << "," << setprecision(2) << a << "," << setprecision(8) << a << endl;
  23. return 0;
  24. }
  25. /*
  26. Sample input:
  27. 12345567.891234567
  28. Sample output:
  29. 12345567.891235,12345567.89,12345567.89123457
  30. */
  31. /*
  32. Additional input:
  33. 3.141592654
  34. Additional output:
  35. 3.141593,3.14,3.14159265
  36. */

007.动态宽度输出

note:

1.cout、iomanip和printf各有各的好,要灵活使用;

2.setw()和setfill()的先后顺序无所谓。

  1. //Difficulty: 1/10
  2. //Importance:10/10
  3. #include <iostream>
  4. #include <iomanip>
  5. using namespace std;
  6. int main()
  7. {
  8. int m, n;
  9. cin >> m >> n;
  10. cout << setw(n) << setfill('0') << m << endl;
  11. return 0;
  12. }
  13. /*
  14. Sample input:
  15. 123 5
  16. Sample output:
  17. 00123
  18. */
  19. /*
  20. Additional input:
  21. 123 7
  22. Additional output:
  23. 0000123
  24. */

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

note:

1.使用宏#define的时候注意最后加不加分号;宏是简单的符号替换;

2.变量名写的时候最好可以代表具体意义,不要脸滚键盘;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. #include<cmath>
  5. #include<iomanip>
  6. #define pi 3.141592654
  7. using namespace std;
  8. double hav(double x) {
  9. double result = (1 - cos(x)) * 0.5;
  10. return result;
  11. }
  12. double trans(double e) {
  13. double m;
  14. m = (e / 180) * pi;
  15. return m;
  16. }
  17. int main()
  18. {
  19. double x1, y1, x2, y2,s,m,d;
  20. cin >> x1 >> y1;
  21. cin >> x2 >> y2;
  22. x1 = trans(x1), y1 = trans(y1), x2 = trans(x2), y2 = trans(y2);
  23. s = hav(x2 - x1) + cos(x1) * cos(x2) * hav(y2 - y1);
  24. m = acos(1 - 2*s);
  25. d = m * 6371;
  26. cout <<fixed<<setprecision(4)<< d <<"km"<< endl;
  27. }
  28. /*
  29. Sample input:
  30. 34.260958 108.942369
  31. 55.755825 37.617298
  32. Sample output:
  33. 5793.2236km
  34. */
  35. /*
  36. Additional input:
  37. 24.260958 105.942369
  38. 45.755825 31.617298
  39. Additional output:
  40. 6917.5743km
  41. */

009.风寒指数

note:

<cmath>库基础:

1.pow()函数:

原型double pow(double x, double y);         

求x的y次方,注意参数与返回值均为double类型,因此有时搭配取整函数食用更佳;

2.三个取整函数:

        double floor(doube x);        floor为地板的意思,所以是向下取整;

        double ceil(doube x);         ceiling为天花板的意思,所以是向上取整;

        double round(doube x);     round为左右、四周的意思,所以是四舍五入;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. #include<cmath>
  5. using namespace std;
  6. int main()
  7. {
  8. double v, t;
  9. double chilldegree;
  10. cin >> v >> t;
  11. double a = pow(v, 0.16);
  12. chilldegree = 13.12 + 0.6215 * t - 11.37 * a+ 0.3965* t * a;
  13. chilldegree = round(chilldegree);
  14. cout << chilldegree;
  15. }
  16. /*
  17. Sample input:
  18. 120 35
  19. Sample output:
  20. 40
  21. */
  22. /*
  23. Additional input:
  24. 66 39
  25. Additional output:
  26. 45
  27. */

010.颜色模型转换

note:

1.注意逻辑顺序,考虑极端情况;

2.涉及浮点数精度问题,当判断一个浮点数x是否为0,应使用 x-0<1e-9等类似方式;

  1. //Difficulty: 3/10
  2. //Importance: 3/10
  3. //given types of float,double have precision problem
  4. //(a - b < 1e-9) is used to judge whether it is 0
  5. #include<iostream>
  6. #include<iomanip>
  7. using namespace std;
  8. int main() {
  9. int R, G, B;
  10. cin >> R >> G >> B;
  11. double r, g, b, h, s, v;
  12. double MAX, MIN, d;
  13. r = R / 255.0;
  14. g = G / 255.0;
  15. b = B / 255.0;
  16. MAX = max(r,max(g,b) );
  17. MIN = min(r,min(g,b));
  18. d = MAX - MIN;
  19. v = MAX;
  20. if (MAX - 0 < 1e-9) {
  21. s = 0;
  22. }
  23. else {
  24. s = d / MAX;
  25. }
  26. if (d - 0 < 1e-9) {
  27. h = 0;
  28. }
  29. else {
  30. s = d / MAX;
  31. if (MAX - r < 1e-9) {
  32. h = 60 * ((g - b) / d);
  33. }
  34. else if (MAX - g < 1e-9) {
  35. h = 60 * (2 + (b - r) / d);
  36. }
  37. else if (MAX - b < 1e-9) {
  38. h = 60 * (4 + (r - g) / d);
  39. }
  40. if (h - 0 < 1e-9) {
  41. h = h + 360;
  42. }
  43. }
  44. cout << fixed << setprecision(4) << h << ","
  45. << fixed << setprecision(4) << s * 100 << "%,"
  46. << fixed << setprecision(4) << v * 100 << "%";
  47. }
  48. /*
  49. Sample input:
  50. 0 215 0
  51. Sample output:
  52. 120.0000,100.0000%,84.3137%
  53. */
  54. /*
  55. Additional input:
  56. 0 0 0
  57. Additional output:
  58. 0.0000,0.0000%,0.0000%
  59. */
  60. /*
  61. Additional input:
  62. 255 255 255
  63. Additional output:
  64. 0.0000,0.0000%,100.0000%
  65. */
  66. /*
  67. Additional input:
  68. 123 54 45
  69. Additional output:
  70. 6.9231,63.4146%,48.2353%
  71. */

第2季(011-020)

011.乘数模

note:

1.依旧是溢出问题,程序里运用数学工具解决;

2.涉及一些数论知识:

        模的乘法性质:对于任意整数a、b和n,有(a * b) mod n = ((a mod n) *( b mod n)) mod n;

3.用了上面一条性质,可以优化程序,有效缩减运行时间,防止TE;

  1. //Difficulty: 2/10
  2. //Importance:10/10
  3. //learn some formulas for mod
  4. //to prevent exceeding and TE
  5. #include <iostream>
  6. using namespace std;
  7. int main()
  8. {
  9. unsigned long long a, b, m, r;
  10. cin >> a >> b >> m;
  11. r = ((a % m) * (b % m)) % m;
  12. cout << r;
  13. }
  14. /*
  15. Sample input:
  16. 123 456 100000
  17. Sample output:
  18. 56088
  19. */
  20. /*
  21. Additional input:
  22. 1244 12567 5758
  23. Additional output:
  24. 378
  25. */

012.方阵

note:

1.小小的数学转化罢了;

2.这里用abs()只是方便熟悉<cmath>库,三目运算更简便,abs()为绝对值函数;

3.有的老师讲三目运算符的时候不太强调,简单说一下:

        假设有表达式a,b,c;那么三目运算符

        a ? b : c ;                  代表  a为真,得到b;a为假,得到c;

  1. //Difficulty: 2/10
  2. //Importance: 4/10
  3. //discover some wonderful functions in cmath!
  4. #include <iostream>
  5. #include <cmath>
  6. using namespace std;
  7. void generatematrix(int n) {
  8. for (int i = 1; i <= n; i++) {
  9. for (int j = 1; j <= n; j++) {
  10. int v = abs(i - j);//or : int v = (i-j)>0 ? i-j : j-i ;
  11. cout << v << " ";
  12. }
  13. cout << endl;
  14. }
  15. }
  16. int main()
  17. {
  18. int m;
  19. cin >> m;
  20. generatematrix(m);
  21. }
  22. /*
  23. Sample input:
  24. 5
  25. Sample output:
  26. 0 1 2 3 4
  27. 1 0 1 2 3
  28. 2 1 0 1 2
  29. 3 2 1 0 1
  30. 4 3 2 1 0
  31. */
  32. /*
  33. Additional input:
  34. 2
  35. Additional output:
  36. 0 1
  37. 1 0
  38. */

013.组合数

note:

1.关于 while,if,for等循环语句的格式:

        当循环内只有一个语句时,可以不加{ };

        至于空行和缩进,都不影响编译器的扫描;

        但是建议一句时,循环内语句直接写在  while,if,for后面,不要随便换行,保证优秀的可阅读性;

        理论上,你把所有语句全换成逗号间隔,那算起来只有一句,也不用{ };你最好不要这么干,考虑逗号表达式和可读性;分号该加就加,换行该打就打,要有美感。

        多语句使用{ }时,建议 while,if,for后面跟 {   空一行之后,与 while,if,for的开头对齐,打 } (这是大部分主流编程软件的自动格式,然鹅破破又烂烂的CodeBlocks,根本不会给自动格式,导致有的人写的程序不忍直视)

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. //simple enum
  4. #include <iostream>
  5. using namespace std;
  6. int main() {
  7. int a,b,c,d,n,sum=0;
  8. cin>>n;
  9. for (a=0;a<=9;++a){
  10. for(b=0;b<=9;++b){
  11. for(c=0;c<=9;++c){
  12. for(d=0;d<=9;++d){
  13. if (n==a+b+c+d) sum++;
  14. else sum=sum+0;
  15. }
  16. }
  17. }
  18. }
  19. cout<<sum;
  20. }
  21. /*
  22. Sample input:
  23. 15
  24. Sample output:
  25. 592
  26. */
  27. /*
  28. Additional input:
  29. 37
  30. Additional output:
  31. 0
  32. */

014.比率

note:

1.gcd,为求最大公因数的算法,也称辗转相除法,非常有用,必须记忆;作为函数时,可以进行递归调用;

2.floor函数前面 009.风寒指数 的note已经介绍过;

  1. //Difficulty: 3/10
  2. //Importance:10/10
  3. //memorize gcd!
  4. #include <iostream>
  5. #include <cmath>
  6. using namespace std;
  7. int gcd(int a,int b){
  8. if(b==0){return a;}
  9. return gcd(b,a%b);
  10. }
  11. int main(){
  12. double fz;
  13. int fm=1,m;
  14. cin>>fz;
  15. for(fz;fz!=floor(fz);fz=fz*10,fm=fm*10);
  16. m=gcd((int)(fz),fm);
  17. fz=fz/m;
  18. fm=fm/m;
  19. cout<<fz<<"/"<<fm;
  20. }
  21. /*
  22. Sample input:
  23. 4.2
  24. Sample output:
  25. 21/5
  26. */
  27. /*
  28. Additional input:
  29. 3.33333
  30. Additional output:
  31. 333333/100000
  32. */

015.分数的加、减、乘、除法

note:

1.相信你肯定为函数如何带回多个数据发愁,这涉及参数传递的概念,然鹅很可能还没讲到这;

2.解决方法(此处仅列出两个)

        数组带回:数组作为函数参数时,传入的是数组地址,因而函数内对数组的修改可以保存下来;

        tuple元组:在C++中,元组(tuple)是一种用于组合多个不同类型的值的数据结构。元组可以将不同类型的数据打包在一起,类似于一个容器,可以按照索引顺序访问其中的元素。

        把函数的返回类型设置成元组tuple,即可返回含有多个数据的tuple;

3.非常实用的C语言函数一例:

        getchar()函数,用于从缓冲区得到一个字符;

        在这个程序里用来去掉不想要的符号‘/’ ,然而在各种程序里,getchar()也可以用来使你的console控制台(没错就是那个大黑框)在程序运行到最后的时候保持显示,直至你有输入(只需在程序的最后加上一行getchar();)

        当你获得的字符对你没有作用,你就可以直接写getchar();而不需要定义一个变量,把getchar()赋进去;

  1. //Difficulty: 3/10
  2. //Importance: 3/10
  3. //I don't think tuple necessary
  4. //you can use point to replace
  5. //more acceptable way is using Global variables
  6. //plus,getchar() is very useful
  7. //version 1:use array to pass values(recommended)
  8. #include <iostream>
  9. using namespace std;
  10. int gcd(int x,int y){
  11. if(y==0)return x;
  12. else return gcd(y,x%y);
  13. }
  14. void add(int fz1,int fm1,int fz2,int fm2,int arr[4][2]){
  15. int nfz,nfm,d;
  16. nfz=fz1*fm2+fz2*fm1;
  17. nfm=fm1*fm2;
  18. d=gcd(nfz,nfm);
  19. nfz=nfz/d;
  20. nfm=nfm/d;
  21. arr[0][0]=nfz;
  22. arr[0][1]=nfm;
  23. }
  24. void sub(int fz1,int fm1,int fz2,int fm2,int arr[4][2]){
  25. int nfz,nfm,d;
  26. nfz=fz1*fm2-fz2*fm1;
  27. nfm=fm1*fm2;
  28. d=gcd(nfz,nfm);
  29. nfz=nfz/d;
  30. nfm=nfm/d;
  31. arr[1][0]=nfz;
  32. arr[1][1]=nfm;
  33. }
  34. void multi(int fz1,int fm1,int fz2,int fm2,int arr[4][2]){
  35. int nfz,nfm,d;
  36. nfz=fz1*fz2;
  37. nfm=fm1*fm2;
  38. d=gcd(nfz,nfm);
  39. nfz=nfz/d;
  40. nfm=nfm/d;
  41. arr[2][0]=nfz;
  42. arr[2][1]=nfm;
  43. }
  44. void divi(int fz1,int fm1,int fz2,int fm2,int arr[4][2]){
  45. int nfz,nfm,d;
  46. nfz=fz1*fm2;
  47. nfm=fm1*fz2;
  48. d=gcd(nfz,nfm);
  49. nfz=nfz/d;
  50. nfm=nfm/d;
  51. arr[3][0]=nfz;
  52. arr[3][1]=nfm;
  53. }
  54. int main() {
  55. int fz1,fm1,fz2,fm2;
  56. cin>>fz1;
  57. getchar();
  58. cin>>fm1;
  59. cin>>fz2;
  60. getchar();
  61. cin>>fm2;
  62. int arr[4][2];
  63. add(fz1,fm1,fz2,fm2,arr);
  64. sub(fz1,fm1,fz2,fm2,arr);
  65. multi(fz1,fm1,fz2,fm2,arr);
  66. divi(fz1,fm1,fz2,fm2,arr);
  67. char symbol[4]={'+','-','*','/'};
  68. for(int i=0;i<4;++i){
  69. cout<<'('<<fz1<<'/'<<fm1<<')'<<symbol[i]<<'('<<fz2<<'/'<<fm2<<')'
  70. <<'='<<arr[i][0]<<'/'<<arr[i][1]<<endl;
  71. }
  72. return 0;
  73. }
  74. //version 2:use tuple
  75. #include <iostream>
  76. #include <tuple>
  77. using namespace std;
  78. int gcd(int x,int y){
  79. if(y==0)return x;
  80. else return gcd(y,x%y);
  81. }
  82. tuple<int,int> add(int fz1,int fm1,int fz2,int fm2){
  83. int nfz,nfm,d;
  84. nfz=fz1*fm2+fz2*fm1;
  85. nfm=fm1*fm2;
  86. d=gcd(nfz,nfm);
  87. nfz=nfz/d;
  88. nfm=nfm/d;
  89. return make_tuple(nfz,nfm);
  90. }
  91. tuple<int,int> sub(int fz1,int fm1,int fz2,int fm2){
  92. int nfz,nfm,d;
  93. nfz=fz1*fm2-fz2*fm1;
  94. nfm=fm1*fm2;
  95. d=gcd(nfz,nfm);
  96. nfz=nfz/d;
  97. nfm=nfm/d;
  98. return make_tuple(nfz,nfm);
  99. }
  100. tuple<int,int> multi(int fz1,int fm1,int fz2,int fm2){
  101. int nfz,nfm,d;
  102. nfz=fz1*fz2;
  103. nfm=fm1*fm2;
  104. d=gcd(nfz,nfm);
  105. nfz=nfz/d;
  106. nfm=nfm/d;
  107. return make_tuple(nfz,nfm);
  108. }
  109. tuple<int,int> divi(int fz1,int fm1,int fz2,int fm2){
  110. int nfz,nfm,d;
  111. nfz=fz1*fm2;
  112. nfm=fm1*fz2;
  113. d=gcd(nfz,nfm);
  114. nfz=nfz/d;
  115. nfm=nfm/d;
  116. return make_tuple(nfz,nfm);
  117. }
  118. int main() {
  119. int fz1,fm1,fz2,fm2;
  120. cin>>fz1;
  121. getchar();
  122. cin>>fm1;
  123. cin>>fz2;
  124. getchar();
  125. cin>>fm2;
  126. tuple<int,int> result0=add(fz1,fm1,fz2,fm2);
  127. cout<<'('<<fz1<<'/'<<fm1<<')'<<'+'<<'('<<fz2<<'/'<<fm2<<')'
  128. <<'='<<get<0>(result0)<<'/'<<get<1>(result0)<<endl;
  129. tuple<int,int> result1=sub(fz1,fm1,fz2,fm2);
  130. cout<<'('<<fz1<<'/'<<fm1<<')'<<'-'<<'('<<fz2<<'/'<<fm2<<')'
  131. <<'='<<get<0>(result1)<<'/'<<get<1>(result1)<<endl;
  132. tuple<int,int> result2=multi(fz1,fm1,fz2,fm2);
  133. cout<<'('<<fz1<<'/'<<fm1<<')'<<'*'<<'('<<fz2<<'/'<<fm2<<')'
  134. <<'='<<get<0>(result2)<<'/'<<get<1>(result2)<<endl;
  135. tuple<int,int> result3=divi(fz1,fm1,fz2,fm2);
  136. cout<<'('<<fz1<<'/'<<fm1<<')'<<'/'<<'('<<fz2<<'/'<<fm2<<')'
  137. <<'='<<get<0>(result3)<<'/'<<get<1>(result3)<<endl;
  138. }
  139. /*
  140. Sample input:
  141. 2/3
  142. 3/7
  143. Sample output:
  144. (2/3)+(3/7)=23/21
  145. (2/3)-(3/7)=5/21
  146. (2/3)*(3/7)=2/7
  147. (2/3)/(3/7)=14/9
  148. */
  149. /*
  150. Additional input:
  151. 1/2
  152. 1/2
  153. Additional output:
  154. (1/2)+(1/2)=1/1
  155. (1/2)-(1/2)=0/1
  156. (1/2)*(1/2)=1/4
  157. (1/2)/(1/2)=1/1
  158. */

016.幂数模

note:

1.常用的一个位运算:a&1  

        当a(默认dec)为奇数,a&1得到1;为偶数得到0;

        因而用来快速判断数的奇偶性;

 2.对 typedef  unsigned long long(或者long long) ll;的操作是常见的编程实践;

3.同样运用了模的乘法性质来优化,防止TE;

  1. //Difficulty: 3/10
  2. //Importance: 10/10
  3. //learn fast mod algorithm!
  4. //you can use Bitwise operations
  5. //but fast mod is better
  6. #include<iostream>
  7. typedef unsigned long long ll;
  8. using namespace std;
  9. int main(){
  10. ll a,b,m,ans=1;
  11. cin>>a>>b>>m;
  12. while(b){
  13. if(b&1){
  14. ans=(ans*a)%m;
  15. }
  16. a=(a*a)%m;
  17. b>>=1;
  18. }
  19. cout<<ans;
  20. }
  21. /*
  22. Sample input:
  23. 2 10 9
  24. Sample output:
  25. 7
  26. */
  27. /*
  28. Additional input:
  29. 2 63 1314
  30. Additional output:
  31. 512
  32. */

017.对称数

note:

1.使用C++更多的特性;

2.<unordered_map> 无序关联容器 和map一样,十分好用;

3.迭代器,掌握.begin() .end() .rbegin() .rend()

        迭代器在遍历等应用时非常常见且好用;一般命名为it,使用auto自动类型;

  1. //Difficulty: 5/10
  2. //Importance: 5/10
  3. //there must be longer,but stupider ways
  4. //why not take C++'s unique advantages?
  5. //such as map/unordered_map,iterator(which is easier to use)
  6. #include<iostream>
  7. #include<string>
  8. #include<unordered_map>
  9. using namespace std;
  10. int main()
  11. {
  12. string ori,inver;
  13. cin>>ori;
  14. unordered_map<char,char> table{{'0','0'},{'1','1'},{'6','9'},{'9','6'},{'8','8'}};
  15. for(auto it=ori.rbegin();it!=ori.rend();++it){
  16. inver.push_back(table[*it]);}// or: inver+=table[*it];
  17. if(inver==ori)cout<<"Yes";
  18. else cout<<"No";
  19. }
  20. /*
  21. Sample input:
  22. 916
  23. Sample output:
  24. Yes
  25. */
  26. /*
  27. Additional input:
  28. 1000081800001
  29. Additional output:
  30. Yes
  31. */

018.操作数

note:

1.理清楚逻辑就行;

  1. //Difficulty: 3/10
  2. //Importance: 3/10
  3. //version 1: no func
  4. #include<iostream>
  5. using namespace std;
  6. int main()
  7. {
  8. int n,n1,d,sum,k;
  9. cin>>n;
  10. sum=0;
  11. n1=n;
  12. for(k=0;n1>0;++k){
  13. while(n>0){
  14. d=n%10;
  15. sum+=d;
  16. n=n/10;
  17. }
  18. n1-=sum;
  19. n=n1;
  20. sum=0;
  21. }
  22. cout<<k;
  23. }
  24. //version 2: func
  25. #include<iostream>
  26. int sumPerDig(int n){
  27. int sum = 0;
  28. while(n){
  29. sum += n%10;
  30. n /= 10;
  31. }
  32. return sum;
  33. }
  34. int main(){
  35. int n,cnt=0;
  36. cin>>n;
  37. while(n){
  38. n -= sumPerDig(n);
  39. ++cnt;
  40. }
  41. cout<<cnt;
  42. return 0;
  43. }
  44. /*
  45. Sample input:
  46. 21
  47. Sample output:
  48. 3
  49. */
  50. /*
  51. Additional input:
  52. 164
  53. Additional output:
  54. 17
  55. */

019.倍数和 

note:

1.没啥东西,简单的枚举;

  1. //Difficulty: 3/10
  2. //Importance: 3/10
  3. #include<iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. int T;
  8. cin>>T;
  9. int num[T];
  10. for(int i=0;i<T;++i){
  11. int n;
  12. cin>>n;
  13. int sum=0;
  14. for(int j=1;j<n;j++){
  15. if (j%3==0||j%5==0){
  16. sum+=j;
  17. }
  18. }
  19. num[i]=sum;
  20. }
  21. for(int k=0;k<T;++k){
  22. cout<<num[k]<<endl;
  23. }
  24. }
  25. /*
  26. Sample input:
  27. 2
  28. 10
  29. 100
  30. Sample output:
  31. 23
  32. 2318
  33. */
  34. /*
  35. Additional input:
  36. 2
  37. 541
  38. 5115
  39. Additional output:
  40. 68310
  41. 6102195
  42. */

020.级数和

note:

1.没啥东西,注意一下输出的格式就行;

2.关于wid()函数

        返回int类型的位数,简单,常用,某种程度上,最好有肌肉记忆;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. using namespace std;
  5. int wid(int n){
  6. int i=0;
  7. if(n==0)return 1;
  8. else{
  9. while(n){
  10. n=n/10;
  11. i++;
  12. }
  13. return i;
  14. }
  15. }
  16. double decimal(double n){
  17. int w;
  18. w=wid(n);
  19. for(int i=0;i<w;++i){
  20. n=n/10.0;
  21. }
  22. return n;
  23. }
  24. int main()
  25. {
  26. int n;
  27. cin>>n;
  28. int z[n];
  29. for(int i=0;i<n;++i){
  30. z[i]=i+1;
  31. }
  32. int x[n];
  33. for(int j=0;j<n;++j){
  34. x[j]=j+2;
  35. }
  36. double sum=0.0,s;
  37. for(int k=0;k<n;++k){
  38. s=(double)z[k]+decimal((double)x[k]);
  39. sum+=s;
  40. }
  41. cout<<z[0]+decimal(x[0]);
  42. for(int k=0;k<n-1;++k){
  43. cout<<'+'<<z[k+1]+decimal((double)x[k+1]);
  44. }
  45. cout<<'='<<sum;
  46. }
  47. /*
  48. Sample input:
  49. 10
  50. Sample output:
  51. 1.2+2.3+3.4+4.5+5.6+6.7+7.8+8.9+9.1+10.11=59.61
  52. */
  53. /*
  54. Additional input:
  55. 5
  56. Additional output:
  57. 1.2+2.3+3.4+4.5+5.6=17
  58. */

第3季(021-030)

021.竖式乘法

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. //You can use setfill('?') before setw(n) to control
  4. //the character used for filling setw (default is space).
  5. #include <iostream>
  6. #include <iomanip>
  7. using namespace std;
  8. int wid(int n){
  9. int i=0;
  10. if(n==0)return 1;
  11. else{
  12. while(n){
  13. n=n/10;
  14. i++;
  15. }
  16. return i;
  17. }
  18. }
  19. int main() {
  20. int a,b,b0,i=0,ans,n,n0;
  21. cin>>a>>b;
  22. int z[wid(b)];
  23. b0=b;
  24. while(b0>0){
  25. int c=b0%10;
  26. z[i]=c*a;
  27. b0=b0/10;
  28. ++i;
  29. }
  30. ans=a*b;
  31. n=wid(ans);
  32. n0=n;
  33. cout<<setw(n+1)<<a<<endl<<'x'<<setw(n)<<b<<endl;
  34. for(int j=0;j<n+1;++j){
  35. cout<<'-';
  36. }cout<<endl;
  37. for(int j=0;j<wid(b)-1;++j){
  38. cout<<setw(n0+1)<<z[j]<<endl;
  39. n0=n0-1;
  40. }
  41. cout<<'+'<<z[wid(b)-1]<<endl;
  42. for(int j=0;j<n+1;++j){
  43. cout<<'-';
  44. }
  45. cout<<endl<<setw(n+1)<<ans;
  46. }
  47. /*
  48. Sample input:
  49. 12345 120
  50. Sample output:
  51. 12345
  52. x 120
  53. --------
  54. 0
  55. 24690
  56. +12345
  57. --------
  58. 1481400
  59. */
  60. /*
  61. Additional input:
  62. 12479 9347
  63. Additional output:
  64. 12479
  65. x 9347
  66. ----------
  67. 87353
  68. 49916
  69. 37437
  70. +112311
  71. ----------
  72. 116641213
  73. */

022.查找数列

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. #include<tuple>
  5. using namespace std;
  6. tuple<int,int> sum(int n){
  7. int i=1,k;
  8. while(((i*(i+1))/2)<n){
  9. ++i;
  10. }
  11. i=i-1;
  12. k=(i*(i+1))/2;
  13. return make_tuple(i+1,k);
  14. }
  15. int main(){
  16. int n,k;
  17. cin>>n;
  18. tuple<int,int> result=sum(n);
  19. int a[get<0>(result)];
  20. for(int j=0;j<get<0>(result);++j){
  21. a[j]=j+1;
  22. }
  23. k=get<1>(result);
  24. cout<<a[n-k-1];
  25. }
  26. /*
  27. Sample input:
  28. 14
  29. Sample output:
  30. 4
  31. */
  32. /*
  33. Additional input:
  34. 45
  35. Additional output:
  36. 9
  37. */

023.毕达哥拉斯三元组

  1. //Difficulty: 1/10
  2. //Importance: 1/10
  3. #include<iostream>
  4. using namespace std;
  5. int main(){
  6. int n,a,b,c;
  7. cin>>n;
  8. for(a=0;a<n/4;a++){
  9. for(b=n/4+1;b>n/4&&b<n/2;b++){
  10. c=n-a-b;
  11. if(a*a+b*b==c*c&&c>b){
  12. cout<<a*b*c;
  13. }
  14. }
  15. }
  16. }
  17. /*
  18. Sample input:
  19. 1000
  20. Sample output:
  21. 31875000
  22. */
  23. /*
  24. Additional input:
  25. 42360
  26. Additional output:
  27. 1101603584441138216
  28. */

024.余数和

  1. //Difficulty: 1/10
  2. //Importance: 1/10
  3. #include<iostream>
  4. using namespace std;
  5. int main(){
  6. int n,k,s,sum=0;
  7. cin>>n>>k;
  8. for(int i=0;i<n;++i){
  9. s=k%(i+1);
  10. sum+=s;
  11. }
  12. cout<<sum;
  13. }
  14. /*
  15. Sample input:
  16. 10 5
  17. Sample output:
  18. 29
  19. */
  20. /*
  21. Additional input:
  22. 45 2
  23. Additional output:
  24. 86
  25. */

025.最大数字

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. using namespace std;
  5. int wid(int n){
  6. int i=0;
  7. if(n==0)return 1;
  8. else{
  9. while(n){
  10. n=n/10;
  11. i++;
  12. }
  13. return i;
  14. }
  15. }
  16. int main(){
  17. int n,m,m0,f,l,w;
  18. cin>>n;
  19. m=n;
  20. m0=m;
  21. for(;m0>=0;--m0){
  22. int sum=0;
  23. m=m0;
  24. w=wid(m);
  25. for(int i=0;i<w-1;++i){
  26. l=m%10;
  27. m=m/10;
  28. f=m%10;
  29. if (f<=l) sum++;
  30. }
  31. if (sum==w-1){
  32. cout<<m0;
  33. break;
  34. }
  35. }
  36. }
  37. /*
  38. Sample input:
  39. 200
  40. Sample output:
  41. 199
  42. */
  43. /*
  44. Additional input:
  45. 246545
  46. Additional output:
  47. 245999
  48. */

026.倒水

note:

1.很重要,对于真正想学好编程的人来说,而不是只为了那点学分对应的绩点;

2.非常经典的BFS(广度优先遍历)算法题,思路建议直接CSDN搜索C++倒水,好好了解;

  1. //Difficulty: 5/10
  2. //Importance:10/10
  3. //a classic BFS
  4. #include <iostream>
  5. #include <queue>
  6. #include <set>
  7. #include <array>
  8. using namespace std;
  9. int main(){
  10. int m,n,d;
  11. cin>>m>>n>>d;
  12. queue<array<int,2>> q;
  13. set<array<int,2>>visited;
  14. q.push({0,0});
  15. visited.insert({0,0});
  16. int minop=0;
  17. while(!q.empty()){
  18. int size=q.size();
  19. for(int i=0;i<size;++i){
  20. array<int,2> curr=q.front();
  21. q.pop();
  22. int x = curr[0];
  23. int y = curr[1];
  24. if (x == d || y == d) {
  25. cout <<minop<<endl;
  26. return 0;
  27. }
  28. array<int, 2> next;
  29. //fill m
  30. next = {m, y};
  31. if (visited.find(next) == visited.end()) {
  32. q.push(next);
  33. visited.insert(next);
  34. }
  35. //fill n
  36. next = {x, n};
  37. if (visited.find(next) == visited.end()) {
  38. q.push(next);
  39. visited.insert(next);
  40. }
  41. //empty m
  42. next={0,y};
  43. if(visited.find(next)==visited.end()){
  44. q.push(next);
  45. visited.insert(next);
  46. }
  47. //empty n
  48. next={x,0};
  49. if(visited.find(next)==visited.end()){
  50. q.push(next);
  51. visited.insert(next);
  52. }
  53. //m to n
  54. int pourAmount1 = std::min(x, n - y);
  55. next = {x - pourAmount1, y + pourAmount1};
  56. if (visited.find(next) == visited.end()) {
  57. q.push(next);
  58. visited.insert(next);
  59. }
  60. //n to m
  61. int pourAmount2 = std::min(y, m-x);
  62. next = {x + pourAmount2, y - pourAmount2};
  63. if (visited.find(next) == visited.end()) {
  64. q.push(next);
  65. visited.insert(next);
  66. }
  67. }minop++;
  68. }
  69. }
  70. /*
  71. Sample input:
  72. 3 5 4
  73. Sample output:
  74. 6
  75. */
  76. /*
  77. Additional input:
  78. 5 9 7
  79. Additional output:
  80. 12
  81. */
  82. /*
  83. Additional input:
  84. 28 17 13
  85. Additional output:
  86. 26
  87. */

027.好数字

  1. //Difficulty: 3/10
  2. //Importance: 3/10
  3. //fastmod algorithm
  4. #include<iostream>
  5. typedef long long ll;
  6. using namespace std;
  7. ll fastmod(ll base,ll power){
  8. ll m=1000000007,res=1;
  9. while(power){
  10. if(power&1){
  11. res=(res*base)%m;
  12. }
  13. base=(base*base)%m;
  14. power>>=1;
  15. }
  16. return res;
  17. }
  18. int main(){
  19. ll n,evenCount,oddCount,ans,m=1000000007;
  20. cin>>n;
  21. evenCount=fastmod(5,(n+1)/2);
  22. //each even point corresponds to 5 possibilities(5 even numbers:0,2,4,6,8)
  23. //occurtimes=(n+1)/2
  24. oddCount=fastmod(4,n/2);
  25. //each odd point corresponds to 4 possibilities(4 prime numbers:2,3,5,7)
  26. //occurtimes=n/2
  27. ans=(evenCount*oddCount)%m;
  28. cout<<ans;
  29. }
  30. /*
  31. Sample input:
  32. 1
  33. Sample output:
  34. 5
  35. */
  36. /*
  37. Additional input:
  38. 4
  39. Additional output:
  40. 400
  41. */

028.俄罗斯农夫算法

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. using namespace std;
  5. int main(){
  6. int a,b,sum=0;
  7. cin>>a>>b;
  8. while(a>0) {
  9. cout << a << ' ' << b<<endl;
  10. if (a&1) {
  11. sum += b;
  12. }
  13. a /= 2;
  14. b *= 2;
  15. }
  16. cout<<sum;
  17. }
  18. /*
  19. Sample input:
  20. 11 3
  21. Sample output:
  22. 11 3
  23. 5 6
  24. 2 12
  25. 1 24
  26. 33
  27. */
  28. /*
  29. Additional input:
  30. 54 67
  31. Additional output:
  32. 54 67
  33. 27 134
  34. 13 268
  35. 6 536
  36. 3 1072
  37. 1 2144
  38. 3618
  39. */

029.阶乘倍数

note:

1.很重要;

2.二分查找+线性筛优化,防止TE;

  1. //Difficulty: 5/10
  2. //Importance:10/10
  3. //TE warning!
  4. //use binary search and transformation
  5. //never simply use violent factorial(too slow)
  6. #include<iostream>
  7. #include<vector>
  8. using namespace std;
  9. bool check(long long mid, const vector<long long>& primes, const vector<long long>& powers) {
  10. for (int i = 0; i < primes.size(); i++) {
  11. long long cnt = 0, p = primes[i];
  12. while (mid >= p) {
  13. cnt += mid / p;
  14. p *= primes[i];
  15. }
  16. if (cnt < powers[i]) {
  17. return false;
  18. }
  19. }
  20. return true;
  21. }
  22. int main() {
  23. long long k;
  24. cin >> k;
  25. vector<long long> primes, powers;
  26. for (long long i = 2; i * i <= k; i++) {
  27. if (k % i == 0) {
  28. long long cnt = 0;
  29. while (k % i == 0) {
  30. k /= i;
  31. cnt++;
  32. }
  33. primes.push_back(i);
  34. powers.push_back(cnt);
  35. }
  36. }
  37. if (k > 1) {
  38. primes.push_back(k);
  39. powers.push_back(1);
  40. }
  41. long long left = 1, right = 1e18, ans = -1;
  42. while (left <= right) {
  43. long long mid = (left + right) / 2;
  44. if (check(mid, primes, powers)) {
  45. ans = mid;
  46. right = mid - 1;
  47. }
  48. else {
  49. left = mid + 1;
  50. }
  51. }
  52. cout << ans << endl;
  53. }
  54. /*
  55. Sample input:
  56. 30
  57. Sample output:
  58. 5
  59. */
  60. /*
  61. Additional input:
  62. 5195
  63. Additional output:
  64. 1039
  65. */
  66. /*
  67. Additional input:
  68. 1455
  69. Additional output:
  70. 97
  71. */

030.方案数

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. using namespace std;
  5. int main() {
  6. int n, sum = 0;
  7. cin >> n;
  8. int m;
  9. double a;
  10. for (m = 1; m * m< 2 * n; ++m) {
  11. a = (double)n / (double)m - (double)m * 0.5 + 0.5;
  12. if (a - (int)a == 0) {
  13. sum++;
  14. }
  15. }
  16. cout << sum;
  17. }
  18. /*
  19. Sample input:
  20. 9
  21. Sample output:
  22. 3
  23. */
  24. /*
  25. Additional input:
  26. 757
  27. Additional output:
  28. 2
  29. */

第4季(031-040)

031.素数

note:

1.很重要;

2.线性筛最好要会,多看点B站介绍,CSDN文章,把它搞懂;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <cstring>
  5. using namespace std;
  6. int primecount(int n){
  7. bool isprime[n+1];
  8. int prime[n+1];
  9. int cnt=0;
  10. memset(isprime,true,sizeof(isprime));
  11. isprime[1]=false;
  12. for(int i=2;i<=n;++i){
  13. if(isprime[i]) prime[++cnt]=i;
  14. for(int j=1;j<=cnt && i*prime[j]<=n;++j){
  15. isprime[i*prime[j]]=false;
  16. if(i%prime[j]==0) break;
  17. }
  18. }
  19. return cnt;
  20. }
  21. int main() {
  22. int a,b;
  23. cin>>a>>b;
  24. int cnt1=primecount(a-1);
  25. int cnt2=primecount(b);
  26. cout<<cnt2-cnt1;
  27. }
  28. /*
  29. Sample input:
  30. 10 100
  31. Sample output:
  32. 21
  33. */
  34. /*
  35. Additional input:
  36. 19 41455
  37. Additional output:
  38. 4329
  39. */

032.基思数

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <vector>
  5. #include<algorithm>
  6. using namespace std;
  7. inline bool IsKeith(int N){
  8. int tmp=N;
  9. vector<int>sequence;
  10. while(tmp){
  11. sequence.push_back(tmp%10);
  12. tmp/=10;
  13. }
  14. reverse(sequence.begin(),sequence.end());
  15. int num=sequence.size()-1;
  16. int sum = 0;
  17. while(sum<N){
  18. sum=0;
  19. for(int i=num;i>=0;--i){
  20. sum+=sequence[i];
  21. }
  22. sequence.erase(sequence.begin());
  23. sequence.push_back(sum);
  24. if(sum==N) return true;
  25. }
  26. return false;
  27. }
  28. int main(){
  29. int N;
  30. cin>>N;
  31. if(IsKeith(N)) cout<<"Yes"<<endl;
  32. else cout<<"No"<<endl;
  33. }
  34. /*
  35. Sample input:
  36. 197
  37. Sample output:
  38. Yes
  39. */
  40. /*
  41. Additional input:
  42. 891
  43. Additional output:
  44. No
  45. */

033.幂函数【C++】

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. #include<iomanip>
  5. using namespace std;
  6. double pow(double base,int exp=2){
  7. double n=base;
  8. for(;exp>1;--exp){
  9. base*=n;
  10. }
  11. return base;
  12. }
  13. int main(){
  14. double base;
  15. int exp;
  16. cin>>base>>exp;
  17. cout<<fixed<<setprecision(6)<<pow(base)<<endl
  18. <<fixed<<setprecision(6)<<pow(base,exp);
  19. }
  20. /*
  21. Sample input:
  22. 1.23 5
  23. Sample output:
  24. 1.512900
  25. 2.815306
  26. */
  27. /*
  28. Additional input:
  29. 1.01 365
  30. Additional output:
  31. 1.020100
  32. 37.783434
  33. */

034.体积计算器【C++】

note:

1.一切尽在注释中,好吧;

2.题目里的顺序都有问题,直接参考我的代码就行;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. #include<cmath>
  5. #include<tuple>
  6. #include<cstring>
  7. #include<iomanip>
  8. #define pi 3.1415926
  9. /*
  10. pay attention!!! here noj define pi as 3.1415926
  11. instead of 3.141592654(which is more precise),
  12. you MUST define it exactly 3.1415926!!!
  13. */
  14. using namespace std;
  15. /* noj is likely to pay no attention to return type "double",
  16. so don't add ".0" after the int data in your calculation either,
  17. even actually this is important(obviously this is his fault,
  18. clion would even give you warnings if you don't do so,
  19. this make me thought: is noj using a rubbish IDE?)
  20. */
  21. double v(double r){
  22. return 4*pi*r*r*r/3;
  23. }
  24. double v(double r,double h){
  25. return pi*r*r*h/3;
  26. }
  27. double v(long a){
  28. return a*a*a/1;
  29. }
  30. double v(double r,long h){
  31. return pi*r*r*h/1;
  32. }
  33. double v(long l,long w,long h){
  34. return l*w*h;
  35. }
  36. /*
  37. if you are careful enough,you may fing that the
  38. following two examples are in reverse order in
  39. the stem and in the chart, this is also noj's mistake.
  40. */
  41. tuple <double,double> v(double r,double R,double h){
  42. double m=sqrt(R*R-r*r);
  43. double v1,v2;
  44. double h1=R+m;
  45. v1=(pi/3.0)*(3.0*R-h1)*h1*h1;
  46. double h2=R-m;
  47. v2=(pi/3.0)*(3.0*R-h2)*h2*h2;
  48. return make_tuple(v1,v2);
  49. }
  50. tuple <double,double> v(double r,double R,double h,double l){
  51. double m=sqrt(R*R-r*r);
  52. double v0,v1,v2;
  53. v0=pi*r*r*l;
  54. double h1=R+m;
  55. v1=v0+(pi/3.0)*(3.0*R-h1)*h1*h1*2;
  56. double h2=R-m;
  57. v2=v0+(pi/3.0)*(3.0*R-h2)*h2*h2*2;
  58. return make_tuple(v1,v2);
  59. }
  60. double v(long r,double R,double h){
  61. return pi*h*(r*r+R*R+R*r)/3;
  62. }
  63. double v(double a,double b,long c){
  64. return 4*pi*a*b*c/3;
  65. }
  66. double v(long a,long h){
  67. return a*a*h/3;
  68. }
  69. double v(long d1,long d2,double l){
  70. return pi*(d1*d1-d2*d2)*l/4;
  71. }
  72. int main(){
  73. bool z[12];
  74. memset(z, false, sizeof(z));
  75. double s[12],six[2],seven[2];
  76. int t;
  77. cin>>t;
  78. for(int i=0;i<t;++i) {
  79. int n;
  80. cin>>n;
  81. z[n]=true;
  82. if(n==1){
  83. double r;
  84. cin>>r;
  85. s[n]=v(r);
  86. }
  87. else if(n==2){
  88. double r,h;
  89. cin>>r>>h;
  90. s[n]=v(r,h);
  91. }
  92. else if(n==3){
  93. long a;
  94. cin>>a;
  95. s[n]=v(a);
  96. }
  97. else if(n==4){
  98. double r;
  99. long h;
  100. cin>>r>>h;
  101. s[n]=v(r,h);
  102. }
  103. else if(n==5){
  104. long l,w,h;
  105. cin>>l>>w>>h;
  106. s[n]=v(l,w,h);
  107. }
  108. else if(n==6){
  109. double r,R,h=0,l;
  110. cin>>r>>R>>l;
  111. tuple <double,double>result=v(r,R,h,l);
  112. six[0]=get<0>(result);
  113. six[1]=get<1>(result);
  114. }
  115. else if(n==7){
  116. double r,R,h=0;
  117. cin>>r>>R;
  118. tuple <double,double>result=v(r,R,h);
  119. seven[0]=get<0>(result);
  120. seven[1]=get<1>(result);
  121. }
  122. else if(n==8){
  123. long r;
  124. double R,h;
  125. cin>>r>>R>>h;
  126. s[n]=v(r,R,h);
  127. }
  128. else if(n==9){
  129. double a,b;
  130. long c;
  131. cin>>a>>b>>c;
  132. s[n]=v(a,b,c);
  133. }
  134. else if(n==10){
  135. long a,h;
  136. cin>>a>>h;
  137. s[n]=v(a,h);
  138. }
  139. else if(n==11){
  140. long d1,d2;
  141. double l;
  142. cin>>d1>>d2>>l;
  143. s[n]=v(d1,d2,l);
  144. }
  145. }
  146. /*
  147. in fact,you need all you outcomes outcome together,
  148. but not beyond my imagination,noj STILL had no description of it...
  149. */
  150. for(int j=1;j<=5;++j){
  151. if(z[j]) cout<<fixed<<setprecision(6)<<s[j]<<endl;
  152. }
  153. if(z[6]) cout<<six[0]<<' '<<six[1]<<endl;
  154. if(z[7]) cout<<seven[0]<<' '<<seven[1]<<endl;
  155. for(int j=8;j<=11;++j){
  156. if(z[j]) cout<<s[j]<<endl;
  157. }
  158. /*
  159. noj had no description of demand on precision,
  160. but noj had it in fact,I'm tired from bottom heart...
  161. */
  162. }
  163. /*
  164. Sample input:
  165. 11
  166. 1 3
  167. 2 3 5
  168. 3 6
  169. 4 5 12
  170. 5 1 2 3
  171. 6 5 7 12
  172. 7 5 7
  173. 8 3 4 5
  174. 9 6 7 8
  175. 10 5 10
  176. 11 12 8 6
  177. Sample output:
  178. 113.097334
  179. 47.123889
  180. 216.000000
  181. 942.477780
  182. 6.000000
  183. 3641.261807 1117.203784
  184. 1349.392014 87.363002
  185. 193.731544
  186. 1407.433485
  187. 83.000000
  188. 376.991112
  189. */
  190. /*
  191. Additional input:
  192. 11
  193. 1 8
  194. 2 7 4
  195. 3 9
  196. 4 7 11
  197. 5 1 5 6
  198. 6 5 6 13
  199. 7 5 9
  200. 8 3 5 5
  201. 9 6 9 8
  202. 10 5 13
  203. 11 12 5 4
  204. Additional output:
  205. 113.097334
  206. 47.123889
  207. 216.000000
  208. 942.477780
  209. 6.000000
  210. 3641.261807 1117.203784
  211. 1349.392014 87.363002
  212. 193.731544
  213. 1407.433485
  214. 83.000000
  215. 38390.261572
  216. */

035.可变参数平均

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <cstdarg>
  5. #include <iomanip>
  6. using namespace std;
  7. double avg(int cnt,...){
  8. va_list nums;
  9. double sum=0;
  10. va_start(nums,cnt);
  11. for(int i=0;i<cnt;++i){
  12. sum+=va_arg(nums,double);
  13. }
  14. va_end(nums);
  15. double result=sum/cnt;
  16. return result;
  17. }
  18. int main(){
  19. double a,b,c,d,e;
  20. cin>>a>>b>>c>>d>>e;
  21. cout<<fixed<<setprecision(4)<<avg(2,a,b)-avg(3,c,d,e);
  22. }
  23. /*
  24. Sample input:
  25. 1 2 3 4 5 6
  26. Sample output:
  27. -2.5000
  28. */
  29. /*
  30. Additional input:
  31. 3 645 14 662 7
  32. Additional output:
  33. 96.3333
  34. */

036.可变参数累加

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <cstdarg>
  5. using namespace std;
  6. int pl(int first,...){
  7. va_list nums;
  8. va_start(nums,first);
  9. int cnt=1;
  10. while(va_arg(nums,int)){
  11. cnt++;
  12. }
  13. va_end(nums);
  14. int sum=first;
  15. va_start(nums,first);
  16. for(int i=1;i<cnt;++i){
  17. sum+=va_arg(nums,int);
  18. }
  19. va_end(nums);
  20. return sum;
  21. }
  22. int main(){
  23. int a,b,c,d,e,f;
  24. cin>>a>>b>>c>>d>>e>>f;
  25. cout<<pl(a,b,0)-pl(c,d,e,f,0);
  26. }
  27. /*
  28. Sample input:
  29. 1 2 3 4 5 6
  30. Sample output:
  31. -15
  32. */
  33. /*
  34. Additional input:
  35. 123 5624 2645 167 3427 12354
  36. Additional output:
  37. -12846
  38. */

037.运动会

note:

1.突破口:建立平面直角坐标系,利用斜率的唯一性;

2.又用到了线性筛(aka欧拉筛),我说啥来着?

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. int phiEuler(int n){
  6. int phi[n+1],prime[n+1];
  7. bool isSieved[n+1];
  8. int sum = 0,cnt = 1, comp;
  9. prime[0] = 1;
  10. phi[1] = 1;
  11. for (int i = 2; i < n; ++i){
  12. if (!isSieved[i]){
  13. prime[cnt++] = i;
  14. phi[i] = i-1;
  15. }
  16. for (int j = 1; i*prime[j] <= n; ++j){
  17. comp = i*prime[j];
  18. isSieved[comp] = true;
  19. if (i%prime[j] == 0){
  20. phi[comp] = prime[j]*phi[i];
  21. break;
  22. } else{
  23. phi[comp] = (prime[j]-1)*phi[i];
  24. }
  25. }
  26. }
  27. for (int i = 1; i <= n-1; ++i) {
  28. sum += phi[i];
  29. }
  30. return sum;
  31. }
  32. int main() {
  33. int n, num;
  34. cin>>n;
  35. num = n == 1 ? 0 : (2 * phiEuler(n) + 1);
  36. cout<<num;
  37. }
  38. /*
  39. Sample input:
  40. 4
  41. Sample output:
  42. 9
  43. */
  44. /*
  45. Additional input:
  46. 99
  47. Additional output:
  48. 71315
  49. */

038.光线追踪

note:

1.作为小fw,我搞懂真正逻辑花了很久,甚至现在已经忘了,彻底搞不明白了;

2.我能告诉你的只有:得到每一步的结果和辗转相除法的过程极其相似,这就是用gcd的原因;同时正三角形三条边的对称性使得最后乘3;

3.真优美啊,一辈子也永远理解不了的优美;(类目.jpg)

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. using namespace std;
  5. unsigned int gcd(unsigned int a, unsigned int b){
  6. if (b == 0) return a;
  7. return gcd(b,a%b);
  8. }
  9. int main(){
  10. unsigned int n,x,l;
  11. cin>>n>>x;
  12. l = 3*(n-gcd(n,x));
  13. cout<<l;
  14. }
  15. /*
  16. Sample input:
  17. 5 2
  18. Sample output:
  19. 12
  20. */
  21. /*
  22. Additional input:
  23. 5 3
  24. Additional output:
  25. 12
  26. */
  27. /*
  28. Additional input:
  29. 199 31
  30. Additional output:
  31. 594
  32. */

039.佩尔数

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. using namespace std;
  5. int PA(int n){
  6. if(n==0) return 0;
  7. else if(n==1) return 1;
  8. return 2*PA(n-1)+PA(n-2);
  9. }
  10. int PB(int n){
  11. int p0=0,p1=1,pn,i;
  12. for (i=0;i<=n;++i){
  13. if (i==0) pn=p0;
  14. else if (i==1) pn=p1;
  15. else{
  16. pn=2*p1+p0;
  17. p0=p1;
  18. p1=pn;
  19. }
  20. }
  21. return pn;
  22. }
  23. int main(){
  24. int n;
  25. cin>>n;
  26. if(n&1) cout<<PA(n);
  27. else cout<<PB(n);
  28. }
  29. /*
  30. Sample input:
  31. 10
  32. Sample output:
  33. 2378
  34. */
  35. /*
  36. Additional input:
  37. 42
  38. Additional output:
  39. 339564650
  40. */

040.哈沙德数

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. using namespace std;
  5. inline int hn(int n){
  6. int t=n,s=0;
  7. while (t){
  8. s+=t%10;
  9. t/=10;
  10. }
  11. if ((s == 0) || (n%s != 0)) return 0;
  12. if (s == 1) return 1;
  13. return n/s;
  14. }
  15. int main(){
  16. int cnt = 0, n;
  17. cin>>n;
  18. if (n == 1) cnt = 1;
  19. while ((n != 0) && (n != 1)) {
  20. n = hn(n);
  21. if (n) ++cnt;
  22. }
  23. cout<<cnt;
  24. }
  25. /*
  26. Sample input:
  27. 6804
  28. Sample output:
  29. 4
  30. */
  31. /*
  32. Additional input:
  33. 1048
  34. Additional output:
  35. 0
  36. */

第5季(041-050)

041.完美矩阵

note: 

1.别看noj题目里面矩阵矩阵的,实际上就是装,蒙骗你;最后就是判断方阵,正方形,懂么;

2.前缀和基础;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. //这道题用正常方法暴力判断逻辑上成立,但是肯定超时;
  4. //下面的程序做了一点优化,实测34ms(再改改细节说不定能到30ms);
  5. //修正题目:子矩阵必须是方阵(noj又错了,气死);
  6. //规定1:一个矩阵的记号为[(x1,y1),(x2,y2)],即左上角和右下角的两个值;
  7. //规定2:记号s[(x1,y1),(x2,y2)]为矩阵[(x1,y1),(x2,y2)]内所有元素的和;
  8. //翻译题目:首先对要输入的矩阵进行处理,将所有0换成-1,简化运算;
  9. //简化之后:条件(2)转化成 s[(x1,y1),(x2,y2)]-s[(x1+1,y1+1),(x2-1,y2-1)]=2(y2-y1+x2-x1) 可类比矩形周长理解;
  10. // 上面一条仅针对 行数>=3且列数>=3
  11. //简化之后:条件(3)转化成 s[(x1,y1),(x2,y2)]=-1,0,1;
  12. //注意程序里面有多个函数需要传多个相同参数,因此最好确保顺序统一;
  13. #include <iostream>
  14. using namespace std;
  15. static int arr[301][301]; //arr为转换之后的矩阵;
  16. static int preSum[301][301]; //preSum为前缀和运算后得到的矩阵;
  17. //此处两个数组要用static修饰并在main外部定义,作为全局静态参量,便于调用;
  18. void prefixSum(int n,int m) {
  19. for (int i = 1; i <= n; ++i) {
  20. for (int j = 1; j <= m; ++j) {
  21. preSum[i][j] = arr[i][j] + preSum[i-1][j] + preSum[i][j-1] - preSum[i-1][j-1];
  22. }
  23. }
  24. }//prefixSum为前缀和算法,用于快速计算某个二维数列子矩阵的s[(1,1),(x2,y2)],结果存储在preSum中;
  25. int getSum(int x1, int y1, int x2, int y2) {
  26. return preSum[x2][y2]-preSum[x1-1][y2]-preSum[x2][y1-1]
  27. +preSum[x1-1][y1-1];
  28. }//getSum利用prefixSum和容斥原理得到任意的s[(x1,y1),(x2,y2)];
  29. bool isPerfectMatrix(int x1,int y1,int x2,int y2){
  30. int sum1=getSum(x1,y1,x2,y2); //sum1用来存整个子矩阵之和,sum2用来存子矩阵内层之和;
  31. if((x2-x1)==1||(y2-y1)==1){ //即2x2的情况,必须单独考虑,
  32. if(sum1==4) return true; //因为这种情况下getSum里的参数实际上会相等,
  33. else return false; //导致getSum的运算失去意义;
  34. }
  35. else{
  36. int sum2=getSum(x1+1,y1+1,x2-1,y2-1);
  37. if (sum2==-1||sum2==0||sum2==1){ //先判断简单的条件(3),优化一下;
  38. int c=2*(y2-y1+x2-x1);
  39. if(sum1-sum2==c) return true; //判断条件(2);
  40. else return false;
  41. }
  42. else return false;
  43. }
  44. }
  45. int PerfectMatrixCount(int n,int m){
  46. int cnt=0;
  47. for(int i=1;i<=n;++i){
  48. for(int j=1;j<=m;++j){
  49. for(int k=1;i+k<=n&&j+k<=m;++k){ //记得开头说的子矩阵必须是方阵;
  50. if (arr[i][k + j] == -1 || arr[k + i][j] == -1) break;
  51. //此处为优化部分,发现扩展遍历时遇到元素为0,那么该方向的扩展均不可能为完美矩阵,
  52. //因为扩展的最外圈总会包含这个0,至于为什么是==-1,因为矩阵转换过了;
  53. if (isPerfectMatrix(i,j,i+k,j+k)) {
  54. ++cnt;
  55. }
  56. }
  57. }
  58. }
  59. return cnt;
  60. }
  61. int main(){
  62. int n,m;
  63. cin>>n>>m;
  64. for(int i=1;i<=n;++i){
  65. for(int j=1;j<=m;++j){
  66. int tmp;
  67. cin>>tmp;
  68. if (tmp==1) arr[i][j]=1;
  69. else arr[i][j]=-1; //此处完成0-1矩阵到(-1)-1矩阵的转换;
  70. }
  71. }
  72. prefixSum(n,m);
  73. if(n<2||m<2) cout<<0; //其实多余,但是为了完整,还是放在这边;
  74. else cout<<PerfectMatrixCount(n,m);
  75. }
  76. /*
  77. Sample input:
  78. 4 4
  79. 1 1 1 1
  80. 1 0 1 1
  81. 1 1 0 1
  82. 1 1 1 1
  83. Sample output:
  84. 3
  85. */
  86. /*
  87. Additional input:
  88. 5 5
  89. 1 0 1 0 1
  90. 1 1 0 1 1
  91. 1 1 1 1 1
  92. 0 1 1 0 0
  93. 1 0 1 1 1
  94. Additional output:
  95. 3
  96. */

042.【专业融合:航空】飞机起飞速度

note:

1.这是第一道无人AC的题;

2.给出一个版本的代码,不能AC的,样例都跑不出来;但这题的样例似乎就有问题;

  1. //Difficulty:??/10
  2. //Importance: 0/10
  3. //1st none AC you meet
  4. #include <iostream>
  5. #include <cmath>
  6. using namespace std;
  7. double calculateSpeed(double temperature, double pressure, int elevation, int runway, int weight, int flaps, int wet) {
  8. // 检查输入是否在有效范围内
  9. if (flaps != 1 && flaps != 5 && flaps != 15) {
  10. return -1;
  11. }
  12. if (weight < 41413 || weight > 65000 || runway <= 6900) {
  13. return -1;
  14. }
  15. // 计算温度档和气压档
  16. int tempRange = floor(temperature / 10);
  17. int pressureRange = ceil(pressure);
  18. // 检查操纵参考表是否存在
  19. if (tempRange < 0 || tempRange > 7 || pressureRange < 0 || pressureRange > 9) {
  20. return -1;
  21. }
  22. // 根据温度档和气压档查找操纵参考值
  23. char referenceTable[8][10] = {
  24. {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K'},
  25. {'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V'},
  26. {'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F'},
  27. {'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R'},
  28. {'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B'},
  29. {'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M'},
  30. {'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X'},
  31. {'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'}
  32. };
  33. char reference = referenceTable[tempRange][pressureRange];
  34. // 检查操纵参考表是否存在V1、Vr和V2
  35. if (reference != 'A' && reference != 'B' && reference != 'C' && reference != 'D' && reference != 'E') {
  36. return -1;
  37. }
  38. // 根据襟翼位置、起飞重量和操纵参考值查找V1、Vr和V2
  39. int speedTable[3][5] = {
  40. {117, 126, 134, 142, 151},
  41. {122, 131, 139, 147, 156},
  42. {127, 136, 145, 153, 162}
  43. };
  44. int speedIndex = (flaps - 1) / 7;
  45. int* speedRow = speedTable[speedIndex];
  46. int v1 = speedRow[weight / 13000];
  47. int vr = speedRow[weight / 13000] + 11;
  48. int v2 = speedRow[weight / 13000] + 18;
  49. // 如果是湿跑道,根据跑道长度和襟翼位置查找折扣值
  50. if (wet == 1) {
  51. int discountTable[3][3] = {
  52. {0, 0, 0},
  53. {0, 0, 0},
  54. {0, 0, 0}
  55. };
  56. int discountIndex = (flaps - 1) / 7;
  57. int* discountRow = discountTable[discountIndex];
  58. int discount = discountRow[runway / 1000];
  59. v1 -= discount;
  60. }
  61. printf("V1=%dkts Vr=%dkts V2=%dkts\n", v1, vr, v2);
  62. return 0;
  63. }
  64. int main() {
  65. double temperature, pressure;
  66. int elevation, runway, weight, flaps, wet;
  67. cin>>temperature>>pressure>>elevation>>runway>>weight>>flaps>>wet;
  68. int result = calculateSpeed(temperature, pressure, elevation, runway, weight, flaps, wet);
  69. if (result == -1) {
  70. cout<<"Flight not possible!\n";
  71. }
  72. return 0;
  73. }
  74. /*
  75. Sample input:
  76. 23,8 28.67 1200 7250 52600 5 1
  77. Sample output:
  78. V1=128kts Vr=139kts V2=146kts
  79. */
  80. /*
  81. Additional input:
  82. NULL
  83. Additional output:
  84. NULL
  85. */

043.【专业融合:数学】行列式值

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #define MAXSIZE 10
  5. using namespace std;
  6. void swap(double arr[MAXSIZE][MAXSIZE], int r1, int r2, int n) {
  7. for (int i = 0; i < n; i++) {
  8. double temp = arr[r1][i];
  9. arr[r1][i] = arr[r2][i];
  10. arr[r2][i] = temp;
  11. }
  12. }
  13. double calDet(double arr[MAXSIZE][MAXSIZE], int n) {
  14. int i, j, k;
  15. double det = 1.0;
  16. for (i = 0; i < n; i++) {
  17. if (arr[i][i] == 0.0) {
  18. for (j = i + 1; j < n; j++) {
  19. if (arr[j][i] != 0.0) {
  20. swap(arr, i, j, n);
  21. det *= -1.0;
  22. break;
  23. }
  24. }
  25. }
  26. if (arr[i][i] == 0.0) {
  27. return 0.0;
  28. }
  29. double v = arr[i][i];
  30. det *= v;
  31. for (j = i; j < n; j++) {
  32. arr[i][j] /= v;
  33. }
  34. for (j = i + 1; j < n; j++) {
  35. double e = arr[j][i];
  36. for (k = i; k < n; k++) {
  37. arr[j][k] -= e * arr[i][k];
  38. }
  39. }
  40. }
  41. return det;
  42. }
  43. int main() {
  44. int n;
  45. cin>>n;
  46. double arr[MAXSIZE][MAXSIZE];
  47. for (int i = 0; i < n; i++) {
  48. for (int j = 0; j < n; j++) {
  49. cin>>arr[i][j];
  50. }
  51. }
  52. double det = calDet(arr, n);
  53. cout<<det;
  54. return 0;
  55. }
  56. /*
  57. Sample input:
  58. 3
  59. 2 6 3
  60. 1 0 2
  61. 5 8 4
  62. Sample output:
  63. 28
  64. */
  65. /*
  66. Additional input:
  67. 3
  68. 8 91 1
  69. 4 15 15
  70. 61 237 16
  71. Additional output:
  72. 50954
  73. */

044.稀疏矩阵

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. int main () {
  6. int raw, col, n, num = 0;
  7. cin>>raw>>col;
  8. for (int i = 0; i < raw; ++i) {
  9. for (int j = 0; j < col; ++j) {
  10. cin>>n;
  11. if (n) ++num;
  12. }
  13. }
  14. double ratio = (double)num / (raw * col);
  15. if (num == raw || num == col || (ratio - 0.05) <= 1e-9)
  16. cout<<"Yes"<<endl;
  17. else cout<<"No"<<endl;
  18. return 0;
  19. }
  20. /*
  21. Sample input:
  22. 4 4
  23. 5 0 0 0
  24. 0 8 0 0
  25. 0 0 3 0
  26. 0 6 0 0
  27. Sample output:
  28. Yes
  29. */
  30. /*
  31. Additional input:
  32. 4 4
  33. 5 5 0 0
  34. 0 1 0 0
  35. 0 0 3 0
  36. 0 6 0 2
  37. Additional output:
  38. No
  39. */

045.【专业融合:管理】航空旅行

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. void pass(int a, int b, int c, int d, int e) {
  6. bool flag = false;
  7. if (a <= e && (b + c) <= d) flag = true;
  8. if (b <= e && (a + c) <= d) flag = true;
  9. if (c <= e && (a + b) <= d) flag = true;
  10. if (flag) cout<<"YES"<<endl;
  11. else cout<<"NO"<<endl;
  12. }
  13. int main() {
  14. int n, a, b, c, d, e;
  15. cin>>n;
  16. while (n--){
  17. cin>>a>>b>>c>>d>>e;
  18. pass(a, b, c, d, e);
  19. }
  20. return 0;
  21. }
  22. /*
  23. Sample input:
  24. 3
  25. 1 1 1 15 5
  26. 8 7 6 15 5
  27. 8 5 7 15 6
  28. Sample output:
  29. YES
  30. NO
  31. YES
  32. */
  33. /*
  34. Additional input:
  35. 3
  36. 1 4 5 5 16
  37. 15 51 16 15 15
  38. 15 61 16 16 16
  39. Additional output:
  40. YES
  41. NO
  42. NO
  43. */

046.【专业融合:管理】货运优化

note:

1.这么喜欢管理?都融合俩了;

2.这题使用贪心算法,必须掌握

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. int main() {
  6. int l3s2[4] = {0, 5, 3, 1};
  7. int n, x1, x2, x3, x4, x5, x6, s2, s1;
  8. while (1) {
  9. cin>>x1>>x2>>x3>>x4>>x5>>x6;
  10. if ((x1 + x2 + x3 + x4 + x5 + x6) == 0) break;
  11. n = (x3 + 3) / 4 + x4 + x5 + x6;
  12. s2 = 5 * x4 + l3s2[x3 % 4];
  13. if (x2 > s2) n += (x2 - s2 + 8) / 9;
  14. s1 = 36 * n - 36 * x6 - 25 * x5 - 16 * x4 - 9 * x3 - 4 * x2;
  15. if (x1 > s1) n += (x1 - s1 + 35) / 36;
  16. cout<<n<<endl;
  17. }
  18. return 0;
  19. }
  20. /*
  21. Sample input:
  22. 1 1 1 1 1 1
  23. 2 2 2 2 2 2
  24. 1 2 3 4 5 6
  25. 0 0 0 0 0 0
  26. Sample output:
  27. 4
  28. 7
  29. 16
  30. */
  31. /*
  32. Additional input:
  33. 1 5 6 2 1 6
  34. 1 6 7 1 2 7
  35. 1 2 7 19 8 6
  36. 0 0 0 0 0 0
  37. Additional output:
  38. 11
  39. 12
  40. 35
  41. */

047.回文数之和

note:

1.回文数,实验考试挺喜欢考的(似乎);

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. int dec0[10] , kSys[32] ;
  6. bool isPalindrome(int arr[], int cnt){
  7. int head = 0, tail = cnt - 1;
  8. while (head < tail) {
  9. if (arr[head] != arr[tail]) return false;
  10. ++head, --tail;
  11. }
  12. return true;
  13. }
  14. bool isBiPalindrome(int n, int k){
  15. int tmp = n, cnt = 0;
  16. while (tmp) {
  17. dec0[cnt++] = tmp % 10;
  18. tmp /= 10;
  19. }
  20. if (!isPalindrome(dec0, cnt)) return false;
  21. tmp = n, cnt = 0;
  22. while (tmp) {
  23. kSys[cnt++] = tmp % k;
  24. tmp /= k;
  25. }
  26. if (!isPalindrome(kSys, cnt)) return false;
  27. return true;
  28. }
  29. int main() {
  30. int n, k, sum = 0;
  31. cin>>n>>k;
  32. for (int i = 1; i <= n; ++i) {
  33. if (isBiPalindrome(i, k)) sum += i;
  34. }
  35. cout<<sum;
  36. return 0;
  37. }
  38. /*
  39. Sample input:
  40. 100 2
  41. Sample output:
  42. 157
  43. */
  44. /*
  45. Additional input:
  46. 1000 3
  47. Additional output:
  48. 2638
  49. */

048.【专业融合:物理】蒙特卡罗方法求积分

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <cmath>
  5. #include <cstdlib>
  6. using namespace std;
  7. double func1(double x) {
  8. return pow(x, 4) * exp(-x);
  9. }
  10. double func2(double x) {
  11. return x * x + 1;
  12. }
  13. double func3(double x) {
  14. return cos(x);
  15. }
  16. double func4(double x) {
  17. return sqrt(x) * (x - 2);
  18. }
  19. double func5(double x) {
  20. return 2 * sin(x) - 5 * cos(x);
  21. }
  22. double func(int m, double x) {
  23. switch (m) {
  24. case 1: return func1(x);
  25. case 2: return func2(x);
  26. case 3: return func3(x);
  27. case 4: return func4(x);
  28. case 5: return func5(x);
  29. default: return 0;
  30. }
  31. }
  32. double mtk(int m, double a, double b, int n) {
  33. srand(RAND_MAX);
  34. double w = b - a, sum = 0;
  35. for (int i = 1; i < n; ++i) {
  36. double x = ((double)rand() / RAND_MAX) * w + a;
  37. sum += func(m, x);
  38. }
  39. sum *= w / n;
  40. return sum;
  41. }
  42. int main() {
  43. int m, n;
  44. double a, b;
  45. cin>>m>>a>>b>>n;
  46. printf("%.6lf", mtk(m, a, b, n));
  47. return 0;
  48. }
  49. /*
  50. Sample input:
  51. 1 1 5 2000
  52. Sample output:
  53. 13.317870
  54. */
  55. /*
  56. Additional input:
  57. 3 8 7 1292
  58. Additional output:
  59. -0.331019
  60. */

049.【专业融合:计算机】波士顿房价预测

note:

1.这到底哪里融合计算机了,我真看不出来,实在扯不上能不能直接不扯关系,看着怪尴尬的;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. double nAvg(double arr[], int n) {
  6. double sum = 0;
  7. for (int i = 0; i < n; ++i) {
  8. sum += arr[i];
  9. }
  10. return sum / n;
  11. }
  12. int main() {
  13. int n;
  14. cin>>n;
  15. double x[n], y[n];
  16. for (int i = 0; i < n; ++i) {
  17. cin>>x[i]>>y[i];
  18. }
  19. double xBar = nAvg(x, n), yBar = nAvg(y, n);
  20. double sumUp = 0, sumDown = 0;
  21. for (int i = 0; i < n; ++i) {
  22. sumUp += (x[i] - xBar) * (y[i] - yBar);
  23. }
  24. for (int i = 0; i < n; ++i) {
  25. sumDown += (x[i] - xBar) * (x[i] - xBar);
  26. }
  27. double b = sumUp / sumDown;
  28. double a = yBar - b * xBar;
  29. printf("Y=%.4lf+%.4lf*X",a,b);
  30. return 0;
  31. }
  32. /*
  33. Sample input:
  34. 7
  35. 150 6450
  36. 200 7450
  37. 250 8445
  38. 300 9450
  39. 350 11450
  40. 400 15450
  41. 600 18450
  42. Sample output:
  43. Y=1770.2394+28.7793*X
  44. */
  45. /*
  46. Additional input:
  47. 5
  48. 1 1
  49. 2 2
  50. 3 3
  51. 4 4
  52. 5 5
  53. Additional output:
  54. Y=0.0000+1.0000*X
  55. */

050.素数筛选法

note:

1.这题出现的也太迟了,应该塞第2季,线性筛都用烂了;

2.下面代码我记得没用埃氏筛,直接上的欧拉筛;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include<iostream>
  4. using namespace std;
  5. #define NUM (int)1e7+1
  6. static bool isSieved[NUM];
  7. static int prime[NUM];
  8. int main() {
  9. int n, k = 0;
  10. cin>>n;
  11. isSieved[1] = true;
  12. for (int i = 2; i <= n; ++i) {
  13. if (!isSieved[i]) prime[++k] = i;
  14. for (int j = 1; prime[j] * i <= n; ++j) {
  15. isSieved[prime[j] * i] = true;
  16. if (i % prime[j] == 0) break;
  17. }
  18. }
  19. cout<<k;
  20. }
  21. /*
  22. Sample input:
  23. 100
  24. Sample output:
  25. 25
  26. */
  27. /*
  28. Additional input:
  29. 1590
  30. Additional output:
  31. 250
  32. */

第6季(051-060)

051.分离字符串

note:

1.字符串的好好搞懂吧,实验的期末抽到概率不小;

2.对string类的各种函数(原型,参数,返回值)要牢牢把握,随便列几个常用的:

        str.substr();         str.find();         str.erase();         str.find();        str.append();

        str.size();            str.length();      str.insery();        str.compare();  str.replace();

        atoi();                  stoi();               tolower();           toupper();      ......

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6. vector<string> split(string str, string pattern)
  7. {
  8. string::size_type pos;
  9. vector<string> result;
  10. str += pattern;
  11. int size = str.size();
  12. for (int i = 0; i < size; i++)
  13. {
  14. pos = str.find(pattern, i);
  15. if (pos < size)
  16. {
  17. string s = str.substr(i, pos - i);
  18. result.push_back(s);
  19. i = pos + pattern.size() - 1;
  20. }
  21. }
  22. return result;
  23. }
  24. int main(){
  25. string str;
  26. getline(cin,str);
  27. string pattern;
  28. getline(cin,pattern);
  29. vector<string> result=split(str,pattern);
  30. for(int i=0; i<result.size(); i++)
  31. {
  32. cout<<result[i]<<endl;
  33. }
  34. return 0;
  35. }
  36. /*
  37. Sample input:
  38. www.nwpu.edu.cn
  39. .
  40. Sample output:
  41. www
  42. nwpu
  43. edu
  44. cn
  45. */
  46. /*
  47. Additional input:
  48. https://www.bh3.com/
  49. .
  50. Additional output:
  51. https://www
  52. bh3
  53. com/
  54. */

052.Kids A+B

note:

1.这题真没意思,优化也优化不到哪里去,所以直接让GPT生成map了;(别以为我是慢慢敲的,叉腰.jpg)

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <map>
  5. #include <vector>
  6. using namespace std;
  7. vector<string> split(string str, string pattern)
  8. {
  9. string::size_type pos;
  10. vector<string> result;
  11. str += pattern;
  12. int size = str.size();
  13. for (int i = 0; i < size; i++)
  14. {
  15. pos = str.find(pattern, i);
  16. if (pos < size)
  17. {
  18. string s = str.substr(i, pos - i);
  19. result.push_back(s);
  20. i = pos + pattern.size() - 1;
  21. }
  22. }
  23. return result;
  24. }
  25. int stringToInt(vector<string> str){
  26. map<string, int> stringToIntMap;
  27. //个位映射
  28. stringToIntMap["zero"] = 0;
  29. stringToIntMap["one"] = 1;
  30. stringToIntMap["two"] = 2;
  31. stringToIntMap["three"] = 3;
  32. stringToIntMap["four"] = 4;
  33. stringToIntMap["five"] = 5;
  34. stringToIntMap["six"] = 6;
  35. stringToIntMap["seven"] = 7;
  36. stringToIntMap["eight"] = 8;
  37. stringToIntMap["nine"] = 9;
  38. //十位映射
  39. stringToIntMap["twenty"] = 20;
  40. stringToIntMap["thirty"] = 30;
  41. stringToIntMap["forty"] = 40;
  42. stringToIntMap["fifty"] = 50;
  43. stringToIntMap["sixty"] = 60;
  44. stringToIntMap["seventy"] = 70;
  45. stringToIntMap["eighty"] = 80;
  46. stringToIntMap["ninety"] = 90;
  47. //特殊映射(其实包含十位映射)
  48. stringToIntMap["ten"] = 10;
  49. stringToIntMap["eleven"] = 11;
  50. stringToIntMap["twelve"] = 12;
  51. stringToIntMap["thirteen"] = 13;
  52. stringToIntMap["fourteen"] = 14;
  53. stringToIntMap["fifteen"] = 15;
  54. stringToIntMap["sixteen"] = 16;
  55. stringToIntMap["seventeen"] = 17;
  56. stringToIntMap["eighteen"] = 18;
  57. stringToIntMap["nineteen"] = 19;
  58. //使用映射
  59. int result=0;
  60. if(str.size()==1){
  61. result=stringToIntMap[str[0]];
  62. }
  63. if(str.size()==2){
  64. result=stringToIntMap[str[0]]+stringToIntMap[str[1]];
  65. }
  66. return result;
  67. }
  68. int intNumWidth(int x){
  69. string str = to_string(x);
  70. return str.size();
  71. }
  72. string intToString(int num){
  73. // 肯定有好的方法,但是下面的map可以直接让ai生成,也不费时间
  74. map<int,string> intToStringMap;
  75. intToStringMap[0] = "zero";
  76. intToStringMap[1] = "one";
  77. intToStringMap[2] = "two";
  78. intToStringMap[3] = "three";
  79. intToStringMap[4] = "four";
  80. intToStringMap[5] = "five";
  81. intToStringMap[6] = "six";
  82. intToStringMap[7] = "seven";
  83. intToStringMap[8] = "eight";
  84. intToStringMap[9] = "nine";
  85. intToStringMap[10] = "ten";
  86. intToStringMap[11] = "eleven";
  87. intToStringMap[12] = "twelve";
  88. intToStringMap[13] = "thirteen";
  89. intToStringMap[14] = "fourteen";
  90. intToStringMap[15] = "fifteen";
  91. intToStringMap[16] = "sixteen";
  92. intToStringMap[17] = "seventeen";
  93. intToStringMap[18] = "eighteen";
  94. intToStringMap[19] = "nineteen";
  95. intToStringMap[20] = "twenty";
  96. intToStringMap[21] = "twenty-one";
  97. intToStringMap[22] = "twenty-two";
  98. intToStringMap[23] = "twenty-three";
  99. intToStringMap[24] = "twenty-four";
  100. intToStringMap[25] = "twenty-five";
  101. intToStringMap[26] = "twenty-six";
  102. intToStringMap[27] = "twenty-seven";
  103. intToStringMap[28] = "twenty-eight";
  104. intToStringMap[29] = "twenty-nine";
  105. intToStringMap[30] = "thirty";
  106. intToStringMap[31] = "thirty-one";
  107. intToStringMap[32] = "thirty-two";
  108. intToStringMap[33] = "thirty-three";
  109. intToStringMap[34] = "thirty-four";
  110. intToStringMap[35] = "thirty-five";
  111. intToStringMap[36] = "thirty-six";
  112. intToStringMap[37] = "thirty-seven";
  113. intToStringMap[38] = "thirty-eight";
  114. intToStringMap[39] = "thirty-nine";
  115. intToStringMap[40] = "forty";
  116. intToStringMap[41] = "forty-one";
  117. intToStringMap[42] = "forty-two";
  118. intToStringMap[43] = "forty-three";
  119. intToStringMap[44] = "forty-four";
  120. intToStringMap[45] = "forty-five";
  121. intToStringMap[46] = "forty-six";
  122. intToStringMap[47] = "forty-seven";
  123. intToStringMap[48] = "forty-eight";
  124. intToStringMap[49] = "forty-nine";
  125. intToStringMap[50] = "fifty";
  126. intToStringMap[51] = "fifty-one";
  127. intToStringMap[52] = "fifty-two";
  128. intToStringMap[53] = "fifty-three";
  129. intToStringMap[54] = "fifty-four";
  130. intToStringMap[55] = "fifty-five";
  131. intToStringMap[56] = "fifty-six";
  132. intToStringMap[57] = "fifty-seven";
  133. intToStringMap[58] = "fifty-eight";
  134. intToStringMap[59] = "fifty-nine";
  135. intToStringMap[60] = "sixty";
  136. intToStringMap[61] = "sixty-one";
  137. intToStringMap[62] = "sixty-two";
  138. intToStringMap[63] = "sixty-three";
  139. intToStringMap[64] = "sixty-four";
  140. intToStringMap[65] = "sixty-five";
  141. intToStringMap[66] = "sixty-six";
  142. intToStringMap[67] = "sixty-seven";
  143. intToStringMap[68] = "sixty-eight";
  144. intToStringMap[69] = "sixty-nine";
  145. intToStringMap[70] = "seventy";
  146. intToStringMap[71] = "seventy-one";
  147. intToStringMap[72] = "seventy-two";
  148. intToStringMap[73] = "seventy-three";
  149. intToStringMap[74] = "seventy-four";
  150. intToStringMap[75] = "seventy-five";
  151. intToStringMap[76] = "seventy-six";
  152. intToStringMap[77] = "seventy-seven";
  153. intToStringMap[78] = "seventy-eight";
  154. intToStringMap[79] = "seventy-nine";
  155. intToStringMap[80] = "eighty";
  156. intToStringMap[81] = "eighty-one";
  157. intToStringMap[82] = "eighty-two";
  158. intToStringMap[83] = "eighty-three";
  159. intToStringMap[84] = "eighty-four";
  160. intToStringMap[85] = "eighty-five";
  161. intToStringMap[86] = "eighty-six";
  162. intToStringMap[87] = "eighty-seven";
  163. intToStringMap[88] = "eighty-eight";
  164. intToStringMap[89] = "eighty-nine";
  165. intToStringMap[90] = "ninety";
  166. intToStringMap[91] = "ninety-one";
  167. intToStringMap[92] = "ninety-two";
  168. intToStringMap[93] = "ninety-three";
  169. intToStringMap[94] = "ninety-four";
  170. intToStringMap[95] = "ninety-five";
  171. intToStringMap[96] = "ninety-six";
  172. intToStringMap[97] = "ninety-seven";
  173. intToStringMap[98] = "ninety-eight";
  174. intToStringMap[99] = "ninety-nine";
  175. return intToStringMap[num];
  176. }
  177. int main() {
  178. string num1,num2;
  179. cin>>num1>>num2;
  180. vector<string> str1=split(num1,"-");
  181. vector<string> str2=split(num2,"-");
  182. int ans=0;
  183. ans=stringToInt(str1)+stringToInt(str2);
  184. cout<<intToString(ans);
  185. return 0;
  186. }
  187. /*
  188. Sample input:
  189. twenty-seven fifty-four
  190. Sample output:
  191. eighty-one
  192. */
  193. /*
  194. Additional input:
  195. zero zero
  196. Additional output:
  197. zero
  198. */

053.前后缀移除

note:

1.题目不说人话也是noj一大特色,你得好好理解到底说的什么意思;

2.遍历到chars,也就是子字符串中不含的字符,遍历终止;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. string removePre(string input, const string& std) {
  6. int pos = 0;
  7. for (auto it = input.begin(); it != input.end(); ++it) {
  8. if (std.find(*it) != string::npos) ++pos;
  9. else break;
  10. }
  11. if (pos) input.erase(0, pos);
  12. return input;
  13. }
  14. string removeSuf(string input, const string& std) {
  15. int pos = 0;
  16. for (auto it = input.rbegin(); it != input.rend(); ++it) {
  17. if (std.find(*it) != string::npos) ++pos;
  18. else break;
  19. }
  20. if (pos) input.erase(input.size()-pos, pos);
  21. return input;
  22. }
  23. int main() {
  24. string input,std;
  25. getline(cin,input);
  26. getline(cin,std);
  27. cout<<removePre(input,std)<<endl<<removeSuf(input,std)<<endl<<removeSuf(removePre(input,std),std);
  28. return 0;
  29. }
  30. /*
  31. Sample input:
  32. www.example.com
  33. cmowz.
  34. Sample output:
  35. example.com
  36. www.example
  37. example
  38. */
  39. /*
  40. Additional input:
  41. https://www.bh3.com/
  42. pthsocm/:w.
  43. Additional output:
  44. bh3.com/
  45. https://www.bh3
  46. bh3
  47. */

054.删除前后缀

note:

1.名字整的和上一题那么像,这一题更简单,但更实用;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. string removePre(string input, const string& std) {
  6. int base=0;
  7. for (auto it = input.begin(); it != input.end(); ) {
  8. if (input.substr(distance(input.begin(), it),std.size())== std){
  9. ++base;
  10. it+=std.size();
  11. }
  12. else break;
  13. }
  14. int pos=std.size()*base;
  15. if (base) input.erase(0, pos);
  16. return input;
  17. }
  18. string removeSuf(string input, const string& std) {
  19. int base=0;
  20. for (auto it = input.rbegin(); it != input.rend(); ) {
  21. if (input.substr(distance(input.rbegin(), it),std.size()) == std){
  22. ++base;
  23. it+=std.size();
  24. }
  25. else break;
  26. }
  27. int pos=std.size()*base;
  28. if (base) input.erase(input.size()-pos, pos);
  29. return input;
  30. }
  31. int main() {
  32. string input,std;
  33. getline(cin,input);
  34. getline(cin,std);
  35. cout<<removePre(input,std)<<endl<<removeSuf(input,std);
  36. return 0;
  37. }
  38. /*
  39. Sample input:
  40. antiantianwartiantianti
  41. anti
  42. Sample output:
  43. anwartiantianti
  44. antiantianwarti
  45. */
  46. /*
  47. Additional input:
  48. yuanshenyuanshenyuanshengyuanshenshenshen
  49. yuan
  50. Additional output:
  51. shenyuanshenyuanshengyuanshenshenshen
  52. yuanshenyuanshenyuanshengyuanshenshen
  53. */

055.AtoI转换

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <climits>
  5. using namespace std;
  6. int ATOI(string str) {
  7. auto it = str.begin();
  8. int sgn = 1;
  9. long long tmp = 0;
  10. if (*it == '+') ++it;
  11. else if (*it == '-') sgn = -1, ++it;
  12. while (it != str.end()) {
  13. if (*it == ' ') ;
  14. else if ('0' <= *it && *it <= '9') {
  15. tmp = (*it - '0') + tmp * 10;
  16. if ((tmp * sgn) >= INT_MAX) return INT_MAX;
  17. else if ((tmp * sgn) <= INT_MIN) return INT_MIN;
  18. }
  19. else break;
  20. ++it;
  21. }
  22. return tmp * sgn;
  23. }
  24. int main() {
  25. string str;
  26. cin>>str;
  27. cout<<ATOI(str);
  28. return 0;
  29. }
  30. /*
  31. Sample input:
  32. -123x+123
  33. Sample output:
  34. -123
  35. */
  36. /*
  37. Additional input:
  38. 00519
  39. Additional output:
  40. 519
  41. */

056.大小写交换

note:

1.ASCII码表的简单运用罢了;

2.ASCII码表建议记忆的如下:

Dec(十进制值)字符
0NUL
32空格
480
65a
97A

        上面的就够用了,实在不行直接cout<<(int)(char); 让编译器告诉你;

3.这题的本质是实现string类的str.toUpperCase(),所以你可以用这个秒杀;

   有str.toUpperCase(),那自然也有str.toLowerCase();

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. string trans(string str){
  7. for(int i=0;i<str.size();++i){
  8. if(str[i]>64&&str[i]<91){
  9. str[i]+=32;
  10. }
  11. else if(str[i]>96&&str[i]<123){
  12. str[i]-=32;
  13. }
  14. }
  15. return str;
  16. }
  17. int main() {
  18. string str;
  19. getline(cin,str);
  20. cout<<trans(str);
  21. }
  22. /*
  23. Sample input:
  24. Hello world
  25. Sample output:
  26. hELLO WORLD
  27. */
  28. /*
  29. Additional input:
  30. eLYSIA tRUe
  31. Additional output:
  32. Elysia TruE
  33. */

057.字符串替换

note:

1.第二道无人AC的题;

2.肯定是noj后台样例又出什么毛病了呗;

3.本质是实现string类的str.replace(),可以用这个直接解决;

  1. //Difficulty: 3/10
  2. //Importance: 3/10
  3. //2nd none AC you meet
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7. void subreplace(string &str,string olds,string news){
  8. size_t pos=0;
  9. while((pos=str.find(olds))!=string::npos){
  10. str.replace(pos,olds.length(),news);
  11. }
  12. }
  13. int main() {
  14. string str,olds,news;
  15. getline(cin,str);
  16. getline(cin,olds);
  17. getline(cin,news);
  18. subreplace(str,olds,news);
  19. cout<<str;
  20. }
  21. /*
  22. Sample input:
  23. xxforxx xx for xx
  24. xx
  25. nwpu
  26. Sample output:
  27. nwpufornwpu nwpu for nwpu
  28. */
  29. /*
  30. Additional input:
  31. xx for xx is xx
  32. xx
  33. Genshin Impact
  34. Additional output:
  35. Genshin Impact for Genshin Impact is Genshin Impact
  36. */

058.字符串切片

note:

1.本质是模拟python的slice,这下不能像上面几题直接来了,悲;

2.但是还可以用string类的str.substr()取出特点位置的字符串,进行曲线救国;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. int len;
  7. string string_slice(string str, int start,int stop,int step){
  8. string res;
  9. if (stop < 0) stop += len;
  10. if (start < 0) start += len;
  11. if(start>stop&&step<0){
  12. for (int i = start; i >stop; i+=step) {
  13. res += str[i];
  14. }
  15. return res;
  16. }
  17. if(start<=stop&&step>0){
  18. for (int i = start; i<stop; i+=step) {
  19. res += str[i];
  20. }
  21. return res;
  22. }
  23. }
  24. int main() {
  25. string str;
  26. getline(cin,str);
  27. len=str.size();
  28. int T;
  29. cin>>T;
  30. for(int i=0;i<T;++i){
  31. int n,a,b,c;
  32. cin>>n;
  33. switch (n) {
  34. case 1:
  35. cin>>a;
  36. cout<<string_slice(str,a,len,1)<<endl;break;
  37. case 2:
  38. cin>>a>>b;
  39. cout<<string_slice(str,a,b,1)<<endl;break;
  40. case 3:
  41. cin>>a>>b>>c;
  42. cout<<string_slice(str,a,b,c)<<endl;break;
  43. }
  44. }
  45. }
  46. /*
  47. Sample input:
  48. ABCDEFGHI
  49. 8
  50. 2 2 7
  51. 2 -7 -2
  52. 2 2 -5
  53. 3 2 7 2
  54. 3 6 1 -2
  55. 2 0 3
  56. 1 6
  57. 3 -1 -10 -1
  58. Sample output:
  59. CDEFG
  60. CDEFG
  61. CD
  62. CEG
  63. GEC
  64. ABC
  65. GHI
  66. IHGFEDCBA
  67. */
  68. /*
  69. Additional input:
  70. NULL
  71. Additional output:
  72. NULL
  73. */

059.元宇宙A+B

note:

1.不觉得decToMeta的数组很妙吗,也符合进制的概念;

2.在C++中,gets()函数由于具有极大安全性风险,已经被废弃,不建议使用;(就这样你瓜教材里还是它)

3.针对上一点,使用C++新标准的编译器依旧为了兼容C考虑,允许使用gets(),但是必定警告;如果你的编程工具啥也没说,这提醒你,该换啦,参考“必看”中的“推荐工具”;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <cstring>
  5. using namespace std;
  6. const static char decToMeta[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  7. static char c[100] = "", a[100] = "", b[100] = "";
  8. static int C[100] = {0}, A[100] = {0}, B[100] = {0};
  9. int metaToDec(char m) {
  10. if ('0' <= m && m <= '9') return m - '0';
  11. return m - 'A' + 10;
  12. }
  13. void add(void) {
  14. int lenA = strlen(a), lenB = strlen(b);
  15. for (int i = 0; i < lenA; ++i) A[i] = metaToDec(a[lenA - i - 1]);
  16. for (int i = 0; i < lenB; ++i) B[i] = metaToDec(b[lenB - i - 1]);
  17. int carry = 0;
  18. int lenC = lenA > lenB ? lenA : lenB;
  19. for (int i = 0; i < lenC; ++i) {
  20. C[i] = A[i] + B[i] + carry;
  21. carry = C[i] / 36;
  22. C[i] %= 36;
  23. }
  24. if (carry != 0) {
  25. C[lenC] = carry;
  26. ++lenC;
  27. }
  28. for (int i = lenC - 1; i >= 0; --i) c[i] = decToMeta[C[lenC - i - 1]];
  29. c[lenC] = '\0';
  30. }
  31. int main() {
  32. cin>>a>>b;
  33. add();
  34. cout<<c;
  35. return 0;
  36. }
  37. /*
  38. Sample input:
  39. ZZ 321
  40. Sample output:
  41. 420
  42. */
  43. /*
  44. Additional input:
  45. ac 42
  46. Additional output:
  47. 1BA
  48. */

060.字符串后缀

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. bool findSuf(string input, const string& std) {
  6. int pos = 0;
  7. if(input.substr(input.size()-std.size(),std.size()) == std) return true;
  8. return false;
  9. }
  10. int main(){
  11. string input,std;
  12. getline(cin,input);
  13. getline(cin,std);
  14. if (findSuf(input,std)) cout<<"Yes";
  15. else cout<<"No";
  16. }
  17. /*
  18. Sample input:
  19. hello world!
  20. world!
  21. Sample output:
  22. Yes
  23. */
  24. /*
  25. Additional input:
  26. Genshin Impact!
  27. Impact!
  28. Additional output:
  29. Yes
  30. */

第7季(061-070)

061.有效表达式

note:

1.回忆起来一点点高中时期的排列组合知识就够了,考虑顺序与匹配问题;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. int main() {
  6. long long n;
  7. cin>>n;
  8. long long cnt = 1;
  9. for (long long i = n + 2; i <= 2 * n; ++i) cnt *= i;
  10. for (long long i = 1; i <= n; ++i) cnt /= i;
  11. cout<<cnt;
  12. return 0;
  13. }
  14. /*
  15. Sample input:
  16. 6
  17. Sample output:
  18. 132
  19. */
  20. /*
  21. Additional input:
  22. 14
  23. Additional output:
  24. 2674440
  25. */

062.【专业融合:生物】DNA双螺旋结构

note:

1.不是我说,这融合得也太生硬了吧,这题也没有任何意义;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. void displayDNA1(){
  6. cout<<" AT \n";
  7. cout<<" T--A \n";
  8. cout<<" A----T \n";
  9. cout<<"T------A\n";
  10. cout<<"T------A\n";
  11. cout<<" G----C \n";
  12. cout<<" T--A \n";
  13. cout<<" GC \n";
  14. }
  15. void displayDNA2(){
  16. cout<<" CG \n";
  17. cout<<" C--G \n";
  18. cout<<" A----T \n";
  19. cout<<"A------T\n";
  20. cout<<"T------A\n";
  21. cout<<" A----T \n";
  22. cout<<" A--T \n";
  23. cout<<" GC \n";
  24. }
  25. void displayDNA3(){
  26. cout<<" AT \n";
  27. cout<<" C--G \n";
  28. cout<<" T----A \n";
  29. cout<<"C------G\n";
  30. cout<<"C------G\n";
  31. cout<<" T----A \n";
  32. cout<<" G--C \n";
  33. cout<<" AT \n";
  34. }
  35. int main() {
  36. int n;
  37. cin>>n;
  38. for (int i = 1; i <= n/2; ++i) {
  39. if (i % 3 == 1) displayDNA1();
  40. else if (i % 3 == 2) displayDNA2();
  41. else displayDNA3();
  42. }
  43. return 0;
  44. }
  45. /*
  46. Sample input:
  47. 8
  48. Sample output:
  49. AT
  50. T--A
  51. A----T
  52. T------A
  53. T------A
  54. G----C
  55. T--A
  56. GC
  57. CG
  58. C--G
  59. A----T
  60. A------T
  61. T------A
  62. A----T
  63. A--T
  64. GC
  65. AT
  66. C--G
  67. T----A
  68. C------G
  69. C------G
  70. T----A
  71. G--C
  72. AT
  73. AT
  74. T--A
  75. A----T
  76. T------A
  77. T------A
  78. G----C
  79. T--A
  80. GC
  81. */
  82. /*
  83. Additional input:
  84. 2
  85. Additional output:
  86. AT
  87. T--A
  88. A----T
  89. T------A
  90. T------A
  91. G----C
  92. T--A
  93. GC
  94. */

063.【专业融合:通信】GPS通信协议

note:

1.不是我说,这题完全可以算是极其重磅的了,在noj的一众卧龙凤雏里面也算得上是鹤立鸡群;

2.光这逆天Sample input,要是没有OCR,我得打到天荒地老;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. string out[100];
  7. int k=0;
  8. int check(string str){
  9. int i,result;
  10. for(result=str[1],i=2;str[i]!='*';i++)
  11. {
  12. result^=str[i];
  13. }
  14. return result;
  15. }
  16. int convert(string str){
  17. int res=0;
  18. res=stoi(str,0,16);
  19. return res;
  20. }
  21. void convert_BeingTime(string utcTime){
  22. int hour=stoi(utcTime.substr(0,2));
  23. int B_hour=(hour+8)%24;
  24. if(B_hour/10==0)
  25. out[k++]="0"+to_string(B_hour)+":"+utcTime.substr(2,2)+":"+utcTime.substr(4,2);
  26. else
  27. out[k++]=to_string(B_hour)+":"+utcTime.substr(2,2)+":"+utcTime.substr(4,2);
  28. }
  29. int main(){
  30. string str;
  31. while(cin>>str){
  32. if(str=="END") break;
  33. if(str.compare(0,6,"$GPRMC")==0){
  34. size_t asteriskPos = str.find('*');
  35. if(asteriskPos!=string::npos){
  36. int checksum=check(str);
  37. int senchecksum=convert(str.substr(asteriskPos + 1, 2));
  38. if(checksum!=senchecksum) {
  39. out[k++]="error";
  40. }
  41. else{
  42. string utcTime = str.substr(7, 6);
  43. convert_BeingTime(utcTime);
  44. }
  45. }
  46. }
  47. }
  48. for(int i=0;i<k;i++){
  49. cout<<out[i]<<endl;
  50. }
  51. }
  52. /*
  53. Sample input:
  54. $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05 ,324.27,150706,,,A*50
  55. $GPGSV,3,1,11,10,63,137,17,07,61,098,15,05,59,290,20,08,54,157,30*70
  56. $GPRMC,194548.127,A,5230.657,N,01325.713,E,3968.7,122.8,200220,000.0,W*44
  57. $GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76
  58. $GPRMC,111724.681,A,5231.801,N,01329.267,E,1289.3,000.0,291123,000.0,W*48
  59. $GNVTG,112.99,T,109.99,M,0.15,N,0.08,K,A*3B
  60. END
  61. Sample output:
  62. 10:48:13
  63. error
  64. 19:17:24
  65. */
  66. /*
  67. Additional input:
  68. NULL
  69. Additional output:
  70. NULL
  71. */

064.循环排序

note:

1.在那么多种类的算法里,排序算是基础而又十分重要的;

2.无论你有多摆,请至少学会冒泡和选择排序;

3.我会在后期更新一个排序算法的blog,七种排序算法的代码+演示;

  (真别小瞧这个,中文互联网上能搜到的排序算法大部分甚至不能编译成功,剩下的又有一大半是错的,正确的寥寥无几)

4.不行了,我抑制不住了,咕咕咕~

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. void swap(int *a, int *b) {
  6. int tmp = *a;
  7. *a = *b, *b = tmp;
  8. }
  9. void CycleSort(int arr[], int n) {
  10. for (int i = 0; i < n - 1; ++i) {
  11. int item = arr[i], pos = i;
  12. for (int j = i + 1; j < n; ++j) if (arr[j] < item) ++pos;
  13. if (pos == i) continue;
  14. swap(&arr[pos], &item);
  15. while(pos != i) {
  16. pos = i;
  17. for (int j = i + 1; j < n; ++j) if (arr[j] < item) ++pos;
  18. while (item == arr[pos]) ++pos;
  19. swap(&arr[pos], &item);
  20. }
  21. }
  22. }
  23. int main() {
  24. int n;
  25. cin>>n;
  26. int arr[n];
  27. for (int i = 0; i < n; ++i) cin>>arr[i];
  28. CycleSort(arr, n);
  29. for (int i = 0; i < n; ++i) cout<<arr[i]<<' ';
  30. return 0;
  31. }
  32. /*
  33. Sample input:
  34. 8
  35. 1 8 3 9 10 10 2 4
  36. Sample output:
  37. 1 2 3 4 8 9 10 10
  38. */
  39. /*
  40. Additional input:
  41. 10
  42. 1 1 1 1 2 6 2 6 2 7
  43. Additional output:
  44. 1 1 1 1 2 2 2 6 6 7
  45. */

065.长安

note:

1.实际上可以用简单的排列组合解决,但是我写的WA了,似乎是cmath里一些函数在noj里不能用?反正代码对的,我也懒得自己写函数代替那个函数了;

2.仔细读题,你会发现,noj连平面直角坐标系标点都不会标,请默认把所有除了(0,0)的坐标的x,y值减1,还原成正确坐标;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. //version 1:simple combination
  4. //but noj doesn't fully support cmath(seemly), so it's WA
  5. //of course you can write functions to fully replace cmath to ensure AC
  6. #include <iostream>
  7. #include <cmath>
  8. using namespace std;
  9. int combination(int n, int k) {
  10. if (k > n) {
  11. return 0;
  12. }
  13. return tgamma(n + 1) / (tgamma(k + 1) * tgamma(n - k + 1));
  14. }
  15. int ways(int bx,int by,int px, int py){
  16. bx-=1;by-=1;px-=1;py-=1;
  17. if(bx>=px&&by>=py){
  18. int nx=bx-px,ny=by-py;
  19. return combination(bx+by,bx<by?bx:by)-combination(px+py,px<py?px:py)* combination(nx+ny,nx<ny?nx:ny);
  20. }
  21. else return combination(bx+by,bx<by?bx:by);
  22. }
  23. int main() {
  24. int bx,by,px,py,cnt=0;
  25. while (1) {
  26. cin.sync();
  27. cin>>bx>>by>>px>>py;
  28. if (bx <= 0 || by <= 0 || px <= 0 || py <= 0) break;
  29. cnt=ways(bx,by,px,py);
  30. cout<<cnt<<endl;
  31. }
  32. }
  33. //version 2:DFS
  34. #include <iostream>
  35. using namespace std;
  36. int bx, by, px, py, cnt;
  37. void dfs(int x, int y) {
  38. if ((x == px && y == py) || x > bx || y > by) return;
  39. if (x == bx && y == by) {
  40. ++cnt;
  41. return;
  42. }
  43. dfs(x + 1, y);
  44. dfs(x, y + 1);
  45. }
  46. int main() {
  47. while (1) {
  48. fflush(stdin);
  49. cin>>bx>>by>>px>>py;
  50. if (bx <= 0 || by <= 0 || px <= 0 || py <= 0) break;
  51. cnt = 0;
  52. dfs(1, 1);
  53. cout<<cnt<<endl;
  54. }
  55. return 0;
  56. }
  57. /*
  58. Sample input:
  59. 8 6 5 3
  60. 8 6 8 6
  61. 8 6 9 6
  62. 0 0 0 0
  63. Sample output:
  64. 492
  65. 0
  66. 792
  67. */
  68. /*
  69. Additional input:
  70. 8 5 6 1
  71. 1 6 1 6
  72. 1 6 3 7
  73. 0 0 0 0
  74. Additional output:
  75. 315
  76. 0
  77. 1
  78. */

066.时钟A-B

note:

1.<ctime>是得学着用用,挺有用的;你甚至可以用ctime生成随机数,比rand()的假随机要好;

2.然鹅,很多情况下,<ctime>的计时并不准确,尤其是ms尺度,这个时候建议改用<chrono>;

3.某种程度上,你可以用chrono全面代替ctime,因为chrono是C++11标准库的一部分,更现代,更易用,更好;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <ctime>
  5. #include <iomanip>
  6. using namespace std;
  7. int main(){
  8. tm begin = {0}, end = {0};
  9. cin >> end.tm_year >> end.tm_mon >> end.tm_mday;
  10. cin >> begin.tm_year >> begin.tm_mon >> begin.tm_mday;
  11. begin.tm_year -= 1900;
  12. begin.tm_mon -= 1;
  13. end.tm_year -= 1900;
  14. end.tm_mon -= 1;
  15. time_t tmBegin = mktime(&begin);
  16. time_t tmEnd = mktime(&end);
  17. double difference = difftime(tmEnd, tmBegin);
  18. cout << fixed<<setprecision(6)<<difference << endl;
  19. return 0;
  20. }
  21. /*
  22. Sample input:
  23. 2021 1 2
  24. 2021 1 1
  25. Sample output:
  26. 86400.000000
  27. */
  28. /*
  29. Additional input:
  30. 2024 1 1
  31. 2024 1 2
  32. Additional output:
  33. -86400.000000
  34. */

067.Arduino显示

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. static const int digit[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  6. int getUnit(int num) {
  7. int cnt = 0;
  8. do {
  9. cnt += digit[num % 10];
  10. num /= 10;
  11. } while (num);
  12. return cnt;
  13. }
  14. int main() {
  15. int n;
  16. cin>>n;
  17. n -= 4;
  18. if (n <= 0) cout<<0;
  19. else {
  20. int cnt = 0;
  21. for (int i = 0; i <= 1111; ++i) {
  22. for (int j = 0; j <= 1111; ++j) {
  23. if (getUnit(i) + getUnit(j) + getUnit(i + j) == n) ++cnt;
  24. }
  25. }
  26. cout<<cnt;
  27. }
  28. return 0;
  29. }
  30. /*
  31. Sample input:
  32. 14
  33. Sample output:
  34. 2
  35. */
  36. /*
  37. Additional input:
  38. 23
  39. Additional output:
  40. 88
  41. */

068.【专业融合:网安】加密字串

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. static int freq[26] = {0};
  6. int main() {
  7. char plain[8000] = "";
  8. int x;
  9. cin>>plain>>x;
  10. for (int i = 0; plain[i]; ++i) ++freq[plain[i] - 'a'];
  11. char cipher[8000] = "";
  12. for (int i = 0; plain[i]; ++i) {
  13. if (freq[plain[i] - 'a'] & 1)
  14. cipher[i] = (char) (((plain[i] - 'a' - x) % 26 + 26) % 26 + 'a');
  15. else
  16. cipher[i] = (char) ((plain[i] - 'a' + x) % 26 + 'a');
  17. }
  18. cout<<cipher;
  19. return 0;
  20. }
  21. /*
  22. Sample input:
  23. abcda
  24. 3
  25. Sample output:
  26. dyzad
  27. */
  28. /*
  29. Additional input:
  30. fdmrghm
  31. 1
  32. Additional output:
  33. ecnqfgn
  34. */

069.【专业融合:自动化】PID控制

note:

1.你别说,你还真别说,这Sample output 也忒长了吧;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. struct PIDData {
  6. double Kp, Ki, Kd;
  7. double preError, integral;
  8. } ;
  9. double PIDCalculate(PIDData *pid, double setPoint, double measuredValue) {
  10. double error = setPoint - measuredValue;
  11. pid->integral += error;
  12. double differential = error - pid->preError;
  13. double output = pid->Kp * error + pid->Ki * pid->integral + pid->Kd * differential;
  14. pid->preError = error;
  15. return output;
  16. }
  17. int main() {
  18. double setPoint, measuredValue;
  19. int time;
  20. PIDData pid = {0};
  21. cin>>pid.Kp>>pid.Ki>>pid.Kd;
  22. cin>>setPoint>>measuredValue>>time;
  23. for (int i = 1; i <= time; ++i) {
  24. double output = PIDCalculate(&pid, setPoint, measuredValue);
  25. measuredValue += output;
  26. printf("%d %.6lf\n", i, measuredValue);
  27. }
  28. return 0;
  29. }
  30. /*
  31. Sample input:
  32. 0.1 0.01 0.05
  33. 100 0
  34. 100
  35. Sample output:
  36. 1 16.000000
  37. 2 25.440000
  38. 3 35.009600
  39. 4 44.265664
  40. 5 53.169142
  41. 6 61.668210
  42. 7 69.720909
  43. 8 77.293448
  44. 9 84.359807
  45. 10 90.901240
  46. 11 96.905764
  47. 12 102.367624
  48. 13 107.286754
  49. 14 111.668241
  50. 15 115.521778
  51. 16 118.861142
  52. 17 121.703666
  53. 18 124.069744
  54. 19 125.982338
  55. 20 127.466524
  56. 21 128.549046
  57. 22 129.257909
  58. 23 129.621989
  59. 24 129.670681
  60. 25 129.433566
  61. 26 128.940117
  62. 27 128.219429
  63. 28 127.299977
  64. 29 126.209409
  65. 30 124.974359
  66. 31 123.620295
  67. 32 122.171385
  68. 33 120.650395
  69. 34 119.078603
  70. 35 117.475745
  71. 36 115.859968
  72. 37 114.247816
  73. 38 112.654219
  74. 39 111.092512
  75. 40 109.574456
  76. 41 108.110278
  77. 42 106.708722
  78. 43 105.377103
  79. 44 104.121378
  80. 45 102.946217
  81. 46 101.855081
  82. 47 100.850307
  83. 48 99.933190
  84. 49 99.104069
  85. 50 98.362420
  86. 51 97.706938
  87. 52 97.135627
  88. 53 96.645882
  89. 54 96.234574
  90. 55 95.898129
  91. 56 95.632605
  92. 57 95.433761
  93. 58 95.297129
  94. 59 95.218079
  95. 60 95.191874
  96. 61 95.213729
  97. 62 95.278857
  98. 63 95.382521
  99. 64 95.520066
  100. 65 95.686962
  101. 66 95.878832
  102. 67 96.091477
  103. 68 96.320904
  104. 69 96.563341
  105. 70 96.815249
  106. 71 97.073341
  107. 72 97.334582
  108. 73 97.596194
  109. 74 97.855665
  110. 75 98.110740
  111. 76 98.359419
  112. 77 98.599956
  113. 78 98.830847
  114. 79 99.050823
  115. 80 99.258838
  116. 81 99.454062
  117. 82 99.635862
  118. 83 99.803795
  119. 84 99.957590
  120. 85 100.097136
  121. 86 100.222469
  122. 87 100.333754
  123. 88 100.431276
  124. 89 100.515421
  125. 90 100.586667
  126. 91 100.645566
  127. 92 100.692736
  128. 93 100.728849
  129. 94 100.754615
  130. 95 100.770775
  131. 96 100.778092
  132. 97 100.777339
  133. 98 100.769291
  134. 99 100.754719
  135. 100 100.734384
  136. */
  137. /*
  138. Additional input:
  139. NULL
  140. Additional output:
  141. NULL
  142. */

070.三元搜索

note:

1.三元搜索可能在一些优化的排序算法中使用到;

2.但是没有那么必要去掌握,但二分查找得会吧;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. int terSearch(int arr[], int n, int k) {
  6. int left = 0, right = n - 1, mid1 = (n - 1) / 3, mid2 = n - mid1;
  7. while(mid1 != mid2) {
  8. if (k > arr[right] || k < arr[left]) return -1;
  9. if (k == arr[mid1]) return mid1;
  10. if (k == arr[mid2]) return mid2;
  11. if (mid1 == mid2) break;
  12. if (k < arr[mid1]) right = mid1 - 1;
  13. else if (k > arr[mid2]) left = mid2 + 1;
  14. else left = mid1 + 1, right = mid2 - 1;
  15. mid1 = left + (right - left) / 3, mid2 = right - (right - left) / 3;
  16. }
  17. return -1;
  18. }
  19. int main() {
  20. int n, k;
  21. cin>>n;
  22. int arr[n];
  23. for (int i = 0; i < n; ++i) cin>>arr[i];
  24. cin>>k;
  25. printf("%d in [%d]", k, terSearch(arr, n, k));
  26. return 0;
  27. }
  28. /*
  29. Sample input:
  30. 10
  31. 1 2 3 4 5 6 7 8 9 10
  32. 5
  33. Sample output:
  34. 5 in [4]
  35. */
  36. /*
  37. Additional input:
  38. 10
  39. 11 16 18 19 21 34 53 55 69 70
  40. 55
  41. Additional output:
  42. 55 in [7]
  43. */

第8季(071-080)

071.【专业融合:数学】中位数

note:

1.实质上就是排序,试着使用STL里的vector容器,体验一把C++的力量吧;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <vector>
  5. #include <iomanip>
  6. using namespace std;
  7. int main() {
  8. vector<int> arr;
  9. int tmp;
  10. int cnt=0;
  11. int count[100]={0};
  12. cin>>tmp;
  13. while(tmp>=0) {
  14. arr.push_back(tmp);
  15. if (tmp == 0) {
  16. cnt++;
  17. }else{
  18. count[cnt]++;
  19. }
  20. cin >> tmp;
  21. }
  22. vector<int> print;
  23. for(int i=0;i<arr.size();++i){
  24. if(arr[i]>0){
  25. print.push_back(arr[i]);
  26. }
  27. }
  28. int sum[100]={0};
  29. for(int k=0;k<cnt;++k){
  30. for(int l=0;l<=k;++l)
  31. {
  32. sum[k]+=count[l];
  33. }
  34. }
  35. for(int k=0;k<cnt;++k){
  36. for(int i=0;i<sum[k];++i){
  37. cout<<print[i]<<' ';
  38. }
  39. double ans;
  40. if(sum[k]%2==0){
  41. ans=(print[sum[k]/2]+print[sum[k]/2-1])/2.0;
  42. }
  43. else ans=print[sum[k]/2];
  44. cout<<fixed<<setprecision(6)<<ans<<endl;
  45. }
  46. }
  47. /*
  48. Sample input:
  49. 1 2 3 4 5 0
  50. 7 8 9 0
  51. -1
  52. Sample output:
  53. 1 2 3 4 5 3.000000
  54. 1 2 3 4 5 7 8 9 4.500000
  55. */
  56. /*
  57. Additional input:
  58. 145 15 163 13609 135 0
  59. 15 3614 16345 13051 16349 14 0
  60. -1
  61. Additional output:
  62. 145 15 163 13609 135 163.000000
  63. 145 15 163 13609 135 15 3614 16345 13051 16349 14 15.000000
  64. */

072.【专业融合:航天】卫星定位

note:

1.不是我说,这俩卫星的题目(另一道GPS),主打一个人间不值得;

  1. //Difficulty:??/10
  2. //Importance: 0/10
  3. #include <iostream>
  4. #include <cmath>
  5. #define N 11
  6. #define c 299792.458
  7. using namespace std;
  8. double X[N],A[N],B[N],C[N],T[N];
  9. void scanf1(double A[N],int n){
  10. for(int i=0;i<n;i++) {
  11. cin >> A[i];
  12. }
  13. }
  14. void print1(double A[N],int n) { //输出一个矢量
  15. int i,tmp;
  16. double a;
  17. for (i=0; i<n-1; i++){
  18. tmp=(int)(A[i]*10000);
  19. a=(double)tmp/10000.0;
  20. printf("%.4lf,",a);
  21. }
  22. tmp=(int)(A[n-1]*10000);
  23. a=(double)tmp/10000.0;
  24. printf("%.4lf",a);
  25. }
  26. void print2(double A[N][N],int n) { //输出一个矩阵
  27. int i, j;
  28. for (i=0; i<n; i++) {
  29. for (j=0; j<n; j++)
  30. printf("%.7lf ", A[i][j]);
  31. printf("\n");
  32. }
  33. }
  34. // 计算代数余子式函数,结果=dest
  35. int GetCoFactor(double dest[N][N], double src[N][N], int row, int col, int n)
  36. {
  37. int i, j;
  38. int colCount=0,rowCount=0;
  39. for(i=0; i<n; i++ ) {
  40. if( i!=row ) {
  41. colCount = 0;
  42. for(j=0; j<n; j++ )
  43. if( j != col ) { //当j不是元素时
  44. dest[rowCount][colCount] = src[i][j];
  45. colCount++;
  46. }
  47. rowCount++;
  48. }
  49. }
  50. return 1;
  51. }
  52. // 递归计算行列式,结果=返回值
  53. double CalcDeterminant(double mat[N][N], int n)
  54. {
  55. int i,j;
  56. double det = 0; //行列式值
  57. double minor[N][N]; // allocate 余子式矩阵
  58. // n 必须 >= 0,当矩阵是单个元素时停止递归
  59. if( n == 1 ) return mat[0][0];
  60. for(i = 0; i < n; i++ ) {
  61. GetCoFactor(minor, mat, 0, i , n);
  62. det += ( i%2==1 ? -1.0 : 1.0 ) * mat[0][i] * CalcDeterminant(minor,n-1);
  63. }
  64. return det;
  65. }
  66. // 伴随矩阵法矩阵求逆 , 结果存放到 inv 数组
  67. void MatrixInversion(double J[N][N], int n)
  68. {
  69. int i,j;
  70. double det, temp [N][N], minor[N][N];
  71. double inv[N][N];
  72. det = 1.0/CalcDeterminant(J,n); //计算行列式
  73. for(j=0; j<n; j++)
  74. for(i=0; i<n; i++) {
  75. // 得到矩阵A(j,i)的代数余子式
  76. GetCoFactor(minor,J,j,i,n);
  77. inv[i][j] = det*CalcDeterminant(minor,n-1);
  78. if( (i+j)%2 == 1)
  79. inv[i][j] = -inv[i][j];
  80. }
  81. //结果存回J矩阵
  82. for(j=0; j<n; j++)
  83. for(i=0; i<n; i++)
  84. J[i][j] = inv[i][j];
  85. }
  86. // 由Xn计算函数Fn,结果存放到 F
  87. void CalcF(double F[N],double X[N],int n) {
  88. double f;
  89. int i;
  90. for (i=0; i<n; i++) {
  91. switch (i+1) {
  92. case 1:
  93. f=X[0]*X[0]+X[1]*X[1]-2*X[0]-X[2]+1; //x^2+y^2-2x-z+1
  94. break;
  95. case 2:
  96. f=X[0]*X[1]*X[1]-X[0]-3*X[1]+X[1]*X[2]+2; //xy^2-x-3y+yz+2
  97. break;
  98. case 3:
  99. f=X[0]*X[2]*X[2]-3*X[2]+X[1]*X[2]*X[2]+X[0]*X[1]; //xz^2-3z+yz^2+xy
  100. break;
  101. }
  102. F[i]=f;
  103. }
  104. }
  105. void CalcF_re(double F[N],double X[N],int n) {
  106. double f;
  107. int i;
  108. for (i=0; i<n; i++) {
  109. switch (i+1) {
  110. case 1:
  111. f=(X[0]-A[0])*(X[0]-A[0])+(X[1]-B[0])*(X[1]-B[0])+(X[2]-C[0])*(X[2]-C[0])-(c*(T[0]-X[3]))*(c*(T[0]-X[3]));
  112. break;
  113. case 2:
  114. f=(X[0]-A[1])*(X[0]-A[1])+(X[1]-B[1])*(X[1]-B[1])+(X[2]-C[1])*(X[2]-C[1])-(c*(T[1]-X[3]))*(c*(T[1]-X[3]));
  115. break;
  116. case 3:
  117. f=(X[0]-A[2])*(X[0]-A[2])+(X[1]-B[2])*(X[1]-B[2])+(X[2]-C[2])*(X[2]-C[2])-(c*(T[2]-X[3]))*(c*(T[2]-X[3]));
  118. break;
  119. case 4:
  120. f=(X[0]-A[3])*(X[0]-A[3])+(X[1]-B[3])*(X[1]-B[3])+(X[2]-C[3])*(X[2]-C[3])-(c*(T[3]-X[3]))*(c*(T[3]-X[3]));
  121. }
  122. F[i]=f;
  123. }
  124. }
  125. // 由Xn计算偏导数Jacobian矩阵F'n,结果存放到 J
  126. void CalcJ(double J[N][N],double X[N],int n) {
  127. double f;
  128. int i,j;
  129. for (i=0; i<n; i++)
  130. switch (i+1) {
  131. case 1:
  132. for (j=0; j<n; j++) {
  133. switch (j+1) {
  134. case 1: f=2*X[0]-2;break; // J1.1=2x-2
  135. case 2: f=2*X[1];break; // J1.2=2y
  136. case 3: f=-1;break; // J1.3=-1
  137. }
  138. J[i][j]=f;
  139. }
  140. break;
  141. case 2:
  142. for (j=0; j<n; j++) {
  143. switch (j+1) {
  144. case 1: f=X[1]*X[1]-1;break; // J2.1=y^2-1
  145. case 2: f=2*X[0]*X[1]-3+X[2];break; // J2.2=2xy-3+z
  146. case 3: f=X[1];break; // J2.3=y
  147. }
  148. J[i][j]=f;
  149. }
  150. break;
  151. case 3:
  152. for (j=0; j<n; j++) {
  153. switch (j+1) {
  154. case 1: f=X[2]*X[2]+X[1];break; // J3.1=z^2+y
  155. case 2: f=X[2]*X[2]+X[0];break; // J3.2=z^2+x
  156. case 3: f=2*X[0]*X[2]-3+2*X[1]*X[2];break; // J3.3=2xz-3+2yz
  157. }
  158. J[i][j]=f;
  159. }
  160. break;
  161. }
  162. }
  163. // 由Xn计算偏导数Jacobian矩阵F'n,结果存放到 J
  164. void CalcJ_re(double J[N][N],double X[N],int n) {
  165. double f;
  166. int i,j;
  167. for (i=0; i<n; i++)
  168. switch (i+1) {
  169. case 1:
  170. for (j=0; j<n; j++) {
  171. switch (j+1) {
  172. case 1: f=2*(X[0]-A[0]);break; // J1.1=2(x-A1)
  173. case 2: f=2*(X[1]-B[0]);break; // J1.2=2(y-B1)
  174. case 3: f=2*(X[2]-C[0]);break; // J1.3=2(z-C1)
  175. case 4: f=2*c*c*(T[0]-X[3]);break;//J1.4=2*c^2(t1-d)
  176. }
  177. J[i][j]=f;
  178. }
  179. break;
  180. case 2:
  181. for (j=0; j<n; j++) {
  182. switch (j+1) {
  183. case 1: f=2*(X[0]-A[1]);break; // J1.1=2(x-A1)
  184. case 2: f=2*(X[1]-B[1]);break; // J1.2=2(y-B1)
  185. case 3: f=2*(X[2]-C[1]);break; // J1.3=2(z-C1)
  186. case 4: f=2*c*c*(T[1]-X[3]);break;//J1.4=2*c^2(t1-d)
  187. }
  188. J[i][j]=f;
  189. }
  190. break;
  191. case 3:
  192. for (j=0; j<n; j++) {
  193. switch (j+1) {
  194. case 1: f=2*(X[0]-A[2]);break; // J1.1=2(x-A1)
  195. case 2: f=2*(X[1]-B[2]);break; // J1.2=2(y-B1)
  196. case 3: f=2*(X[2]-C[2]);break; // J1.3=2(z-C1)
  197. case 4: f=2*c*c*(T[2]-X[3]);break;//J1.4=2*c^2(t1-d)
  198. }
  199. J[i][j]=f;
  200. }
  201. break;
  202. case 4:
  203. for (j=0; j<n; j++) {
  204. switch (j+1) {
  205. case 1: f=2*(X[0]-A[3]);break; // J1.1=2(x-A1)
  206. case 2: f=2*(X[1]-B[3]);break; // J1.2=2(y-B1)
  207. case 3: f=2*(X[2]-C[3]);break; // J1.3=2(z-C1)
  208. case 4: f=2*c*c*(T[3]-X[3]);break;//J1.4=2*c^2(t1-d)
  209. }
  210. J[i][j]=f;
  211. }
  212. break;
  213. }
  214. }
  215. // 计算 J^-1* F,结果存放到 R
  216. void CalcJF(double R[N], double J[N][N], double F[N], int n) {
  217. int i,j,k;
  218. for (i=0; i<n; i++) {
  219. R[i]=0.0;
  220. for (j=0; j<n; j++)
  221. R[i] = R[i] + J[i][j]*F[j];
  222. }
  223. }
  224. // 计算 X=X0-R,结果存放到 X
  225. void CalcX(double X[N],double X0[N],double R[N],int n) {
  226. int i;
  227. for (i=0; i<n; i++)
  228. X[i]=X0[i]-R[i];
  229. }
  230. // 计算 A=B,结果存放到 A
  231. void AequB(double A[N],double B[N],int n) {
  232. int i;
  233. for (i=0; i<n; i++)
  234. A[i]=B[i];
  235. }
  236. // 计算 F-
  237. double Ferror(double F[N], int n) {
  238. double m=0;
  239. int i;
  240. for (i=0; i<n; i++) {
  241. double t=fabs(F[i]);
  242. if (m<t) m = t;
  243. }
  244. return m;
  245. }
  246. // Newton–Raphson method 牛顿迭代法求非线性方程组的根,存放到X0
  247. void mvNewtons(double X0[N], int n, double e) {
  248. // Guess为初始猜测值 e为迭代精度要求
  249. int k;
  250. double J[N][N],Y[N][N];
  251. double X[N],R[N],F[N];
  252. //X0一开始为初始猜测值
  253. for (k=1; k<=20; k++) { //限定20次迭代
  254. /*
  255. printf("-------------- n=%d\n",k);
  256. printf("X\n");
  257. print1(X0,n); //输出X0
  258. */
  259. CalcF_re(F,X0,n); //计算F矩阵
  260. /*
  261. printf("F\n"); //观察 F
  262. print1(F,n); //输出F
  263. */
  264. CalcJ_re(J,X0,n); //计算Jacobian矩阵F'n(x0)
  265. /*
  266. printf("J\n");
  267. print2(J,n); //观察 J
  268. */
  269. MatrixInversion(J, n); // 求J的逆矩阵 J^-1
  270. CalcJF(R,J,F,n); // R=J^-1 * F
  271. CalcX(X,X0,R,n); // X=X0-R
  272. AequB(X0,X,n); // X0=X 下次迭代
  273. if (Ferror(F,n)<e) break; //达到精度要求,终止迭代
  274. }
  275. }
  276. int main() {
  277. int n=4;
  278. cin>>A[0]>>B[0]>>C[0];
  279. cin>>A[1]>>B[1]>>C[1];
  280. cin>>A[2]>>B[2]>>C[2];
  281. cin>>A[3]>>B[3]>>C[3];
  282. scanf1(T,n);
  283. scanf1(X,n);
  284. mvNewtons(X,n,1e-6); //根存放在X
  285. print1(X,3);
  286. return 0;
  287. }
  288. /*
  289. Sample input:
  290. 15600 7540 20140
  291. 18760 2750 18610
  292. 17610 14630 13480
  293. 19170 610 18390
  294. 0.07074 0.07220 0.07690 0.07242
  295. 0 0 6370 0
  296. Sample output:
  297. -41.7727,-16.7891,6370.0595
  298. */
  299. /*
  300. Additional input:
  301. NULL
  302. Additional output:
  303. NULL
  304. */

073.【专业融合:机械】几何约束

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. int maxi(int a, int b) {
  6. return a > b ? a : b;
  7. }
  8. int mini(int a, int b) {
  9. return a < b ? a : b;
  10. }
  11. int cross(int x1, int y1, int x2, int y2) {
  12. return x1 * y2 - y1 * x2;
  13. }
  14. bool insert(int line1[4], int line2[4]) {
  15. if (maxi(line2[0], line2[2]) < mini(line1[0], line1[2]) ||
  16. maxi(line1[0],line1[2]) < mini(line2[0], line2[2]) ||
  17. maxi(line2[1], line2[3]) < mini(line1[1], line1[3]) ||
  18. maxi(line1[1], line2[3]) < mini(line2[1],line2[3])) return false;
  19. if (cross(line1[2] - line1[0], line1[3] - line1[1], line2[0] - line1[0], line2[1] - line1[1]) *
  20. cross(line1[2] - line1[0], line1[3] - line1[1], line2[2] - line1[0], line2[3] - line1[1]) > 0 ||
  21. cross(line2[2] - line2[0], line2[3] - line2[1], line1[0] - line2[0], line1[1] - line2[1]) *
  22. cross(line2[2] - line2[0], line2[3] - line2[1], line1[2] - line2[0], line1[3] - line2[1]) > 0) return false;
  23. return true;
  24. }
  25. int main() {
  26. int n;
  27. cin>>n;
  28. int line[n][4];
  29. for (int i = 0; i < n; ++i)
  30. for (int j = 0; j < 4; ++j) cin>>line[i][j];
  31. int cnt = 0;
  32. for (int i = 0; i < n; ++i) {
  33. for (int j = i + 1; j < n; ++j) {
  34. if (insert(line[i], line[j])) {
  35. cout<<"X: #"<<i + 1<<" #"<<j + 1<<endl;
  36. ++cnt;
  37. }
  38. }
  39. }
  40. cout<<"n="<<cnt;
  41. return 0;
  42. }
  43. /*
  44. Sample input:
  45. 5
  46. 1 5 4 5
  47. 2 5 10 1
  48. 3 2 10 3
  49. 6 4 9 4
  50. 7 1 8 1
  51. Sample output:
  52. X: #1 #2
  53. X: #2 #3
  54. n=2
  55. */
  56. /*
  57. Additional input:
  58. 5
  59. 1 5 4 9
  60. 2 5 10 3
  61. 3 2 10 6
  62. 6 4 9 45
  63. 7 1 8 0
  64. Additional output:
  65. X: #2 #3
  66. X: #2 #4
  67. n=2
  68. */

074.【专业融合:天文】日出日落时间

note:

1.你瓜甚至没有天文专业,这整的多生硬;你甚至还要输出正午的时间,为啥要叫“日出日落时间”;

2.第三个无人AC的题目;

3.主要问题在于,题目里给的链接根本打不开,不知道他用的是什么样的NOAA算法,就不知道算法的每一步的精度、误差是如何产生的;所以就算自己写一个NOAA算法,也势必得不到和题目里一样的结果;

4.所以,下面的代码只是一个例子,只能WA,图一乐;

  1. //Difficulty:??/10
  2. //Importance: 0/10
  3. //3th none AC you meet
  4. #include <cmath>
  5. #include <iostream>
  6. #include <iomanip>
  7. #define M_PI 3.14159265358979323846
  8. using namespace std;
  9. double degToRad(double deg) {
  10. return deg * M_PI / 180.0;
  11. }
  12. double calcSolarDeclination(int dayOfYear) {
  13. return 0.4093 * sin((2.0 * M_PI / 365.0) * dayOfYear - 1.405);
  14. }
  15. double calcTime(int year, int month, int day, double longitude, double latitude, bool sunrise, int timezone) {
  16. int dayOfYear = (month - 1) * 30 + day;
  17. double declination = calcSolarDeclination(dayOfYear);
  18. double time = 12.0 + (sunrise ? -1 : 1) * acos(( sin(degToRad(-0.83)) - sin(degToRad(latitude)) * sin(declination)) / ( cos(degToRad(latitude)) * cos(declination))) / M_PI * 180.0 / 15.0;
  19. return time - 4.0 * longitude / 60.0 + timezone;
  20. }
  21. int main() {
  22. int year, month, day, timezone;
  23. double longitude, latitude;
  24. cin >> year >> month >> day ;
  25. cin >> longitude >> latitude ;
  26. cin >> timezone;
  27. double sunrise = calcTime(year, month, day, longitude, latitude, true, timezone);
  28. double sunset = calcTime(year, month, day, longitude, latitude, false, timezone);
  29. double noon = calcTime(year, month, day, longitude, latitude, false, timezone) - (sunset - sunrise) / 2;
  30. cout <<"Solar Noon="<< setfill('0') << setw(2) << int(noon) << ":" << setw(2) << int((noon - int(noon)) * 60) << ":" << setw(2) << int(((noon - int(noon)) * 60 - int((noon - int(noon)) * 60)) * 60) << endl;
  31. cout <<" Sunrise="<< setfill('0') << setw(2) << int(sunrise) << ":" << setw(2) << int((sunrise - int(sunrise)) * 60) << ":" << setw(2) << int(((sunrise - int(sunrise)) * 60 - int((sunrise - int(sunrise)) * 60)) * 60) << endl;
  32. cout <<" Sunset="<< setfill('0') << setw(2) << int(sunset) << ":" << setw(2) << int((sunset - int(sunset)) * 60) << ":" << setw(2) << int(((sunset - int(sunset)) * 60 - int((sunset - int(sunset)) * 60)) * 60) << endl;
  33. return 0;
  34. }
  35. /*
  36. Sample input:
  37. 2022 8 15
  38. 108.760064 34.035672
  39. Sample output:
  40. */
  41. /*
  42. Additional input:
  43. NULL
  44. Additional output:
  45. NULL
  46. */

075.成绩单

1.结构体是一定要会滴,尽管这题题干让用C的方式写结构体,但是最好直接用C++风格的方式;

2.最好还要掌握结构体写入写出文件;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. using namespace std;
  5. typedef struct tag {
  6. long long id;
  7. char name[31];
  8. int score;
  9. } ST;
  10. void load(ST* arr[], int n) {
  11. for (int i = 0; i < n; ++i) {
  12. arr[i] = (ST*) malloc(sizeof(ST));
  13. cin>>arr[i]->id>>arr[i]->name>>arr[i]->score;
  14. }
  15. }
  16. void sort(ST* arr[], int n) {
  17. ST* tmp = NULL;
  18. for (int i = 0; i < n; ++i) {
  19. bool isSwapped = false;
  20. for (int j = 0; j < n - 1 - i; ++j)
  21. if ((arr[j]->score < arr[j + 1]->score) ||
  22. (arr[j]->score == arr[j + 1]->score && arr[j]->id > arr[j + 1]->id))
  23. tmp = arr[j], arr[j] = arr[j + 1], arr[j + 1] = tmp, isSwapped = true;
  24. if (!isSwapped) break;
  25. }
  26. }
  27. void traverse(ST* arr[], int n) {
  28. for (int i = 0; i < n; ++i)
  29. cout<<arr[i]->id<<' '<<arr[i]->name<<' '<<arr[i]->score<<endl;
  30. }
  31. int main() {
  32. int n;
  33. cin>>n;
  34. ST* grade[n];
  35. load(grade, n);
  36. sort(grade, n);
  37. traverse(grade, n);
  38. return 0;
  39. }
  40. /*
  41. Sample input:
  42. 6
  43. 2001900001 Jerry 88
  44. 2001900005 Tom 92
  45. 2001900006 Milla 85
  46. 2001900002 Alice 80
  47. 2001900003 Mickey 85
  48. 2001900004 Aladdin 83
  49. Sample output:
  50. 2001900005 Tom 92
  51. 2001900001 Jerry 88
  52. 2001900003 Mickey 85
  53. 2001900006 Milla 85
  54. 2001900004 Aladdin 83
  55. 2001900002 Alice 80
  56. */
  57. /*
  58. Additional input:
  59. NULL
  60. Additional output:
  61. NULL
  62. */

076.【专业融合:材料】晶体结构

note:

1.专业融合真是一锅乱炖啊,什么都融只会害了你!

2.这题目整的多牵强;

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <string>
  5. #include <iomanip>
  6. #include <cmath>
  7. using namespace std;
  8. struct Atom
  9. {
  10. string name;
  11. double mass;
  12. double APF;
  13. double r;
  14. };
  15. Atom elemList[] =
  16. {
  17. { "Po", 208.998, 0.52360, 1.68 },
  18. { "Li", 6.941, 0.68017, 1.52 },
  19. { "Na", 22.989770, 0.68017, 1.86 },
  20. { "Cr", 51.9961, 0.68017, 1.28 },
  21. { "Mn", 54.938049, 0.68017, 1.27 },
  22. { "Fe", 55.845, 0.68017, 1.26 },
  23. { "Mo", 95.94, 0.68017, 1.39 },
  24. { "Ta", 180.9749, 0.68017, 1.46 },
  25. { "Al", 26.981538, 0.74048, 1.43 },
  26. { "Ca", 40.078, 0.74048, 1.97 },
  27. { "Ni", 58.6934, 0.74048, 1.24 },
  28. { "Cu", 63.546, 0.74048, 1.28 },
  29. { "Ge", 72.64, 0.74048, 1.22 },
  30. { "Ag", 107.8682, 0.74048, 1.44 },
  31. { "Pt", 195.078, 0.74048, 1.39 },
  32. { "Au", 196.96655, 0.74048, 1.44 },
  33. { "Pb", 207.2, 0.74048, 1.75 }
  34. };
  35. template <int N> double pow(double a) { return a * pow<N - 1>(a); }
  36. template <> double pow<0>(double a) { return 1; }
  37. int main()
  38. {
  39. int n;
  40. cin >> n;
  41. for (int i = 0; i < n; ++i)
  42. {
  43. string in;
  44. cin >> in;
  45. for(const auto& e : elemList)
  46. {
  47. if(e.name == in)
  48. {
  49. double V = 4.0 * M_PI * pow<3>(e.r) / 3.0;
  50. cout << fixed << setprecision(2) << 10.0 * e.mass * e.APF / 6.022 / V << endl;
  51. break;
  52. }
  53. }
  54. }
  55. return 0;
  56. }
  57. /*
  58. Sample input:
  59. 3
  60. Po
  61. Mo
  62. Cu
  63. Sample output:
  64. 9.15
  65. 9.63
  66. 8.89
  67. */
  68. /*
  69. Additional input:
  70. NULL
  71. Additional output:
  72. NULL
  73. */

077.【专业融合:化学】原子计数

  1. //Difficulty: 2/10
  2. //Importance: 2/10
  3. #include <iostream>
  4. #include <map>
  5. using namespace std;
  6. string s;
  7. int getnum(int x)
  8. {
  9. int res = 0;
  10. while (true)
  11. {
  12. char ch = s[x];
  13. if(ch >='0'&& ch<='9' && s[x])
  14. {
  15. res = res * 10 + ch - '0';
  16. ++ x;
  17. }
  18. else return res;
  19. }
  20. return res;
  21. }
  22. int main()
  23. {
  24. getline(cin, s);
  25. map<string, int> ump;
  26. for (int i = 0; s[i]; i ++)
  27. {
  28. char ch = s[i];
  29. if(!(ch>='A' && ch<='Z'))continue;
  30. char ch1 = s[i+1];
  31. if (ch1>='a' && ch1<='z')
  32. {
  33. string ele = "";
  34. ele.append(1, ch);
  35. ele.append(1, ch1);
  36. int num = getnum(i+2);
  37. if(num)
  38. {
  39. if(!ump.count(ele)) ump[ele] = num;
  40. else ump[ele] += num;
  41. }
  42. else
  43. {
  44. if(!ump.count(ele)) ump[ele] = 1;
  45. else ump[ele] += 1;
  46. }
  47. }
  48. else
  49. {
  50. int num = getnum(i+1);
  51. string ele = "";
  52. ele.append(1, ch);
  53. if(num)
  54. {
  55. if(!ump.count(ele)) ump[ele] = num;
  56. else ump[ele] += num;
  57. }
  58. else
  59. {
  60. if(!ump.count(ele)) ump[ele] = 1;
  61. else ump[ele] += 1;
  62. }
  63. }
  64. }
  65. for (auto& p : ump)
  66. {
  67. cout << p.first << " " << p.second << endl;
  68. }
  69. }
  70. /*
  71. Sample input:
  72. Fe2H3OH
  73. Sample output:
  74. Fe 2
  75. H 4
  76. */
  77. /*
  78. Additional input:
  79. H2O
  80. Additional output:
  81. H 2
  82. O 1
  83. */

078.【专业融合:动能】热能计算

note:

1.写到这题就好好笑吧,毕竟表示百分数的这种写法真没见过

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