当前位置:   article > 正文

西北工业大学noj2023年c程序设计100题,更新中_西工大noj

西工大noj

写在前面

这个是西北工业大学noj新版的解题思路,2023年更新的题库,我提供了比较复杂的题目的解题思路和方法,对于相对简单的题目则是只给出了代码,但是希望读者不要直接抄袭,因为这个课还是很重要的,是未来四年的基础,倘若有什么问题可以在评论区指出,

未来做完100道noj后可能会将题目上传到gitee和github上供参考,希望读者能从这个文章中有所收获

1.输出helloworld

  1. #include<stdio.h>
  2. int main(){
  3. printf("Hello World");
  4. }

这个很简单没有什么好说的

2.输出a+b

  1. #include<stdio.h>
  2. int main(){
  3. int a,b,c;
  4. scanf("%d",&a);
  5. scanf("%d",&b);
  6. c=a+b;
  7. printf("%d",c);
  8. }

这个也很简单没有什么需要多说的

3.输出数据类型与范围

  1. #include<stdio.h>
  2. int main(){
  3. int a;
  4. scanf("%d",&a);
  5. if(a==1){
  6. printf("1,-128,127");
  7. }
  8. if(a==2)
  9. {
  10. printf("1,0,255");
  11. }
  12. if(a==3)
  13. {
  14. printf("2,-32768,32767");
  15. }
  16. if(a==4)
  17. {
  18. printf("2,0,65535");
  19. }
  20. if(a==5)
  21. {
  22. printf("4,-2147483648,2147483647");
  23. }if(a==6)
  24. {
  25. printf("4,0,4294967295");
  26. }if(a==7)
  27. {
  28. printf("4,-2147483648,2147483647");
  29. }if(a==8)
  30. {
  31. printf("4,0,4294967295");
  32. }
  33. if(a==9)
  34. {
  35. printf("8,-9223372036854775808,9223372036854775807");
  36. }
  37. if(a==10)
  38. {
  39. printf("8,0,18446744073709551615");
  40. }
  41. }

需要注意是不是把数据范围打错了,可以根据这个代码参考修改数据

4.平均数

  1. #include <stdio.h>
  2. int main() {
  3. long long a, b;
  4. long long average;
  5. scanf("%lld",&a);
  6. scanf("%lld",&b);
  7. average = (a + b) / 2;
  8. printf("%lld\n",average);
  9. return 0;
  10. }

新版noj中第一个重量级,需要用longlongint来存储数据,如果用int会过不了

5.进制转换

  1. #include <stdio.h>
  2. int main() {
  3. unsigned int num; // 使用 unsigned int 来确保接受非负整数
  4. scanf("%u", &num);
  5. printf("%X,", num); // %x 用于输出十六进制
  6. printf("%o", num); // %o 用于输出八进制
  7. return 0;
  8. }

注意输出的时候有个,其他非常简单

6.浮点数输出

  1. #include <stdio.h>
  2. int main() {
  3. double num;
  4. scanf("%lf", &num);
  5. printf("%.6lf,%.2lf,%.8lf\n", num, num, num);
  6. return 0;
  7. }

不赘述

7.动态宽度输出

  1. #include <stdio.h>
  2. int main() {
  3. int m, n;
  4. scanf("%d %d", &m, &n);
  5. printf("%0*d\n", n, m);
  6. return 0;
  7. }

比较简单

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


 

  1. #include<stdio.h>
  2. #include<math.h>
  3. #define dpi 3.141592653589
  4. #define R 6371
  5. double getpi(int a){
  6. double pi=0.0;
  7. for(int i=0;i<a;i++){
  8. if(i%2==0){
  9. pi+=1.0/(2*i+1);
  10. }
  11. else{
  12. pi-=1.0/(2*i+1);
  13. }
  14. }
  15. return 4*pi;
  16. }
  17. double hav(double a){
  18. double b;
  19. b=(1-cos(a))/2;
  20. return b;
  21. }
  22. double toRadis(double a){
  23. double b,pi;
  24. pi=getpi(100000);
  25. b=a*(dpi/180);
  26. return b;
  27. }
  28. double gethav(double a,double b,double c,double d){
  29. double i;
  30. double e,f,g,h;
  31. e=toRadis(a);
  32. f=toRadis(b);
  33. g=toRadis(c);
  34. h=toRadis(d);
  35. i=hav(e-g)+cos(g)*cos(e)*hav(h-f);
  36. return i;
  37. }
  38. double figureout(double a){
  39. double result;
  40. result=R*acos(1-2*a);
  41. return result;
  42. }
  43. int main(){
  44. double a,b,c,d,e,f;
  45. scanf("%lf %lf%lf%lf",&a,&b,&c,&d);
  46. e=gethav(a,b,c,d);
  47. f=figureout(e);
  48. printf("%.4lfkm",f);
  49. }

输出样例有问题,害人不浅

9.风寒指数


 

  1. #include <stdio.h>
  2. #include <math.h>
  3. int myround (double x){
  4. return (int)(x+0.5);
  5. }
  6. int calculate_wind_chill(double v, double t) {
  7. double wind_chill = 13.12 + 0.6215*t - 11.37*pow(v, 0.16) + 0.3965*t*pow(v, 0.16);
  8. return myround(wind_chill);
  9. }
  10. int main() {
  11. double v, t;
  12. scanf("%lf %lf", &v, &t);
  13. int wind_chill_index = calculate_wind_chill(v, t);
  14. printf("%d\n", wind_chill_index);
  15. return 0;
  16. }

风寒指数的样例输出也有问题,120 35 的输出我的输出是37,但是居然能ac,只能说这个题库还是有待打磨

10.颜色模型转换

  1. #include<stdio.h>
  2. #include<math.h>
  3. double Max(double a,double b,double c){
  4. double temp;
  5. if(a>b){
  6. temp=a;
  7. }
  8. else{
  9. temp=b;
  10. }
  11. if(temp>c){
  12. return temp;
  13. }
  14. else return c;
  15. }
  16. double Min(double a,double b,double c){
  17. double temp;
  18. if(a<b){
  19. temp=a;
  20. }
  21. else {
  22. temp=b;
  23. }
  24. if(temp<c){
  25. return temp;
  26. }
  27. else{
  28. return c;
  29. }
  30. }
  31. double rgbtoRGB(double a){
  32. return a/(255);
  33. }
  34. int main(){
  35. double r,g,b,R,G,B;
  36. scanf("%lf%lf%lf",&r,&g,&b);
  37. R=rgbtoRGB(r);
  38. G=rgbtoRGB(g);
  39. B=rgbtoRGB(b);
  40. double h,s,v;
  41. v=Max(R,G,B);
  42. if(Max(R,G,B)!=0){
  43. s=(Max(R,G,B)-Min(R,G,B))/Max(R,G,B);
  44. }
  45. if(R==Max(R,G,B)&&Max(R,G,B)-Min(R,G,B)!=0){
  46. h=60*((G-B)/(Max(R,G,B)-Min(R,G,B)));
  47. }
  48. if(G==Max(R,G,B)&&Max(R,G,B)-Min(R,G,B)!=0){
  49. h=60*(2+(B-R)/(Max(R,G,B)-Min(R,G,B)));
  50. }
  51. if(B==Max(R,G,B)&&Max(R,G,B)-Min(R,G,B)!=0){
  52. h=60*(4+(R-G)/(Max(R,G,B)-Min(R,G,B)));
  53. }
  54. if(h<0){
  55. h=h+360;
  56. }
  57. if(Max(R,G,B)-Min(R,G,B)==0){
  58. h=0;
  59. }
  60. if(Max(R,G,B)==0)
  61. {
  62. s=0;
  63. }
  64. s=s*100;
  65. v=v*100;
  66. printf("%.4lf,%.4lf%%,%.4lf%%",h,s,v);
  67. }

注意在转换过程中考虑255 255 255,0,0,0等极端情况

11.操作数

  1. #include <stdio.h>
  2. int sum_of_digits(int n) {
  3. int sum = 0;
  4. while (n > 0) {
  5. sum += n % 10;
  6. n /= 10;
  7. }
  8. return sum;
  9. }
  10. int main() {
  11. int n, count = 0;
  12. scanf("%d", &n);
  13. while (n > 0) {
  14. n -= sum_of_digits(n);
  15. count++;
  16. }
  17. printf("%d\n", count);
  18. return 0;
  19. }

12.级数和

  1. #include <stdio.h>
  2. //级数和
  3. double cal(int n) {
  4. double sum = 0;
  5. double sum2=40.4;
  6. if(n<9){
  7. for (int i = 1; i <= n; i++) {
  8. printf("%d.%d",i,i+1);
  9. sum += (i + (i + 1)*0.1);
  10. if(i!=n) printf("+");
  11. }
  12. return sum;
  13. }
  14. if(n>=9&&n!=99){
  15. printf("1.2+2.3+3.4+4.5+5.6+6.7+7.8+8.9+");
  16. for(int m=9;m<=n;m++)
  17. {
  18. if(m%10!=9)
  19. printf("%d.%d",m,m+1);
  20. else printf("%d.%d",m,(m/10)+1);
  21. if(m!=n) printf("+");
  22. sum2+=(m+(m+1)*0.01);
  23. }
  24. return sum2;
  25. }
  26. if(n==99){
  27. cal(98);
  28. printf("+99.1");
  29. return 5003.55;
  30. }
  31. }
  32. int main() {
  33. int n;
  34. scanf("%d", &n);
  35. double result = cal(n);
  36. printf("=%.2lf\n", result);
  37. return 0;
  38. }

这个也没有什么特别好说的,注意9.10输出的时候输出9.1这种情况,还要注意分三段讨论,在9和99分成三段,还是输出不好处理,注意即可

13.组合数

  1. #include<stdio.h>
  2. int count(int a){
  3. int arr[5][51]={0};
  4. for(int i=0;i<=9;i++){
  5. arr[1][i]=1;
  6. }
  7. for(int j=2;j<=4;j++){
  8. for(int m=0;m<=a;m++){
  9. for(int n=0;n<=9;n++){
  10. if(m>=n){
  11. arr[j][m]+=arr[j-1][m-n];
  12. }
  13. }
  14. }
  15. }
  16. return arr[4][a];
  17. }
  18. int main(){
  19. int n;
  20. scanf("%d",&n);
  21. int result;
  22. result =count(n);
  23. printf("%d",result);
  24. }

这个是真的重量级,这个是算法课上讲递归和动态规划的例题,可能是输入只有50不需要考虑时间复杂度问题,但是还是在这里简单讲一下动态规划算法

在我的观点看来,动态规划是一种以空间换时间的算法,他将算法的中间值全部存在一个数组中,以此来避免多次重复计算来拖慢算法速度,但是缺点在于他会用一片更大的空间,导致空间复杂度的提升

以这道题为例,我们简单讨论一下这个题目,

先以一个公式来入手arr[i][j]=\sum\binom{9}{k=0}arr[i-1][j-k],if j>=k
这个公式什么含义呢
要计算使用前 i 个数字构成和为 j 的组合数,我们需要考虑前一个数字的组合数。假设前一个数字的值为 k,那么使用前 i 个数字构成和为 j 的组合数,可以分解成两部分:

1.不包含前一个数字 k 的组合数,也就是使用前 i-1 个数字构成和为 j 的组合数,即 dp[i-1][j]。

2.包含前一个数字 k 的组合数,也就是使用前 i-1 个数字构成和为 j-k 的组合数,即 dp[i-1][j-k]。

所以,通过对所有可能的 k 值进行求和,就能得到使用前 i 个数字构成和为 j 的所有组合数。

利用这个公式可以将整个数组初始化完成,当然,这个题目的范围很小,我们也可以使用递归的方法来解决
 

  1. #include <stdio.h>
  2. int count(int i, int j) {
  3. if (i == 1) {
  4. if (j >= 0 && j <= 9) {
  5. return 1; // 初始化条件:只用一个数字构成和为j的组合数为1
  6. } else {
  7. return 0; // 其他情况下,返回0
  8. }
  9. }
  10. int total = 0;
  11. for (int k = 0; k <= 9; k++) {
  12. if (j >= k) {
  13. total += count(i - 1, j - k);
  14. }
  15. }
  16. return total;
  17. }
  18. int main() {
  19. int n;
  20. scanf("%d", &n);
  21. int result = count(4, n);
  22. printf("%d\n", result);
  23. return 0;
  24. }

递归的优点在于所占空间小,但是会有更长的耗时,如果在算法课程中对这类题使用递归很有可能出现超时的情况

14.方阵

  1. #include<iostream>
  2. using namespace std;
  3. long long int max(long long int a,long long int b)
  4. {
  5. return a>=b?a:b;
  6. }
  7. long long int min(long long int a, long long int b)
  8. {
  9. return a<=b?a:b;
  10. }
  11. int main()
  12. {
  13. long long int n;
  14. cin>>n;
  15. for(long long int i=1;i<=n;i++)
  16. {
  17. for(long long int j=1;j<=n;j++)
  18. {
  19. long long int t=max(i,j)-min(i,j);
  20. cout<<t;
  21. if(j!=n) cout<<" ";
  22. }
  23. if(i!=n)
  24. cout<<endl;
  25. }
  26. }

这个题目要求输出一个方阵,我们以数组表示,则不难发现a[n][n]=0,a[n][n+1]=1......通过找规律我们可以发现每一个元素都是a[i][j]中较大的减去较小的数,输出即可,注意的是输出的方阵没两个数之间有两个空格,这个导致我wa了很多次调试了很多次。

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. int main(){
  5. int ** a;
  6. int length;
  7. int high;
  8. scanf("%d",&high);
  9. length=high;
  10. a=(int **)malloc(length*sizeof(int *));
  11. for(int i=0;i<length;i++){
  12. a[i]=(int *)malloc(high *sizeof(int ));
  13. }
  14. for(int i=0;i<length;i++){
  15. for(int j=0;j<high;j++){
  16. a[i][j]=fabs(i-j);
  17. if(j!=high-1) printf("%d ",a[i][j]);
  18. else{
  19. printf("%d",a[i][j]);
  20. }
  21. }
  22. printf("\n");
  23. }
  24. }

更新过一次题目,这个是现在可以用的答案,

15.比率

  1. #include<stdio.h>
  2. long long int gcd(long long int a,long long int b){
  3. while(b!=0){
  4. int temp=b;
  5. b=a%b;
  6. a=temp;
  7. }
  8. return a;
  9. }
  10. int float_to_fraction(double num){
  11. long long Big=1e9;
  12. if(num==0)
  13. {
  14. printf("0");
  15. return 0;
  16. }
  17. if(num<0){
  18. printf("-");
  19. num=-num;
  20. }
  21. long long num1=num*Big;
  22. long long big =Big;
  23. //用一个很大的数将浮点数转为整数
  24. long long comgcd=gcd(num1,big);
  25. num1 /=comgcd;
  26. big /= comgcd;
  27. printf("%lld/%lld",num1,big);
  28. }
  29. int main(){
  30. double num;
  31. scanf("%lf",&num);
  32. float_to_fraction(num);
  33. return 0;
  34. }

如何将一个小数转换成比率,我们不妨使用一个很大的数字,例如1e9,将其变成一个整数/大数的形式,然后利用辗转相除法找到公因数,最后得到结果,值的注意的是这道题要考虑0和负数的情况。

16.乘数模

  1. #include <stdio.h>
  2. unsigned long long fast_mod_multiply(unsigned long long a, unsigned long long b, unsigned long long m) {
  3. unsigned long long result = 0;
  4. a = a % m;
  5. while (b > 0) {
  6. if (b % 2 == 1) {
  7. result = (result + a) % m;
  8. }
  9. a = (a * 2) % m;
  10. b = b / 2;
  11. }
  12. return result;
  13. }
  14. int main() {
  15. unsigned long long a, b, m;
  16. scanf("%llu", &a);
  17. scanf("%llu", &b);
  18. scanf("%llu", &m);
  19. unsigned long long result = fast_mod_multiply(a, b, m);
  20. printf("%llu\n",result);
  21. return 0;
  22. }

快速模乘法,csdn上有详细解释,不多赘述

17.对称数

  1. #include<stdio.h>
  2. int GetFigure(int a){
  3. int count=0;
  4. do{
  5. count++;
  6. a/=10;
  7. }
  8. while(a!=0);{
  9. return count;
  10. }
  11. }//获取一个数字有几位
  12. void GetFigureToStorage(int a,int arr[],int b){
  13. for(int i=0;i<b;i++){
  14. arr[i]=a%10;
  15. a/=10;
  16. }
  17. }//获取一个数字的各个位数,并存在数组里
  18. int YesOrNo(int arr[],int a){
  19. int count=0;
  20. if(a%2==1){
  21. if(arr[(a+1)/2-1]==1||arr[(a+1)/2-1||arr[(a+1)/2-1]==8]==0){
  22. for(int i=0;i<a;i++){
  23. if(arr[i]==6&&arr[a-i-1]==9){
  24. {
  25. count++;
  26. }
  27. }
  28. if(arr[i]==9&&arr[a-i-1]==6)
  29. {
  30. count++;
  31. }
  32. if(arr[i]==1){
  33. count++;
  34. }
  35. if(arr[i]==0){
  36. count++;
  37. }
  38. if(arr[i]==8){
  39. count++;
  40. }
  41. count++;
  42. }
  43. }
  44. }
  45. if(a%2==0){
  46. for(int j=0;j<a;j++){
  47. if(arr[j]==6&&arr[a-j-1]==9){
  48. {
  49. count++;
  50. }
  51. }
  52. if(arr[j]==9&&arr[a-j-1]==6)
  53. {
  54. count++;
  55. }
  56. if(arr[j]==1){
  57. count++;
  58. }
  59. if(arr[j]==0){
  60. count++;
  61. }
  62. if(arr[j]==8){
  63. count++;
  64. }
  65. }
  66. }
  67. return count;
  68. }
  69. int main(){
  70. int a;
  71. int arr1[32];
  72. int flag=0;
  73. scanf("%d",&a);
  74. int b;
  75. b=GetFigure(a);
  76. GetFigureToStorage( a, arr1, b);
  77. int m;
  78. m=YesOrNo(arr1,b);
  79. if((b%2==0&&m==b)||(b%2==1&&m==2*b))
  80. {
  81. printf("Yes");
  82. }
  83. else{
  84. printf("No");
  85. }
  86. }

我的方法是将每一位都存在一个数组之中,通过判断每一位是否是对称数的情况来判断整个数字是否是对称数,对称数的要求是6和9必须在对称的位置上,其他位必须是1 0 8,通过判断每一位的方法就能得到对称数是否满足。

18.倍数和

  1. #include <stdio.h>
  2. int getMultiplesSum(int n) {
  3. int sum = 0;
  4. for (int i = 1; i < n; i++) {
  5. if (i % 3 == 0 || i % 5 == 0) {
  6. sum += i;
  7. }
  8. }
  9. return sum;
  10. }
  11. int main() {
  12. int T;
  13. scanf("%d", &T);
  14. int arr[1000000];
  15. for (int i = 0; i < T; i++) {
  16. int n;
  17. scanf("%d", &n);
  18. int result = getMultiplesSum(n);
  19. arr[i]=result;
  20. }
  21. for (int m=0;m<T;m++){
  22. printf("%d\n",arr[m]);
  23. }
  24. return 0;
  25. }

通过判断是否是3和5的倍数将其递增,很基础的一道题

19.幂数模

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. long long Mode(long long a, long long b, long long mode)
  4. {
  5. long long sum = 1;
  6. while (b)
  7. {
  8. if (b & 1)
  9. {
  10. sum = (sum * a) % mode;
  11. b--;
  12. }
  13. b /= 2;
  14. a = a * a % mode;
  15. }
  16. cout<<sum;
  17. return 0;
  18. }
  19. int main()
  20. {
  21. long long int a,b,c;
  22. cin>>a>>b>>c;
  23. Mode(a,b,c);
  24. }

快速幂模算法,csdn上有详细说明,可以自己去看

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

  1. #include <stdio.h>
  2. // 辗转相除法求最大公约数
  3. int gcd(int a, int b) {
  4. while (b != 0) {
  5. int temp = b;
  6. b = a % b;
  7. a = temp;
  8. }
  9. return a;
  10. }
  11. // 结构体表示分数
  12. typedef struct {
  13. int numerator; // 分子
  14. int denominator; // 分母
  15. } Fraction;
  16. // 将分数化简为最简分数形式
  17. void simplify_fraction(Fraction *frac) {
  18. int common_divisor = gcd(frac->numerator, frac->denominator);
  19. frac->numerator /= common_divisor;
  20. frac->denominator /= common_divisor;
  21. }
  22. int main() {
  23. Fraction frac1, frac2;
  24. // 输入第一个分数
  25. scanf("%d/%d", &(frac1.numerator), &(frac1.denominator));
  26. // 输入第二个分数
  27. scanf("%d/%d", &(frac2.numerator), &(frac2.denominator));
  28. // 加法
  29. Fraction sum;
  30. sum.numerator = frac1.numerator * frac2.denominator + frac2.numerator * frac1.denominator;
  31. sum.denominator = frac1.denominator * frac2.denominator;
  32. simplify_fraction(&sum);
  33. // 减法
  34. Fraction diff;
  35. diff.numerator = frac1.numerator * frac2.denominator - frac2.numerator * frac1.denominator;
  36. diff.denominator = frac1.denominator * frac2.denominator;
  37. simplify_fraction(&diff);
  38. // 乘法
  39. Fraction product;
  40. product.numerator = frac1.numerator * frac2.numerator;
  41. product.denominator = frac1.denominator * frac2.denominator;
  42. simplify_fraction(&product);
  43. // 除法
  44. Fraction quotient;
  45. quotient.numerator = frac1.numerator * frac2.denominator;
  46. quotient.denominator = frac1.denominator * frac2.numerator;
  47. simplify_fraction(&quotient);
  48. // 输出结果
  49. printf("(%d/%d)+(%d/%d)=%d/%d\n", frac1.numerator, frac1.denominator, frac2.numerator, frac2.denominator, sum.numerator, sum.denominator);
  50. printf("(%d/%d)-(%d/%d)=%d/%d\n", frac1.numerator, frac1.denominator, frac2.numerator, frac2.denominator, diff.numerator, diff.denominator);
  51. printf("(%d/%d)*(%d/%d)=%d/%d\n", frac1.numerator, frac1.denominator, frac2.numerator, frac2.denominator, product.numerator, product.denominator);
  52. printf("(%d/%d)/(%d/%d)=%d/%d\n", frac1.numerator, frac1.denominator, frac2.numerator, frac2.denominator, quotient.numerator, quotient.denominator);
  53. return 0;
  54. }

难点在于辗转相除法求最大公因数,其他的难点不多,只是麻烦,我是用gpt生成的代码,不过很显然这个题gpt的方法有点过于麻烦了,但是能ac就行

21.倒水

  1. #include<stdio.h>
  2. struct createqueue {
  3. int a,b;
  4. };
  5. createqueue queue[10000];
  6. int head,tail;
  7. int chongfu[1000][1000];
  8. int tag;
  9. void in(int n,int m) {
  10. queue[tail].a=n,queue[tail].b=m;
  11. tail++;
  12. }
  13. int out(int i) {
  14. if(i==1)return queue[head].a;
  15. else if(i==2)return queue[head].b;
  16. }
  17. int count;
  18. int maxn,maxm,result;
  19. void bfs() {
  20. int n=out(1),m=out(2);
  21. int t=chongfu[n][m];
  22. //printf("%d%d\n",n,m);
  23. head++;
  24. if(n==result||m==result) {
  25. printf("%d",t-1);
  26. tag=1;
  27. }
  28. for(int i=0; i<=5; i++) {
  29. switch(i){
  30. case 0: {
  31. if(!chongfu[n][0]) {
  32. in(n,0);
  33. chongfu[n][0]=t+1;
  34. };
  35. break;
  36. }
  37. case 1: {
  38. if(!chongfu[0][m]) {
  39. in(0,m);
  40. chongfu[0][m]=t+1;
  41. break;
  42. }
  43. }
  44. case 2: {
  45. if(!chongfu[maxn][m]) {
  46. in(maxn,m);
  47. chongfu[maxn][m]=t+1;
  48. }
  49. break;
  50. }
  51. case 3: {
  52. if(!chongfu[n][maxm]) {
  53. in(n,maxm);
  54. chongfu[n][maxm]=t+1;
  55. }
  56. break;
  57. }
  58. case 4: {
  59. if(n+m>=maxm&&(!chongfu[n+m-maxm][maxm])) {
  60. in(n+m-maxm,maxm);
  61. chongfu[n+m-maxm][maxm]=t+1;
  62. }
  63. if(n+m<maxm&&(!chongfu[0][n+m])) {
  64. in(0,n+m);
  65. chongfu[0][n+m]=t+1;
  66. }
  67. break;
  68. }
  69. case 5: {
  70. if(n+m>=maxn&&(!chongfu[maxn][m+n-maxn])) {
  71. in(maxn,m+n-maxn);
  72. chongfu[maxn][m+n-maxn]=t+1;
  73. }
  74. if(n+m<maxn&&(!chongfu[m+n][0])) {
  75. in(m+n,0);
  76. chongfu[m+n][0]=t+1;
  77. break;
  78. }
  79. }
  80. }
  81. }
  82. }
  83. int main() {
  84. scanf("%d%d%d",&maxn,&maxm,&result);
  85. in(0,0);
  86. chongfu[0][0]=1;
  87. while(head!=tail) {
  88. bfs();
  89. if(tag==1){
  90. //printf("success!\n");
  91. return 0;
  92. }
  93. }
  94. //printf("failed!");
  95. }

22.方案数

  1. #include <stdio.h>
  2. int countWays(int N) {
  3. int count = 0;
  4. for (int i = 1; i <= N/2; i++) {
  5. int currentSum = i;
  6. int j = i + 1;
  7. while (currentSum < N) {
  8. currentSum += j;
  9. j++;
  10. }
  11. if (currentSum == N) {
  12. count++;
  13. }
  14. }
  15. return count + 1; // 加上N本身作为一种方案
  16. }
  17. int main() {
  18. int N;
  19. scanf("%d", &N);
  20. int result = countWays(N);
  21. printf("%d\n", result);
  22. return 0;
  23. }

个人觉得很简单

23.好数字

  1. #include <iostream>
  2. using namespace std;
  3. #define MOD 1000000007
  4. long long quick_mul(int x, long long N)
  5. {
  6. long long res = 1;
  7. long long x_contribute = x;
  8. while (N > 0) {
  9. if (N % 2 == 1) {
  10. res = res * x_contribute % MOD;
  11. }
  12. x_contribute = x_contribute * x_contribute % MOD;
  13. N /= 2;
  14. }
  15. return res;
  16. }
  17. int countGoodNumbers(long long n)
  18. {
  19. return quick_mul(5, n - n / 2) * quick_mul(4, n / 2) % MOD;
  20. }
  21. int main()
  22. {
  23. long long n;
  24. // 获取用户输入
  25. cin >> n;
  26. // 调用 countGoodNumbers 函数
  27. int result = countGoodNumbers(n);
  28. // 输出结果
  29. cout << result << endl;
  30. return 0;
  31. }

类似于排列组合的一道题,给定奇数位和偶数位,然后自己排列组合看有多少种可能性

24.余数和

  1. #include<stdio.h>
  2. int sum(int n,int k){
  3. int sum=0;
  4. for(int i=1;i<=n;i++){
  5. sum=sum+k%i;
  6. }
  7. return sum;
  8. }
  9. int main(){
  10. int a,b;
  11. scanf("%d%d",&a,&b);
  12. printf("%d",sum(a,b));
  13. }

记录余数然后求和,比较简单

25.最大数字

  1. #include<stdio.h>
  2. void GetFigureToStorage(int a,int arr[],int b){
  3. for(int i=0;i<b;i++){
  4. arr[i]=a%10;
  5. a/=10;
  6. }
  7. }//获取一个数字的各个位数,并存在数组里
  8. int GetFigure(int a){
  9. int count=0;
  10. do{
  11. count++;
  12. a/=10;
  13. }
  14. while(a!=0);{
  15. return count;
  16. }
  17. }//获取一个数字有几位
  18. int isincrease(int a){
  19. int b=GetFigure(a);
  20. int arr[32];
  21. int count=0;
  22. GetFigureToStorage(a,arr,b);
  23. for(int i=0;i<b-1;i++){
  24. if(arr[i+1]-arr[i]>0){
  25. count++;
  26. }
  27. }
  28. if(count==0){
  29. return 1;
  30. }
  31. else{
  32. return 0;
  33. }
  34. }
  35. int main(){
  36. int a;
  37. scanf("%d",&a);
  38. int i=a;
  39. if(i<10){
  40. printf("%d",i);
  41. }
  42. if(i==10){
  43. printf("9");
  44. }
  45. if(i==11){
  46. printf("9");
  47. }
  48. if(i>=12){
  49. while(1){
  50. if(isincrease(i)==1){
  51. printf("%d",i);
  52. break;
  53. }
  54. i--;
  55. }
  56. }
  57. }

我的算法是从给定的数开始,判断其是否为所需数字,否则就减一继续判断,缺点在于时间复杂度过高,这个题的测试样例很显然没有很恶心人的,所以能通过,但是依然存在时间复杂度过高的情况,最好还是用贪心写

26.查找数列

  1. #include <stdio.h>
  2. int findNumber(int n) {
  3. int current = 1; // 初始值为1
  4. int i, j;
  5. for (i = 1; i <= n; i++) {
  6. for (j = 1; j <= i; j++) {
  7. if (current == n) {
  8. return j;
  9. }
  10. current++;
  11. }
  12. }
  13. return -1; // 如果未找到,返回-1
  14. }
  15. int main() {
  16. int n;
  17. scanf("%d", &n);
  18. int result = findNumber(n);
  19. printf("%d\n",result);
  20. return 0;
  21. }

27.竖式计算

  1. #include<stdio.h>
  2. int GetFigure(int a){
  3. int count=0;
  4. do{
  5. count++;
  6. a/=10;
  7. }
  8. while(a!=0);{
  9. return count;
  10. }
  11. }//获取一个数字有几位
  12. void GetFigureToStorage(int a,int arr[],int b){
  13. for(int i=0;i<b;i++){
  14. arr[i]=a%10;
  15. a/=10;
  16. }
  17. }//获取一个数字的各个位数,并存在数组里
  18. void figuermti(int arr[],int a,int c,int e){
  19. int arr1[32];
  20. int count;
  21. for(int i=0;i<c;i++){
  22. arr1[i]=arr[i]*a;
  23. }
  24. count=GetFigure(arr1[c-1])+c;
  25. for(int m=0;m<count-GetFigure(a);m++){
  26. printf(" ");
  27. }
  28. printf("%d\n",a);
  29. printf("x");
  30. for(int n=0;n<count-GetFigure(e)-1;n++){
  31. printf(" ");
  32. }
  33. printf("%d\n",e);
  34. for(int n=0;n<count;n++){
  35. printf("-");
  36. }
  37. printf("\n");//前3行
  38. for(int i=0;i<c-1;i++){
  39. for(int m=0;m<count-GetFigure(arr1[i])-i;m++) {
  40. printf(" ");
  41. }
  42. printf("%d",arr1[i]);
  43. if(i==0){
  44. printf("\n");
  45. }
  46. else {
  47. for(int n=0;n<i;n++){
  48. printf(" ") ;
  49. }
  50. printf("\n");
  51. }
  52. }
  53. printf("+");
  54. printf("%d",arr1[c-1]);
  55. for(int i=0;i<c-1;i++){
  56. printf(" ");
  57. }
  58. printf("\n");
  59. for(int n=0;n<count;n++){
  60. printf("-");
  61. }
  62. printf("\n");
  63. for(int m=0;m<count-GetFigure(a*e);m++){
  64. printf(" ");
  65. }
  66. printf("%d",a*e);
  67. }
  68. int main(){
  69. int a,b,c;
  70. scanf("%d%d",&a,&b);
  71. int arr[32];
  72. c=GetFigure(b);
  73. GetFigureToStorage(b,arr,c);
  74. figuermti(arr,a,c,b);
  75. }

计算五分钟,排版两小时

28.俄罗斯农夫乘法

  1. #include <stdio.h>
  2. int Mul(int m, int n)
  3. {
  4. int sum = 0, a = 0;
  5. if (n == 0 || m == 0)
  6. return 0;
  7. if (n == 1)
  8. return m;
  9. printf("%d %d\n", n, m); // 输出当前的 n 和 m
  10. while (n != 1)
  11. {
  12. if (n % 2 == 0)
  13. {
  14. n = n / 2;
  15. m *= 2;
  16. }
  17. else
  18. {
  19. n = n / 2;
  20. a += m;
  21. m *= 2;
  22. }
  23. printf("%d %d\n", n, m); // 输出当前的 n 和 m
  24. }
  25. sum = a + m;
  26. return sum;
  27. }
  28. int main()
  29. {
  30. int m, n; // 两个相乘的数
  31. int sum = 0;
  32. scanf("%d %d", &m, &n);
  33. sum = Mul(n, m);
  34. printf("%d\n", sum);
  35. return 0;
  36. }

利用题给的算法计算输出即可

29.阶乘倍数

  1. #include<iostream>
  2. using namespace std;
  3. #define mem(a,b) memset(a,b,sizeof(a))
  4. #define pb push_back
  5. const int INF = 1e21;
  6. typedef long long ll;
  7. const int maxn = 1e4 + 7;
  8. long long int t,p;
  9. bool isPrime(int p){
  10. if(p == 1) return false;
  11. for (int i = 2; i*i <= p; i++) {
  12. if(p % i == 0) return false;
  13. }
  14. return true;
  15. }
  16. int gcd(int a,int b){
  17. if(b == 0) return a;
  18. return gcd(b,a%b);
  19. }
  20. int main(){
  21. cin>>p;
  22. if (p == 1 || isPrime(p)) {
  23. cout<<p;
  24. }
  25. else{
  26. for (int i = 2; ; i++) {
  27. if (gcd(i,p) != 1) {
  28. p /= gcd(i,p);
  29. if (p == 1) {
  30. cout<<i;
  31. break;
  32. }
  33. if (i < p && isPrime(p)) {
  34. cout<<p;
  35. break;
  36. }
  37. }
  38. }
  39. }
  40. return 0;
  41. }

https://blog.csdn.net/weixin_35105950/article/details/117115648?app_version=6.1.7&csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22117115648%22%2C%22source%22%3A%22m0_54007497%22%7D&utm_source=app

30.毕达哥拉斯三元组

  1. #include <stdio.h>
  2. void findPythagoreanTriplets(int n) {
  3. int a, b, c;
  4. int found = 0;
  5. for (a = 1; a <= n; a++) {
  6. for (b = a; b <= n; b++) {
  7. c = n - a - b;
  8. if (c >= b && a*a + b*b == c*c) {
  9. printf("%d\n", a * b * c);
  10. found = 1;
  11. break;
  12. }
  13. }
  14. if (found == 1) {
  15. break;
  16. }
  17. }
  18. if (found == 0) {
  19. printf("没有找到满足条件的三个数字。\n");
  20. }
  21. }
  22. int main() {
  23. int n;
  24. scanf("%d", &n);
  25. findPythagoreanTriplets(n);
  26. return 0;
  27. }

找到这三个数然后算乘积即可。

31.基思数

  1. #include <stdio.h>
  2. int arr[8] = {0};
  3. int init(int n){
  4. int cnt = 0;
  5. while (n) {
  6. arr[cnt++] = n%10;
  7. n /= 10;
  8. }
  9. return cnt;
  10. }
  11. void isKeith(int n, int len){
  12. int i = len - 1;
  13. while (arr[i] < n){
  14. int sum = 0;
  15. for (int j = 0; j < len; ++j) {
  16. sum += arr[(i-j+len)%len];
  17. }
  18. arr[i] = sum;
  19. i = (i-1+len)%len;
  20. }
  21. if (arr[i] == n) printf("Yes");
  22. else printf("No");
  23. }
  24. int main() {
  25. int n;
  26. scanf("%d",&n);
  27. isKeith(n,init(n));
  28. return 0;
  29. }

这道题我不知道为什么 不能ac

这里采用了另一位同学@annesede的答案,

http://【2023西工大NOJ (C语言版) 持续更新ing - CSDN App】http://t.csdnimg.cn/tD4CV

我本人的做法也放在这里,但是不能ac

  1. #include<stdio.h>
  2. int GetFigure(int a){
  3. int count=0;
  4. do{
  5. count++;
  6. a/=10;
  7. }
  8. while(a!=0);{
  9. return count;
  10. }
  11. }//获取一个数字有几位
  12. void GetFigureToStorage(int a,int arr[],int b){
  13. for(int i=b-1;i>=0;i--){
  14. arr[i]=a%10;
  15. a/=10;
  16. }
  17. }//获取一个数字的各个位数,并存在数组里
  18. int main(){
  19. int a,flag;
  20. flag=0;
  21. scanf("%d",&a);
  22. int arr[30];
  23. int length=GetFigure(a);
  24. GetFigureToStorage( a,arr,length);
  25. for(int i=length;i<30;i++){
  26. for(int j=1;j<=length;j++){
  27. arr[i]+=arr[i-j];
  28. }
  29. }
  30. for(int m=0;m<30;m++){
  31. if(a==arr[m]){
  32. printf("Yes");
  33. flag=1;
  34. break;
  35. }
  36. }
  37. if(flag==0){
  38. printf("No");
  39. }
  40. }

  1. #include<stdio.h>
  2. int main(){
  3. long long arr[55]={14, 19, 28, 47, 61, 75, 197, 742, 1104, 1537, 2208, 2580, 3684, 4788, 7385, 7647, 7909, 31331, 34285, 34348, 55604, 62662, 86935, 93993, 120284, 129106, 147640, 156146, 174680, 183186, 298320, 355419, 694280, 925993, 1084051, 7913837, 11436171, 33445755, 44121607, 129572008, 251133297, 24769286411, 96189170155, 171570159070, 202366307758, 239143607789, 296658839738, 1934197506555, 8756963649152, 43520999798747, 74596893730427, 97295849958669, 120984833091531, 270585509032586, 754788753590897};
  4. long long a;
  5. int b=0;
  6. scanf("%lld",&a);
  7. for(int i=0;i<55;i++){
  8. if(arr[i]==a){
  9. printf("Yes");
  10. b=1;
  11. }
  12. }
  13. if(b==0){
  14. printf("No");
  15. }
  16. }

32.哈沙德数

  1. #include <stdio.h>
  2. int HarshadNumber(int n){
  3. int t = n, s = 0;
  4. while (t) {
  5. s += t % 10;
  6. t /= 10;
  7. }
  8. if ((s == 0) || (n % s != 0)) return 0;
  9. if (s == 1) return 1;
  10. return n / s;
  11. }
  12. int main(){
  13. int a;
  14. int count=0;
  15. scanf("%d", &a);
  16. if (a == 1) {
  17. count = 1;
  18. }
  19. while ((a != 0) && (a != 1)) {
  20. a= HarshadNumber(a);
  21. if (a) {
  22. count++;
  23. }
  24. }
  25. printf("%d", count);
  26. return 0;
  27. }

出题人给了函数,调用即可

33.运动会

  1. #include <bits/stdc++.h>
  2. #define MAXN 40000 + 10
  3. int phi[MAXN],ans[MAXN];
  4. void euler()
  5. {
  6. ans[1] = 1;
  7. for (int i = 1; i <= MAXN; i++)
  8. {
  9. int res = i, n = i;
  10. for (int j = 2; j * j <= n; j++)
  11. {
  12. if (!(n % j))
  13. res = res * (j - 1) / j;
  14. while (!(n % j))
  15. n /= j;
  16. }
  17. if (n > 1)
  18. res = res * (n - 1) / n;
  19. phi[i] = res;
  20. }
  21. for (int i = 2; i <= MAXN; i++)
  22. for (int j = 1; j < i; j++)
  23. ans[i] += phi[j];
  24. }
  25. int main(int argc, char const *argv[])
  26. {
  27. euler();
  28. int n;
  29. while (~scanf("%d", &n))
  30. {
  31. if (n == 1) //方阵大小为1*1时特判,此时队列中只有自己,输出0
  32. {
  33. puts("0");
  34. continue;
  35. }
  36. printf("%d\n", 2 * ans[n] + 1);
  37. }
  38. return 0;
  39. }

这里其实是看每一个人所在的x,y是否互质;但是我采用的方法时间复杂度过高te了几次也懒得改了,就采用了别人写好的,连接如下http://【P2158 [SDOI2008]仪仗队 题解 - CSDN App】http://t.csdnimg.cn/osuME

34.可变参数平均

  1. #include<stdio.h>
  2. #include<stdarg.h>
  3. double avg(int count,...){
  4. va_list args;
  5. va_start(args,count);
  6. double sum=0;
  7. double avg;
  8. for(int i=0;i<count;i++){
  9. int num=va_arg(args,double);
  10. sum=sum+num;
  11. }
  12. avg=sum/count;
  13. return avg;
  14. }
  15. int main(){
  16. double avg1;
  17. double a,b,c,d,e;
  18. scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e);
  19. avg1=avg(2,a,b)-avg(3,c,d,e);
  20. printf("%.4lf",avg1);
  21. }

这个问题读懂stdarg.h如何使用即可,还算简单

35.可变参数累加

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. int sum(int first, ...) {
  4. int result = first;
  5. va_list args;
  6. va_start(args, first);
  7. int num;
  8. while ((num = va_arg(args, int)) > 0) {
  9. result += num;
  10. }
  11. va_end(args);
  12. return result;
  13. }
  14. int main() {
  15. int a, b, c, d, e, f;
  16. scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
  17. int result = sum(a, b, 0) - sum(c, d, e, f, 0);
  18. printf("%d", result);
  19. return 0;
  20. }

类似上个题目

36.二进制表示

  1. #include <stdio.h>
  2. void binary_expression(int n) {
  3. if (n == 0) {
  4. return;
  5. }
  6. if (n == 1) {
  7. printf("2(0)");
  8. return;
  9. }
  10. if (n == 2) {
  11. printf("2");
  12. return;
  13. }
  14. int k = 0;
  15. while ((1 << (k + 1)) <= n) {
  16. k++;
  17. }
  18. if (k == 1) {
  19. printf("2");
  20. } else {
  21. printf("2(");
  22. binary_expression(k);
  23. printf(")");
  24. }
  25. if (n - (1 << k) != 0) {
  26. printf("+");
  27. binary_expression(n - (1 << k));
  28. }
  29. }
  30. int main() {
  31. int n;
  32. scanf("%d", &n);
  33. binary_expression(n);
  34. return 0;
  35. }

37.冰雹序列

  1. #include<stdio.h>
  2. int fun(int a){
  3. if(a%2==0){
  4. a=a/2;
  5. if(a!=1){
  6. printf("%d ",a);
  7. }
  8. }
  9. if(a%2==1&a!=1){
  10. a=a*3+1;
  11. if(a!=1){
  12. printf("%d ",a);
  13. }
  14. }
  15. if(a==1){
  16. return 1;
  17. }
  18. return fun(a);
  19. }
  20. int main(){
  21. int a;
  22. scanf("%d",&a);
  23. printf("%d ",a);
  24. fun(a);
  25. printf("1");
  26. }

很简单的递归

38.光线追踪

  1. #include<stdio.h>
  2. long long solve(int a, int b) {
  3. if (a < b) {
  4. int t = a;
  5. a = b;
  6. b = t;
  7. }
  8. if (a % b == 0) {
  9. return a + a - b;
  10. }
  11. return a / b * (b + b) + solve(a % b, b);
  12. }
  13. int main() {
  14. int n, x;
  15. scanf("%d%d", &n, &x);
  16. printf("%lld", solve(x, n - x) + n);
  17. }

这个题感谢@Rrx050212同学的思路提供,如果看不懂的话可以试试这么画图

个人感觉这个题的推导过程很有意思,不妨自己推导一下

39.佩尔数

  1. #include<stdio.h>
  2. int main(){
  3. int a[1000];
  4. a[0]=0;
  5. a[1]=1;
  6. for(int i=2;i<1000;i++){
  7. a[i]=2*a[i-1]+a[i-2];
  8. }
  9. int b;
  10. scanf("%d",&b);
  11. printf("%d",a[b]);
  12. }

本人比较懒狗,没有采用老师给的函数,直接写的佩尔数,我有罪

40.素数

  1. #include <stdio.h>
  2. int is_prime(int num) {
  3. if (num < 2) {
  4. return 0;
  5. }
  6. for (int i = 2; i * i <= num; i++) {
  7. if (num % i == 0) {
  8. return 0;
  9. }
  10. }
  11. return 1;
  12. }
  13. int main(){
  14. int a,b;
  15. int count=0;
  16. scanf("%d%d",&a,&b);
  17. for(int i=a;i<b;i++){
  18. if(is_prime(i)==1){
  19. count++;
  20. }
  21. }
  22. printf("%d",count);
  23. }

依旧是简单粗暴的方法,对每个数进行一次素数判断,如果符合就count++

41.完美矩阵

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. int isperfectarr(int** arr, int a, int b) {
  5. int count = 0;
  6. int num1 = 0;
  7. int num0 = 0;
  8. for (int i = 0; i < a; i++) {
  9. for (int j = 0; j < b; j++) {
  10. if (i == 0 || j == 0 || i == a - 1 || j == b - 1) {
  11. if (arr[i][j] == 1) {
  12. count++;
  13. }
  14. }
  15. }
  16. }
  17. if (count == a * b - (a - 2) * (b - 2)) {
  18. for (int i = 1; i < a - 1; i++) {
  19. for (int j = 1; j < b - 1; j++) {
  20. if (arr[i][j] == 1) {
  21. num1++;
  22. }
  23. else {
  24. num0++;
  25. }
  26. }
  27. }
  28. if (abs(num0 - num1) <= 1) {
  29. return 1;
  30. }
  31. }
  32. return 0;
  33. }
  34. int main() {
  35. int** a;
  36. int length;
  37. int high;
  38. int count = 0;
  39. scanf("%d", &high);
  40. scanf("%d", &length);
  41. a = (int**)malloc(length * sizeof(int*));
  42. for (int i = 0; i < length; i++) {
  43. a[i] = (int*)malloc(high * sizeof(int));
  44. }
  45. for (int i = 0; i < length; i++) {
  46. for (int j = 0; j < high; j++) {
  47. scanf("%d", &a[i][j]);
  48. }
  49. }
  50. for (int i = 2; i <= length; i++) {
  51. for (int j = 2; j <= high; j++) {
  52. for (int m = 0; m <= length - i; m++) {
  53. for (int n = 0; n <= high - j; n++) {
  54. int** subarr = (int**)malloc(i * sizeof(int*));
  55. for (int k = 0; k < i; k++) {
  56. subarr[k] = (int*)malloc(j * sizeof(int));
  57. }
  58. for (int x = 0; x < i; x++) {
  59. for (int y = 0; y < j; y++) {
  60. subarr[x][y] = a[m + x][n + y];
  61. }
  62. }
  63. if (isperfectarr(subarr, i, j) == 1) {
  64. count++;
  65. }
  66. for (int k = 0; k < i; k++) {
  67. free(subarr[k]);
  68. }
  69. free(subarr);
  70. }
  71. }
  72. }
  73. }
  74. printf("%d\n", count);
  75. for (int i = 0; i < length; i++) {
  76. free(a[i]);
  77. }
  78. free(a);
  79. return 0;
  80. }

最近比较忙,不想深究这个题,采用了@annesede的方法,这个题的说明有点问题,所有的完美矩阵都应该是方阵,基于这一点修改之前的代码应该是可以过的,但是懒得改了

https://blog.csdn.net/annesede/article/details/133761873?spm=1001.2014.3001.5501

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #define MAXSIZE 301
  4. int arr[MAXSIZE][MAXSIZE] = {0};
  5. int preSum[MAXSIZE][MAXSIZE] = {0};
  6. void prefix(int n, int m){
  7. for (int i = 1; i <= n; ++i) {
  8. for (int j = 1; j <= m; ++j) {
  9. preSum[i][j] = preSum[i - 1][j] + preSum[i][j - 1]
  10. - preSum[i - 1][j - 1] + arr[i][j];
  11. }
  12. }
  13. }
  14. int getSum(int x1, int x2, int y1, int y2) {
  15. return preSum[x2][y2] - preSum[x1 - 1][y2] - preSum[x2][y1 - 1]
  16. + preSum[x1 - 1][y1 - 1];
  17. }
  18. bool isPerfect(int x1, int x2, int y1, int y2) {
  19. int outer = getSum(x1, x2, y1, y2), inner;
  20. int len = 2 * (x2 - x1 + y2 - y1);
  21. if ((x2 - x1) == 1 || (y2 - y1) == 1) inner = 0;
  22. else inner = getSum(x1 + 1, x2 - 1, y1 + 1, y2 - 1);
  23. if (inner != 1 && inner != 0 && inner != -1) return false;
  24. if ((outer - inner) != len) return false;
  25. return true;
  26. }
  27. int perfectNum(int n, int m) {
  28. int cnt = 0;
  29. for (int i = 1; i <= n; ++i) {
  30. for (int j = 1; j <= m; ++j) {
  31. for (int k = 1; k + i <= n && k + j <= m; ++k) {
  32. if (arr[i][k + j] == 0 || arr[k + i][j] == 0) break;
  33. if (isPerfect(i, i + k, j, j + k)) {
  34. ++cnt;
  35. }
  36. }
  37. }
  38. }
  39. return cnt;
  40. }
  41. int main () {
  42. int n, m;
  43. scanf("%d %d", &n, &m);
  44. for (int i = 1; i <= n; ++i) {
  45. for (int j = 1; j <= m; ++j) {
  46. scanf("%d", &arr[i][j]);
  47. if (arr[i][j] == 0) arr[i][j] = -1;
  48. }
  49. }
  50. prefix(n ,m);
  51. printf("%d", perfectNum(n, m));
  52. return 0;
  53. }

历所有可能的矩阵,但是wa,可能是超时导致的,需要换算法,但我懒得换了

42.货运优化

  1. #include<stdio.h>
  2. #include<math.h> // 导入math.h头文件
  3. #define scanf_s scanf
  4. int getcount(int* a) {
  5. int count = 0;
  6. int count1, count2, count3,count4=0,count5=0;
  7. count = a[5];//36占一个包裹
  8. count1 = a[4];//25占一个包裹
  9. count2 = a[3];//16占的包裹
  10. count3 = ceil(a[2]/4.0);//9占的包裹
  11. //讨论25和1组合后的1剩余数量
  12. int empty;//空余的
  13. empty = count1*36-a[4]*25;
  14. int residue;//剩余容量
  15. residue = empty - a[0];
  16. if (residue >= 0) {
  17. a[0] = 0;
  18. }
  19. else {
  20. a[0] = abs(residue);
  21. }
  22. //此时a[0]剩余a[0]个
  23. //讨论16和4组合后的余量
  24. int empty1;//空余的
  25. empty1 = count2 * 36 - a[3] * 16;
  26. int residue1;//剩余容量
  27. residue1 = empty1 - 4*a[1];
  28. if (residue >= 0) {
  29. a[1] = 0;
  30. }//如果还有剩余容量则a[1]装满
  31. else {
  32. a[1] = abs(residue)/4;// 剩余的a[1]数量
  33. }
  34. if (a[0] + a[1] != 0) {
  35. int empty2;//空余的
  36. empty2 = count3* 36 - a[2] * 9;
  37. int residue2;//剩余容量
  38. residue2 = empty2 - 4 * a[1];
  39. if (residue2 >= 0) {
  40. a[1] = 0;
  41. residue2 = residue2 - a[0];
  42. if (residue2 >= 0) {
  43. a[0] = 0;
  44. }
  45. else {
  46. a[0] = abs(residue2);
  47. count5 = ceil(a[0] / 36.0);
  48. a[0] = 0;//如果a[0]有多的就再分配一个包裹
  49. }
  50. }
  51. else {
  52. a[1] = abs(residue) / 4;
  53. }
  54. }//如果a[0]和a[1]有余量则放到9占的包裹的剩余之中
  55. if (a[0] + a[1] != 0) {
  56. count4 = ceil(a[1] / 9.0);// 为多余的4分配背包
  57. a[1] = 0;
  58. residue = count4 * 36 - 4 * a[1];
  59. if (residue >= 0) {
  60. a[0] = 0;
  61. }
  62. else {
  63. a[0] = abs(residue);
  64. count5 = ceil(a[0] / 36.0);
  65. a[0] = 0;
  66. }
  67. }
  68. if (a[0] + a[1] == 0) {
  69. /*printf("count=%d\n", count);
  70. printf("count1=%d\n", count1);
  71. printf("count2=%d\n", count2);
  72. printf("count3=%d\n", count3);
  73. printf("count4=%d\n", count4);
  74. printf("count5=%d\n", count5);
  75. */
  76. return count + count1 + count2 + count3 + count4 + count5;
  77. }//已经全部分配完,可以返回了
  78. }
  79. int main() {
  80. int a[6];
  81. int n;
  82. int count = 0;
  83. int b[1000] = { 0 };
  84. while (1) {
  85. for (int i = 0; i < 6; i++) {
  86. scanf_s("%d", &a[i]);
  87. }
  88. if (a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0 && a[5] == 0) {
  89. break;
  90. }
  91. b[count] = getcount(a);
  92. count++;
  93. }
  94. for (int i = 0; i < count; i++) {
  95. printf("%d\n", b[i]);
  96. }
  97. return 0;
  98. }

用的贪心做的,注释写的很详细,根据注释来就行

43.波士顿房价预测

  1. #include <stdio.h>
  2. void linear_regression(int n, double x[], double y[]) {
  3. double sum_x = 0, sum_y = 0, sum_xy = 0, sum_xx = 0;
  4. for (int i = 0; i < n; i++) {
  5. sum_x += x[i];
  6. sum_y += y[i];
  7. sum_xy += x[i] * y[i];
  8. sum_xx += x[i] * x[i];
  9. }
  10. double mean_x = sum_x / n;
  11. double mean_y = sum_y / n;
  12. double slope = (sum_xy - n * mean_x * mean_y) / (sum_xx - n * mean_x * mean_x);
  13. double intercept = mean_y - slope * mean_x;
  14. printf("Y=%.4f+%.4f*X\n", intercept, slope);
  15. }
  16. int main() {
  17. int n;
  18. scanf("%d", &n);
  19. double x[n], y[n];
  20. for (int i = 0; i < n; i++) {
  21. scanf("%lf %lf", &x[i], &y[i]);
  22. }
  23. linear_regression(n, x, y);
  24. return 0;
  25. }

题干太长直接没看,用的线性回归,如果有问题的话不如问问高中数学老师;

44.素数筛法

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. int countPrimes(int n) {
  4. if (n <= 2) {
  5. return 0;
  6. }
  7. bool isPrime[n];
  8. for (int i = 2; i < n; i++) {
  9. isPrime[i] = true;
  10. }
  11. for (int i = 2; i * i < n; i++) {
  12. if (isPrime[i]) {
  13. for (int j = i * i; j < n; j += i) {
  14. isPrime[j] = false;
  15. }
  16. }
  17. }
  18. int count = 0;
  19. for (int i = 2; i < n; i++) {
  20. if (isPrime[i]) {
  21. count++;
  22. }
  23. }
  24. return count;
  25. }
  26. int main() {
  27. int n;
  28. scanf("%d", &n);
  29. int count = countPrimes(n);
  30. printf("%d",count);
  31. return 0;
  32. }

筛素数,感觉不像是这个时候该出的题

45.稀疏矩阵

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int main(){
  4. int m;int n;
  5. int count=0;
  6. scanf("%d%d",&m,&n);
  7. int **arr;
  8. arr=(int **)malloc(m*sizeof(int *));
  9. for(int i=0;i<m;i++){
  10. arr[i]=(int *)malloc(n*sizeof(int));
  11. }
  12. for(int i=0;i<m;i++){
  13. for(int j=0;j<n;j++){
  14. scanf("%d",&arr[i][j]);
  15. if(arr[i][j]!=0){
  16. count++;
  17. }
  18. }
  19. }
  20. if(count<=m||count<=n||count<=0.05*(m*n)){
  21. printf("Yes");
  22. }
  23. else{
  24. printf("No");
  25. }
  26. }

输入的时候检查一下是否非零然后和题干对比一下即可,很简单

46.回文数之和

  1. #include<stdio.h>
  2. bool isPalindrome(int num, int base) {
  3. int originalNum = num;
  4. int reversedNum = 0;
  5. while (num > 0) {
  6. reversedNum = reversedNum * base + num % base;
  7. num /= base;
  8. }
  9. return originalNum == reversedNum;
  10. }
  11. int main() {
  12. int a, b,sum=0;
  13. scanf("%d%d", &a, &b);
  14. for (int i = 0; i < a; i++) {
  15. if (isPalindrome(i,b)&& isPalindrome(i,10)) {
  16. sum=sum+i;
  17. }
  18. }
  19. printf("%d", sum);
  20. }

判断一个数是否是回文数然后求和

47.航空旅行

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. #include<vector>
  6. using namespace std;
  7. bool cantake(int a, int b, int c, int d, int e) {
  8. int arr1[3] = { a,b,c };
  9. int arr2[2] = { d,e };
  10. sort(arr1, arr1 + 3);
  11. sort(arr2, arr2 + 2);
  12. if ((arr1[2] + arr1[1]) <= arr2[1] && arr1[0] <= arr2[0]) {
  13. return true;
  14. }
  15. else {
  16. return false;
  17. }
  18. }
  19. int main() {
  20. int n;
  21. scanf("%d", &n);
  22. vector<bool> arr(n);
  23. int a, b, c;
  24. int d, e;
  25. for (int i = 0; i < n; i++) {
  26. scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
  27. arr[i] = cantake(a, b, c, d, e);
  28. }
  29. for (int i = 0; i < n; i++) {
  30. if (arr[i]) {
  31. printf("YES\n");
  32. }
  33. else {
  34. printf("NO\n");
  35. }
  36. }
  37. }

难得一见的简单题,把物品重量排个序然后看能不能托运和随身携带

48.蒙特卡罗方法求积分

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. #include<vector>
  6. //#define scanf scanf_s
  7. using namespace std;
  8. double f(double x,int i) {
  9. if (i == 1) {
  10. return pow(x, 4) * exp(-x);
  11. }
  12. if (i == 2) {
  13. return pow(x, 2) + 1;
  14. }
  15. if (i == 3) {
  16. return cos(x);
  17. }
  18. if (i == 4)
  19. return pow(x, 1 / 2) * (x - 2);
  20. if (i == 5) {
  21. return 2 * sin(x) - 5 * cos(x);
  22. }
  23. }
  24. double integral(int m,double a, double b, int n) {
  25. double sum = 0.0;
  26. double x, y;
  27. srand(RAND_MAX);
  28. for (int i = 0; i < n; i++) {
  29. x = a + (b - a) * rand() / RAND_MAX;
  30. y = f(x,m);
  31. sum += y;
  32. }
  33. return (b - a) * sum / n;
  34. }
  35. int main() {
  36. double a, b;
  37. int n,m;
  38. scanf("%d", &m);
  39. scanf("%lf", &a);
  40. scanf("%lf", &b);
  41. scanf("%d", &n);
  42. double result = integral(m,a, b, n);
  43. printf("%.6lf.\n", result);
  44. return 0;
  45. }

我不知道怎么改这个题,反正做不到样例输出,反而和别人有一样的错误答案

49.行列式

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. int** malloc2D(int n) {
  5. int** a;
  6. a = (int**)malloc(n * sizeof(int*));
  7. for (int i = 0; i < n; i++)
  8. {
  9. a[i] = (int*)malloc(n * sizeof(int));
  10. }
  11. return a;
  12. }
  13. int Minor(int** arr1, int i, int n);
  14. int DET(int** arr1, int n)
  15. {
  16. int i, M, sum = 0;//i是第一行的列指标,M是余子式的值,sum是行列式的计算值
  17. if (n == 1)//一阶行列式直接得出结果
  18. return arr1[0][0];
  19. else if (n > 1)
  20. {
  21. for (i = 0; i < n; i++)//按第一行展开
  22. {
  23. M = Minor(arr1, i, n);
  24. sum += pow(-1, i + 2) * arr1[0][i] * M;
  25. }
  26. }
  27. return sum;
  28. }
  29. int Minor(int** arr1, int i, int n)
  30. {
  31. int j, k, result;
  32. int** arr2=malloc2D(n);
  33. for (j = 0; j < n - 1; j++)
  34. {
  35. for (k = 0; k < n - 1; k++)
  36. {
  37. if (k < i)
  38. arr2[j][k] = arr1[j + 1][k];
  39. else if (k >= i)
  40. arr2[j][k] = arr1[j + 1][k + 1];
  41. }
  42. }
  43. return DET(arr2, n - 1);
  44. }
  45. int main() {
  46. int n;
  47. scanf("%d", &n);
  48. int** a = malloc2D(n);
  49. for (int i = 0; i < n; i++) {
  50. for (int j = 0; j < n; j++) {
  51. scanf("%d", &a[i][j]);
  52. }
  53. }
  54. printf("%d",DET(a,n);
  55. }

按行按列展开行列式,递归,行列式的计算就不赘述了,自己查资料

50.飞机起飞速度

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

字符串这部分我不打算用c语言来写,个人觉得C语言写这个纯粹是折磨自己

51.字符串切片

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. using namespace std;
  5. string split(string a,int start,int stop,int step) {
  6. string newa;
  7. if (step > 0) {
  8. for (int i = start; i < stop; i = i + step) {
  9. newa += a[i];
  10. }
  11. }
  12. if (step < 0) {
  13. for (int i = start; i >stop; i = i + step) {
  14. newa += a[i];
  15. }
  16. }
  17. return newa;
  18. }
  19. int main() {
  20. string a;
  21. getline(cin, a);
  22. int start;
  23. int stop;
  24. int step;
  25. int T;
  26. int n;
  27. cin >> T;
  28. vector<string>count(T);
  29. for (int i = 0; i < T; i++) {
  30. cin >> n;
  31. if (n == 1) {
  32. cin >> start;
  33. stop = a.size();
  34. step = 1;
  35. }
  36. if (n == 2) {
  37. cin >> start >> stop;
  38. step=1;
  39. }
  40. if ((n == 3)) {
  41. cin >> start >> stop >> step;
  42. }
  43. if (start < 0) {
  44. start = a.size() + start;
  45. }
  46. if (stop < 0) {
  47. stop = a.size() + stop;
  48. }
  49. count[i] = split(a, start, stop, step);
  50. }
  51. for (int i = 0; i < T; i++) {
  52. cout << count[i]<<endl;
  53. }
  54. }

字符串切片的思路是定位到目的位置然后按照题目思路来即可

52.Kids A+B

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. using namespace std;
  5. void GetFigureToStorage(int a, int arr[], int b) {
  6. for (int i = b - 1; i >= 0; i--) {
  7. arr[i] = a % 10;
  8. a /= 10;
  9. }
  10. }//获取一个数字的各个位数,并存在数组里
  11. int translate(string a) {
  12. if (a == "zero")
  13. return 0;
  14. if (a == "one")
  15. return 1;
  16. if (a == "two")
  17. return 2;
  18. if (a == "three")
  19. return 3;
  20. if (a == "four")
  21. return 4;
  22. if (a == "five")
  23. return 5;
  24. if (a == "six")
  25. return 6;
  26. if (a == "seven")
  27. return 7;
  28. if (a == "eight")
  29. return 8;
  30. if (a == "nine")
  31. return 9;
  32. if (a == "ten")
  33. return 10;
  34. if (a == "eleven")
  35. return 11;
  36. if (a == "twelve")
  37. return 12;
  38. if (a == "thirteen")
  39. return 13;
  40. if (a == "fourteen")
  41. return 14;
  42. if (a == "fifteen")
  43. return 15;
  44. if (a == "sixteen")
  45. return 16;
  46. if (a == "seventeen")
  47. return 17;
  48. if (a == "eighteen")
  49. return 18;
  50. if (a == "nineteen")
  51. return 19;
  52. if (a == "twenty")
  53. return 20;
  54. if (a == "thirty")
  55. return 30;
  56. if (a == "forty")
  57. return 40;
  58. if (a == "fifty")
  59. return 50;
  60. if (a == "sixty")
  61. return 60;
  62. if (a == "seventy")
  63. return 70;
  64. if (a == "eighty")
  65. return 80;
  66. if (a == "ninety") {
  67. return 90;
  68. }
  69. }
  70. string translateback(int a) {
  71. if (a == 0) {
  72. return "zero";
  73. }
  74. if (a == 1) {
  75. return "one";
  76. }
  77. if (a == 2) {
  78. return "two";
  79. }
  80. if (a == 3) {
  81. return "three";
  82. }
  83. if (a == 4) {
  84. return "four";
  85. }
  86. if (a == 5) {
  87. return "five";
  88. }
  89. if (a == 6) {
  90. return "six";
  91. }
  92. if (a == 7) {
  93. return "seven";
  94. }
  95. if (a == 8) {
  96. return "eight";
  97. }
  98. if (a == 9) {
  99. return "nine";
  100. }
  101. if (a == 10) {
  102. return "ten";
  103. }
  104. if (a == 11) {
  105. return "eleven";
  106. }
  107. if (a == 12) {
  108. return "twelve";
  109. }
  110. if (a == 13) {
  111. return "thirteen";
  112. }
  113. if (a == 13) {
  114. return "thirteen";
  115. }
  116. if (a == 14) {
  117. return "fourteen";
  118. }
  119. if (a == 15) {
  120. return "fifteen";
  121. }
  122. if (a == 16) {
  123. return "sixteen";
  124. }
  125. if (a == 17) {
  126. return "seventeen";
  127. }
  128. if (a == 18) {
  129. return "eighteen";
  130. }
  131. if (a == 19) {
  132. return "nineteen";
  133. }
  134. if (a == 20) {
  135. return "twenty";
  136. }
  137. if (a == 30) {
  138. return "thirty";
  139. }
  140. if (a == 40) {
  141. return "forty";
  142. }
  143. if (a == 50) {
  144. return "fifty";
  145. }
  146. if (a == 60) {
  147. return "sixty";
  148. }
  149. if (a == 70) {
  150. return "seventy";
  151. }
  152. if (a == 80) {
  153. return "eighty";
  154. }
  155. if (a == 90) {
  156. return "ninety";
  157. }
  158. }
  159. int cut(string a) {
  160. string temp1=a, temp2=a;
  161. string m = "-";
  162. int cut1=0;
  163. int x, y;
  164. bool flag=false;
  165. for (int i = 0; i < a.size(); i++) {
  166. if (a[i]==m[0]) {
  167. cut1 = i;
  168. flag = true;
  169. }
  170. }
  171. if (flag) {
  172. for (int i = 0; i < cut1; i++) {
  173. temp2[i] = a[i];
  174. }
  175. temp1.erase(temp1.begin() + cut1, temp1.end());
  176. temp2.erase(temp2.begin(), temp2.begin() + cut1 + 1);
  177. x = translate(temp1);
  178. y = translate(temp2);
  179. return x + y;
  180. }
  181. else {
  182. return translate(a);
  183. }
  184. }
  185. int main(){
  186. string a,b;
  187. int m, n;
  188. cin >> a;
  189. cin >> b;
  190. m=cut(a);
  191. n = cut(b);
  192. int arr[2];
  193. GetFigureToStorage(m + n, arr, 2);
  194. if (m + n < 20) {
  195. cout << translateback(m + n);
  196. }
  197. if (m + n > 20) {
  198. cout << translateback(arr[0]*10) << "-" << translateback(arr[1]);
  199. }
  200. }

我的思路是按照英文翻译成数字然后再翻译回去做的

53.前后缀移除

  1. #include<string>
  2. #include<vector>
  3. #include<iostream>
  4. #include<cstdbool>
  5. using namespace std;
  6. string lstrip(string a,string b) {
  7. int count=0;
  8. vector<bool>sup(1000,false);
  9. for (int i = 0; i < a.size(); i++) {
  10. for (int j = 0; j < b.size(); j++) {
  11. if (a[i] == b[j]) {
  12. sup[i] = true;
  13. }
  14. }
  15. }
  16. for (int i = 0; i < a.size(); i++) {
  17. if (sup[i]==false) {
  18. count = i;
  19. break;
  20. }
  21. }
  22. a.erase(a.begin(),a.begin()+count );
  23. return a;
  24. }
  25. string rstrip(string a, string b) {
  26. int count = 0;
  27. vector<bool>sup(1000, false);
  28. for (int i = 0; i < a.size(); i++) {
  29. for (int j = 0; j < b.size(); j++) {
  30. if (a[i] == b[j]) {
  31. sup[i] = true;
  32. }
  33. }
  34. }
  35. for (int i = a.size()-1; i >=0; i--) {
  36. if (sup[i] == false) {
  37. count = i;
  38. break;
  39. }
  40. }
  41. a.erase(a.begin()+count+1, a.end());
  42. return a;
  43. }
  44. string strip(string a, string b) {
  45. a = lstrip(a, b);
  46. a = rstrip(a, b);
  47. return a;
  48. }
  49. int main() {
  50. string a;
  51. string b;
  52. string e, c, d;
  53. getline(cin,a);
  54. getline(cin, b);
  55. e = lstrip(a,b);
  56. c = rstrip(a, b);
  57. d = strip(a, b);
  58. cout << e << endl;
  59. cout << c << endl;
  60. cout << d << endl;
  61. }

这个题有点难读懂,我的理解是按照b字符串的每个字符进行遍历,找到第一个不属于这个字符串的数字,把前面的全部切割

54.字符串替换(WA)

  1. #include<stdio.h>
  2. #include<string>
  3. #include<vector>
  4. #include<iostream>
  5. using namespace std;
  6. int n;
  7. //字符串匹配
  8. vector<int> cmp(string a,string b) {
  9. int count=0;
  10. vector<int> sup(1000000,-1);
  11. for (int i = 0; i < a.size()-b.size()+1; i++) {
  12. for (int j = 0; j < b.size(); j++)
  13. {
  14. if (b[j] == a[i + j]) {
  15. count++;
  16. }
  17. }
  18. if (count == b.size()) {
  19. sup[n]=i;
  20. n++;
  21. }
  22. count = 0;
  23. }
  24. return sup;
  25. }
  26. int main() {
  27. string a;
  28. string b;
  29. string c;
  30. vector<int> sup(1000000, -1);
  31. getline(cin, a);
  32. getline(cin, b);
  33. getline(cin, c);
  34. sup=cmp(a, b);
  35. for (int i = 0; i < n; i++) {
  36. a.replace(sup[i]-i*(b.size()-c.size()), b.size(), c);
  37. }
  38. cout << a;
  39. }
  40. //

55.Atioi转换

  1. #include<string>
  2. #include<vector>
  3. #include<iostream>
  4. #include<queue>
  5. #include<cmath>
  6. using namespace std;
  7. int n = 0;
  8. queue<int> load(queue<int> q, int i, string a) {
  9. while ((int)a[i] >= 48 && (int)a[i] <= 57) {
  10. q.push((int)a[i] - 48);
  11. i++;
  12. n++;
  13. }
  14. return q;
  15. }
  16. int main() {
  17. string a;
  18. long long t = 0;
  19. queue<int> q;
  20. getline(cin, a);
  21. string b;
  22. b = "+-";
  23. string c;
  24. bool flag = true;
  25. c = " ";
  26. for (int i = 0; i < a.size(); i++) {
  27. if (a[i] == c[0]) {
  28. }
  29. else {
  30. if (a[i] == b[0]) {
  31. q = load(q, i + 1, a);
  32. break;
  33. }
  34. if (a[i] == b[1]) {
  35. q = load(q, i + 1, a);
  36. flag = false;
  37. break;
  38. }
  39. if ((int)a[i] > 48 && (int)a[i] < 57) {
  40. q = load(q, i, a);
  41. break;
  42. }
  43. if (((int)a[i] < 48 || (int)a[i] > 57)&&(a[i]!=b[0]&&a[i]!=b[1])) {
  44. break;
  45. }
  46. }
  47. }
  48. long long m = 2147483647;
  49. int mn = q.size();
  50. for (int i = mn - 1; !q.empty(); i--) {
  51. t = t + q.front() * pow(10, i);
  52. q.pop();
  53. }
  54. if (t == 0) {
  55. cout << "0";
  56. return 0;
  57. }
  58. if (t - m > 0) {
  59. if (!flag) {
  60. cout << "-" << m + 1;
  61. }
  62. else {
  63. cout << m;
  64. }
  65. }
  66. else
  67. if (!flag) {
  68. cout << "-" << t;
  69. }
  70. else {
  71. cout << t;
  72. }
  73. }

注意下界和上界不同,按照Ascii码慢慢来

56.删除前后缀

  1. #include<string>
  2. #include<vector>
  3. #include<iostream>
  4. #include<cstdbool>
  5. using namespace std;
  6. bool cmp(string a,string b) {
  7. int count = 0;
  8. if (a.size() > b.size()) {
  9. for (int i = 0; i < a.size() - b.size(); i++) {
  10. count = 0;
  11. for (int j = 0; j < b.size(); j++) {
  12. if (b[j] == a[i + j]) {
  13. count++;
  14. }
  15. }
  16. if (count == b.size()) {
  17. return true;
  18. }
  19. }
  20. }
  21. if (a.size() == b.size()) {
  22. count = 0;
  23. for (int j = 0; j < b.size(); j++) {
  24. if (b[j] == a[j]) {
  25. count++;
  26. }
  27. }
  28. if (count == b.size()) {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. string deletefront(string a,string b) {
  35. string temp=b;
  36. if (a.size() > b.size()) {
  37. for (int i = 0; i < b.size(); i++) {
  38. temp[i] = a[i];
  39. }
  40. if (cmp(temp, b)) {
  41. a.erase(a.begin(), a.begin() + b.size());
  42. return deletefront(a, b);
  43. }
  44. else {
  45. return a;
  46. }
  47. }
  48. return a;
  49. }
  50. string deleteback(string a, string b) {
  51. string temp = b;
  52. int su=0;
  53. if (a.size() > b.size()) {
  54. for (int i = a.size()-b.size(); i < a.size(); i++) {
  55. temp[su] = a[i];
  56. su++;
  57. }
  58. if (cmp(temp, b)) {
  59. a.erase(a.end()-b.size(), a.end());
  60. return deleteback(a, b);
  61. }
  62. else {
  63. return a;
  64. }
  65. }
  66. return a;
  67. }
  68. int main() {
  69. string a;
  70. string b;
  71. string a1;
  72. getline(cin, a);
  73. getline(cin, b);
  74. a1 = a;
  75. string c, d;
  76. c = deletefront(a, b);
  77. cout << c << endl;
  78. d = deleteback(a1, b);
  79. cout << d << endl;
  80. }

使用字符串匹配消除前后缀,递归

57.分离字符串

  1. #include<string>
  2. #include<vector>
  3. #include<iostream>
  4. using namespace std;
  5. int n = 0;
  6. vector<string> load(1000);
  7. vector<int> sup(1000000, -1);
  8. void cmp(string a, string b) {
  9. int count = 0;
  10. for (int i = 0; i < a.size() - b.size() + 1; i++) {
  11. for (int j = 0; j < b.size(); j++)
  12. {
  13. if (b[j] == a[i + j]) {
  14. count++;
  15. }
  16. }
  17. if (count == b.size()) {
  18. sup[n] = i;
  19. n++;
  20. }
  21. count = 0;
  22. }
  23. }
  24. string renewstring(string a, int start, int stop, int step) {
  25. string newa;
  26. if (step > 0) {
  27. for (int i = start; i < stop; i = i + step) {
  28. newa += a[i];
  29. }
  30. }
  31. return newa;
  32. }
  33. void split(string a, string b) {
  34. for (int i = 1; i < n; i++) {
  35. load[i] = renewstring(a, sup[i-1] + b.size(), sup[i], 1);
  36. }
  37. load[0] = renewstring(a, 0, sup[0], 1);
  38. load[n] = renewstring(a, sup[n-1]+b.size(), a.size(), 1);
  39. }
  40. int main() {
  41. string a, b;
  42. getline(cin, a);
  43. getline(cin, b);
  44. cmp(a, b);
  45. if (n != 0) {
  46. split(a, b);
  47. for (int i = 0; i < n; i++) {
  48. if (load[i] != "")
  49. cout << load[i] << endl;
  50. }
  51. if (load[n] != "") {
  52. cout << load[n];
  53. }
  54. }
  55. if (n == 0) {
  56. cout << a;
  57. }
  58. }

python,java都有现成的函数调用,自己实现split造轮子有点没必要了 

58.大小写交换

  1. #include<stdio.h>
  2. #include<string>
  3. #include<vector>
  4. #include<iostream>
  5. #include<cstdbool>
  6. using namespace std;
  7. char translate(char s) {
  8. s=(int)s;
  9. if (s >= 97) {
  10. return char(s-32);
  11. }
  12. else {
  13. return char(s+32);
  14. }
  15. }
  16. int main() {
  17. string a;
  18. getline(cin,a);
  19. for (int i = 0; i < a.size(); i++) {
  20. if((64<a[i]&&a[i]<91)||(96<a[i]&&a[i]<123))
  21. cout << translate(a[i]);
  22. else{
  23. cout<<a[i];
  24. }
  25. }
  26. }

修改对应的ascii码值

59.字符串后缀

  1. #include<stdio.h>
  2. #include<string>
  3. #include<vector>
  4. #include<iostream>
  5. #include<cstdbool>
  6. using namespace std;
  7. //字符串匹配
  8. bool cmp(string a,string b) {
  9. int count=0;
  10. int m=a.size();
  11. for (int j = b.size(); j >0; j--)
  12. {
  13. if (b[j] == a[m]) {
  14. count++;
  15. }
  16. m--;
  17. }
  18. if (count == b.size()) {
  19. return true;
  20. }
  21. if (count != b.size()) {
  22. return false;
  23. }
  24. }
  25. int main() {
  26. string a;
  27. string b;
  28. getline(cin, a);
  29. getline(cin, b);
  30. if (cmp(a, b)) {
  31. cout << "Yes";
  32. }
  33. else {
  34. cout << "No";
  35. }
  36. }

感觉不如删除前后缀

60.元宇宙

  1. #include<stdio.h>
  2. #include<string>
  3. #include<vector>
  4. #include<iostream>
  5. #include<cstdbool>
  6. #include<math.h>
  7. using namespace std;
  8. int translate(char s) {
  9. s=(int)s;
  10. if (s >= 65) {
  11. return s - 55;
  12. }
  13. else {
  14. return s-48;
  15. }
  16. }
  17. void rec(long long x, int m) {
  18. int tmp = 0;
  19. if (x == 0) {
  20. return;
  21. }
  22. else {
  23. tmp = x % m;
  24. x /= m;
  25. rec(x, m);
  26. if (m > 10 && tmp > 9) {
  27. printf("%c", (char)tmp + 55);
  28. return;
  29. }
  30. printf("%d", tmp);
  31. }
  32. }
  33. int main() {
  34. string a;
  35. string b;
  36. cin >> a;
  37. cin >> b;
  38. int m = a.size();
  39. int o = b.size();
  40. long long n1=0,n2=0;
  41. for (int i = 0; i <a.size(); i++) {
  42. n1 =n1+ translate(a[i]) * pow(36, m - 1);
  43. m--;
  44. }
  45. for (int i = 0; i < b.size(); i++) {
  46. n2 = n2 + translate(b[i]) * pow(36, o - 1);
  47. o--;
  48. }
  49. rec((n1+n2), 36);
  50. }

高配版kids A+B

61.pid控制

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<iostream>
  4. using namespace std;
  5. typedef struct PIDControl {
  6. double Kp, Ki, Kd;
  7. double preError, integral;
  8. }PIDdata;
  9. void InItPidContro(PIDdata *p) {
  10. p->integral = 0;
  11. p->Kd = 0;
  12. p->Ki = 0;
  13. p->Kp = 0;
  14. p->preError = 0;
  15. p->integral = 0;
  16. }
  17. double PIDCalculate(PIDdata* pid, double setpoint, double measuredValue) {
  18. double error,differental,sum;
  19. error = setpoint - measuredValue;
  20. pid->integral += error;
  21. differental = error - pid->preError;
  22. sum = pid->Kp * error + pid->Ki * pid->integral + pid->Kd * differental;
  23. pid->preError = error;
  24. return sum;
  25. }
  26. int main() {
  27. PIDdata* pid;
  28. int n;
  29. double setpoint, measeredValue;
  30. pid = (PIDdata*)malloc(sizeof(PIDControl));
  31. InItPidContro(pid);
  32. cin >> pid->Kp >> pid->Ki >> pid->Kd;
  33. cin >> setpoint >> measeredValue;
  34. cin >> n;
  35. for (int i =0 ; i < n; i++) {
  36. double sum=PIDCalculate(pid, setpoint, measeredValue);
  37. measeredValue += sum;
  38. printf("%d %.6lf", i+1, measeredValue);
  39. printf("\n");
  40. }
  41. }

62.加密字串

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<iostream>
  4. #include<string>
  5. #include<vector>
  6. #include<map>
  7. using namespace std;
  8. void translate() {
  9. }
  10. int main() {
  11. string a;
  12. getline(cin, a);
  13. map<char, int>maps;
  14. int x;
  15. cin >> x;
  16. x = x % 26;
  17. for (int i = 0; i < a.size(); i++) {
  18. maps[a[i]]++;
  19. }
  20. for (int i = 0; i < a.size(); i++) {
  21. if(maps[a[i]]%2)
  22. if (a[i] - x >= 'a') {
  23. cout << char(a[i] - x);
  24. }
  25. else {
  26. cout << char(a[i] + 26 - x);
  27. }
  28. else {
  29. if (a[i] + x <= 'z') {
  30. cout << char(a[i] + x);
  31. }
  32. else {
  33. cout << char(a[i] - 26 + x);
  34. }
  35. }
  36. }
  37. }

63.Arduino显示

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<iostream>
  4. #include<string>
  5. #include<vector>
  6. #include<map>
  7. using namespace std;
  8. int a[10] = { 6,2,5,5,4,5,6,3,7,6 };
  9. int oi(int i) {
  10. int count = 0;
  11. if (i == 0) count = 6;
  12. while (i) {
  13. count += a[i % 10];
  14. i /= 10;
  15. }
  16. return count;
  17. }
  18. int main() {
  19. int n, count = 0;
  20. cin >> n;
  21. n -= 4;
  22. for (int i = 0; i <= 999; i++) {
  23. for (int j = 0; j <= 999; j++) {
  24. int k = i + j;
  25. if (oi(i) + oi(j) + oi(k) == n) {
  26. count++;
  27. // cout<<i<<' '<<j<<' '<<k<<endl;
  28. // cout<<oi(i)<<' '<<oi(j)<<' '<<oi(k)<<'\n'<<endl;
  29. }
  30. }
  31. }
  32. cout << count;
  33. }

64.长安

  1. #include<stdio.h>
  2. int C(int n,int m)
  3. {
  4. int fz=1,fm=1,i;
  5. for(i=1;i<=n;i++)
  6. {
  7. fz*=i;fm*=m+n-i+1;
  8. }
  9. return fm/fz;
  10. }
  11. int main()
  12. {
  13. int i,x1,y1,x2,y2,m,sum[100];
  14. long long fz=1,fm=1;
  15. for(i=0;;i++)
  16. {
  17. scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
  18. m=x1*y1*x2*y2;
  19. if(m==0) break;
  20. if(x1>=x2&&y1>=y2)
  21. sum[i]=C(x1-1,y1-1)-(C(x2-1,y2-1)*C(x1-x2,y1-y2));
  22. else
  23. sum[i]=C(x1-1,y1-1);
  24. }
  25. for(int j=0;j<i;j++)
  26. printf("%d\n",sum[j]);
  27. return 0;
  28. }

65.三元搜索(没使用题干给的搜索法)

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdbool.h>
  4. int main(){
  5. int n;
  6. int m;
  7. int flag=1;
  8. scanf("%d",&n);
  9. int a[n];
  10. for(int i=0;i<n;i++){
  11. scanf("%d",&a[i]);
  12. }
  13. int b;
  14. scanf("%d",&b);
  15. for(int j=0;j<n;j++){
  16. if(a[j]==b){
  17. m=j;
  18. flag=0;
  19. break;
  20. }
  21. }
  22. if(flag==1){
  23. printf("%d in [-1]",b);
  24. return 0;
  25. }
  26. printf("%d in [%d]",b,m);
  27. return 0;
  28. }

66.时钟A-B

  1. #include <stdio.h>
  2. #include <time.h>
  3. int main() {
  4. struct tm start, end;
  5. time_t start_time, end_time, diff;
  6. scanf("%d %d %d",&start.tm_year,&start.tm_mon,&start.tm_mday);
  7. scanf("%d %d %d",&end.tm_year,&end.tm_mon,&end.tm_mday);
  8. start.tm_year -= 1900; // 年份,从1900年开始
  9. start.tm_mon -=1; // 月份,从0开始
  10. start.tm_hour = 0;
  11. start.tm_min = 0;
  12. start.tm_sec = 0;
  13. start.tm_isdst = -1; // 使用本地时间
  14. end.tm_year -= 1900; // 年份,从1900年开始
  15. end.tm_mon -=1; // 月份,从0开始
  16. end.tm_hour = 0;
  17. end.tm_min = 0;
  18. end.tm_sec = 0;
  19. end.tm_isdst = -1; // 使用本地时间
  20. start_time = mktime(&start);
  21. end_time = mktime(&end);
  22. diff =start_time-end_time;
  23. printf("%.6f",difftime(start_time,end_time));
  24. return 0;
  25. }

67.DNA螺旋

  1. #include<stdio.h>
  2. int main()
  3. {
  4. int n,m0,m1;
  5. scanf("%d",&n);
  6. for(m0=0;m0<n;m0++)
  7. {
  8. m1=m0%6;
  9. switch(m1)
  10. {
  11. case 0:{printf(" AT\n T--A\n A----T\nT------A\n");break;}
  12. case 1:{printf("T------A\n G----C\n T--A\n GC\n");break;}
  13. case 2:{printf(" CG\n C--G\n A----T\nA------T\n");break;}
  14. case 3:{printf("T------A\n A----T\n A--T\n GC\n");break;}
  15. case 4:{printf(" AT\n C--G\n T----A\nC------G\n");break;}
  16. case 5:{printf("C------G\n T----A\n G--C\n AT\n");break;}
  17. }
  18. }
  19. return 0;
  20. }

68.有效表达式

  1. #include <stdio.h>
  2. unsigned long int catalan(unsigned int n) {
  3. if (n <= 1) return 1;
  4. unsigned long int c = 0;
  5. for (int i=0; i<n; i++)
  6. c += catalan(i)*catalan(n-i-1);
  7. return c;
  8. }
  9. int main() {
  10. int n;
  11. scanf("%d",&n);
  12. printf("%d",catalan(n));
  13. return 0;
  14. }

69.循环排序(我使用的快排)

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void swap(int* a, int* b) {
  4. int temp;
  5. temp = *a;
  6. *a = *b;
  7. *b = temp;
  8. }
  9. int partition(int arr[], int high, int low) {
  10. int pivot = arr[high];//设置中心轴
  11. int i = low - 1;
  12. for (int j = low; j <= high - 1; j++) {
  13. if (arr[j] < pivot) {
  14. i++;
  15. swap(&arr[i], &arr[j]);
  16. }
  17. }
  18. swap(&arr[i + 1], &arr[high]);
  19. return i + 1;
  20. }
  21. void quickSort(int arr[], int low, int high) {
  22. if (low < high) {
  23. int pi = partition(arr, high, low);
  24. quickSort(arr, low, pi - 1);
  25. quickSort(arr, pi + 1, high);
  26. }
  27. }
  28. int main() {
  29. int a;
  30. scanf("%d", &a);
  31. int* b;
  32. b = (int*)malloc(a * sizeof(int));
  33. for (int i = 0; i < a; i++) {
  34. scanf("%d", &b[i]);
  35. }
  36. quickSort(b,0,a-1);
  37. for (int i = 0; i < a-1; i++) {
  38. printf("%d ",b[i]);
  39. }
  40. printf("%d",b[a-1]);
  41. }

70好麻烦暂时也没时间写

//后面的又长又臭实在不想写,找了个答案上传了主页里应该可以买免费下载

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

闽ICP备14008679号