ZZNUOJ 我已经在路上了(函数求导模拟)

2117 : 我已经在路上了

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

提交 状态 讨论区

题目描述

 

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

一道大大的模拟题 思路可以说是很清晰了

来自:https://blog.csdn.net/dadaguai001/article/details/81195224

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
#define ll long long
string str;
int i,size,flag,indexx;
ll s,ss,t;
void x()
{
	if(s==0) cout<<1<<endl;
	else cout<<s<<endl;
}
void xx()
{
	indexx=i+2;
	for(int j=indexx;j<size;j++)
	{
		ss=ss*10+str[j]-'0';
	}
	if(s==0)//前面无常数 
	{
		if(ss==1) cout<<1<<endl;//出现 x^1
		else {
			cout<<ss<<"x^"<<ss-1<<endl;
		} 
	}else {
		if(ss==1) cout<<s<<endl;//前面有常数 后面是 x^1 
		else cout<<s*ss<<"x^"<<ss-1<<endl;
	}
}
void sin()
{
	indexx=i+3;
	if(str[indexx]=='x'||str[indexx]=='1'&&str[indexx+1]=='x')//sin1x 和 sinx 
	{
		if(s==0||s==1) cout<<"cosx"<<endl;
		else cout<<s<<"cosx"<<endl; 
	}else {
		while(str[indexx]!='x')
		{
			ss=ss*10+str[indexx]-'0';
			indexx++;
		}if(s==0) cout<<ss<<"cos"<<ss<<"x"<<endl;
		else cout<<ss*s<<"cos"<<ss<<"x"<<endl; 
	}
 }
void cos()
{
	cout<<"-";
	indexx=i+3;
	if(str[indexx]=='x'||(str[indexx]=='1'&&str[indexx+1]=='x'))
	{//cosx cos1x 
		if(s==0||s==1) cout<<"sinx"<<endl;
		else cout<<s<<"sinx"<<endl; 
	}else 
	{
		while(str[indexx]!='x')
		{
			ss=ss*10+str[indexx]-'0';
			indexx++;
		}
		if(s==0) cout<<ss<<"sin"<<ss<<"x"<<endl;
		else cout<<ss*s<<"sin"<<ss<<"x"<<endl;
	}
 } 
 void log()
 {
 	indexx=i+3;
 	while(str[indexx]!='(')//y=loga(x)
 	{
 		ss=ss*10+str[indexx]-'0';
 		indexx++;
	 }
	 if(s==0||s==1) cout<<1<<"/";
	 else cout<<s<<"/";
	 cout<<"ln"<<ss<<"/x"<<endl;//不管有没有前缀数字,后面都是1/x 
 }
void ex()
{
	indexx=i+2;
	if(str[indexx]=='x'||(str[indexx]=='1'&&str[indexx+1]=='x'))
	{
		if(s==1||s==0) cout<<"e^x"<<endl;
		else cout<<s<<"e^x"<<endl;
	}else 
	{
		while(str[indexx]!='x')
		{
			ss=ss*10+str[indexx]-'0';
			indexx++;
		}
		if(s==0||s==1) cout<<ss<<"e^"<<ss<<"x"<<endl; 
		else cout<<ss*s<<"e^"<<ss<<"x"<<endl;
	}
}
void ln()
{
	if(s==0||s==1) cout<<"1/x"<<endl;
	else cout<<s<<"/x"<<endl;
}
int main()
{
	while(cin>>str)
	{
		ss=s=0;
		i=2;//前面的 y = 直接跳过 
		flag=1;
		size=str.length();
		if(str[i]>='0'&&str[i]<='9')//获取前缀数字
		{
			while(str[i]>='0'&&str[i]<='9'&&size!=i)
			{
				s=s*10+str[i]-'0';
				i++;
			}
			if(i==size)//如果是常数 则会跑到结尾 
			{
				cout<<"0"<<endl;
				flag=0;
			}
		 }
		if(flag)
		{
			if(str[i]=='x'&&i==size-1) x();
			else if(str[i]=='x'&&str[i+1]=='^') xx();
			else if(str[i]=='s') sin();
			else if(str[i]=='c') cos();
			else if(str[i]=='l'&&str[i+1]=='o') log();
			else if(str[i]=='l') ln();
			else ex(); 
		 } 
	}return 0;
 } 

猜你喜欢

转载自blog.csdn.net/qq_40046426/article/details/81257921
今日推荐