zznu-oj-2117 : 我已经在路上了(求函数的原函数的字符串)--【暴力模拟题,花式模拟题,String大法好】

2117 : 我已经在路上了

时间限制:1 Sec 内存限制:256 MiB
提交:39 答案正确:8

提交 状态 编辑 讨论区

题目描述

spring是不折不扣的学霸,那可是机房考研中的头号选手,不吹不黑,spring出征,寸草不生

但是这个学霸从来不屑于写简单的东西,因为时间是可贵的,spring喜欢留给B站小姐姐。所以在计算数学求导的时候spring就想出来用编程来完成,这样岂不是美滋滋,正好符合spring高大上的气质

那么问题来了,基础的求导公式那么多,spring只是添加了少许几个原函数,分别为y=C,y=x,y=x^n,y=sinx,y=cosx,y=lnx,y=loga(x),y=e^x,,每次对一个原函数求导,但是学霸spring又觉得这样太简单,所以就决定可以在求导函数中加上常数(正整数,int范围),当然为了平衡难度,常数只能够在x前面,或者函数最前面这两个地方添加(如果两者为同一地点只能添加一次)。

输入

输入形式严格按照上面的函数形式,并且符合常规数学知识,其中C,n,a都是正整数的形式给出,均在int范围。

输出

输出按照样例实现,(拒绝杠精,不接受反驳)不用带有y'=,直接写出求导的结果,占一行。

样例输入

复制
y=sin5x
y=e^2x
y=2log13(8x)

样例输出

复制
5cos5x
2e^2x
2/ln13/x

注意:

      1、严格分类讨论,共八种情况;可以使用string的find()函数来找到每种情况的特殊字符段,该函数自行百度参数及使用方法。

      2、每种情况,注意拆分出常数项和x的系数项,用一个函数来实现这个反复的问题。

      3、每种情况,每个细节都要考虑到,常数项为“1”是否删去,x的系数项为“1”是否删除,理清思路后也就16种情况。具体自己列下来,整理清楚。

      4、特殊情况,y=x求导结果为1,y=1x^1等等18中特例!自行耐心构造!

      5、不全面的样例:

 1 y=sinx
 2 y=sin200000x
 3 y=100000sin200000x
 4 y=2sinx
 5 
 6 y=cosx  y=2000000cosx  y=cos200000x
 7 y=200000cos200000x
 8 
 9 y=lnx  y=ln100000x  y=1000000lnx    y=100000ln100000x
10 
11 y=2log13(8x)  y=200000log130000(10000x)
12 //特例需要约分,还需要注意系数a和n为1的地方
13 y=6ln6x  y=ln6x
14 y=100000e^20000x  y=10000000x 
15  
16 y=1x   y=x^2  y=1x^2  y=e^x  y=10e^10x   y=1x^1
17 y=123456789x^100000


建议AC后再看看题解:

    

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<string>
  5 #include<algorithm>
  6 #define ll long long
  7 using namespace std;
  8 #define N 100
  9 ll get(char s[],ll beg){  //从下标beg的地方开始累乘出一个数值,不越界,找到不是数字的地方停下来
 10     ll x=0;
 11     for(ll i=beg;i<(ll)strlen(s)&&s[i]>='0'&&s[i]<='9';i++){
 12         x=x*10+s[i]-'0';
 13     }
 14     return x;
 15 }
 16 int main(){
 17     char s[100];
 18 
 19     while(scanf("%*2s%s",s)!=EOF){   //在读取的时候跳过前两个字符(%*2c也阔以)
 20         string st(s);
 21         ll c,a,n,b;
 22 
 23         if(st.find("x^")!=-1){ //cx^n求导-->(c*n)x^(n-1)
 24            if(s[0]>='0'&&s[0]<='9')
 25                c=get(s,0);
 26            else
 27                c=1;
 28            ll x=st.find("^");
 29            n=get(s,x+1);
 30 
 31            if(c*n!=1)printf("%lld",c*n);
 32            cout<<"x";
 33           if(n-1==1)printf("
");
 34           else printf("^%lld
",n-1);
 35         }
 36 
 37         else if(st.find("sin")!=-1){  //Csin(ax)求导
 38             if(st[0]>='0'&&st[0]<='9')
 39                 c=get(s,0);
 40             else
 41                 c=1;
 42            ll x=st.find("n");
 43            if(st[x+1]=='x')
 44                 a=1;
 45            else
 46                 a=get(s,x+1);
 47             if(c*a!=1)printf("%lld",c*a);
 48             if(a==1)
 49                 printf("%s%s
","cos","x");
 50             else
 51                 printf("%s%lld%s
","cos",a,"x");
 52         }
 53         else if(st.find("cos")!=-1){   //Ccos(ax)求导
 54             if(st[0]>='0'&&st[0]<='9')
 55                 c=get(s,0);
 56             else
 57                 c=1;
 58            ll x=st.find("s");
 59            if(st[x+1]=='x')
 60                 a=1;
 61            else
 62                 a=get(s,x+1);
 63 
 64             if(c*a==1)
 65                 printf("-");
 66             else
 67                 printf("-%lld",c*a);
 68             if(a!=1)
 69                 printf("%s%lld%s
","sin",a,"x");
 70             else
 71                 printf("%s
","sinx");
 72 
 73         }
 74         else if(st.find("ln")!=-1){  //cln(ax)求导 ==c/x
 75              if(st[0]>='0'&&st[0]<='9')
 76                 c=get(s,0);
 77             else
 78                 c=1;
 79            ll x=st.find("n");
 80            if(st[x+1]=='x')
 81                 a=1;
 82            else
 83                 a=get(s,x+1);
 84            printf("%lld/",c);
 85 
 86             printf("x
");
 87         }
 88         else if(st.find("log")!=-1){  //nloga(bx)求导-->n/lna/x
 89             if(st[0]>='0'&&st[0]<='9')
 90                 n=get(s,0);
 91             else
 92                 n=1;
 93              ll x=st.find("g");
 94             a=get(s,x+1);
 95              x=st.find("(");
 96             if(st[x+1]=='x')
 97                 b=1;
 98             else
 99                 b=get(s,x+1);
100 
101             printf("%lld/ln%lld/x
",n,a);
102         }
103         else if(st.find("e^")!=-1){   //y=ne^ax求导
104             if(st[0]>='0'&&st[0]<='9')
105                 n=get(s,0);
106             else
107                 n=1;
108                ll x=st.find("^");
109                if(st[x+1]=='x')a=1;
110                else
111                 a=get(s,x+1);
112 
113                if(a*n==1)
114                 printf("%s
",s);
115                else{
116                  printf("%llde^",a*n);
117                  if(a==1)
118                     printf("x
");
119                  else
120                     printf("%lldx
",a);
121                }
122         }
123         else if(st.find("x")!=-1){   //y=ax求导
124            if(st[0]=='x')
125             printf("1
");
126            else
127             printf("%lld
",get(s,0));
128         }
129         else
130             printf("0
");
131     }
132 
133     return 0;
134 }
View Code(有注释,不懂留言即可)
原文地址:https://www.cnblogs.com/zhazhaacmer/p/9364356.html