原题链接;https://acm.hdu.edu.cn/showproblem.php?pid=6985
This summer, ZXyang became so tired when doing the problems of Multi-University contests. So he decided to attend the Unified National Graduate Entrance Examination. This day, he sees a problem of series.
Let S(x) be a function with x as the independent variable. S(x) can be represented by the formula as follow.
f(x)=∑i=1nfi(x)
S(x)=∑j=1xf(j)
fi(x) is a function with x as the independent variable. Furthermore. fi(x) belongs to the function set F.
F={C,Cx,Csinx,Ccosx,Csinx,Ccosx,Cx,Cx}
C is a constant integer ranging from 0 to 109.
ZXyang wonders if S(x) is convergent. S(x) is convergent if and only if limx→∞S(x)=c, where c is a constant.
Input
The first line of input contains a single integer t (1≤t≤104) --- the number of test cases.
The first and the only line of each test case contains a single string s (1≤|s|≤100), indicating the formula of f(x). Fraction is presented as a/b. Cx is presented as C^x. It's guaranteed that the constant C won't be left out when C=1. f(x) consists of functions from F connected with +.
The first and the only line of each test case contains a single string s (1≤|s|≤100), indicating the formula of f(x). Fraction is presented as a/b. Cx is presented as C^x. It's guaranteed that the constant C won't be left out when C=1. f(x) consists of functions from F connected with +.
Output
For each test case, print YES in one line if S(x) is a convergent sequence, or print NO in one line if not.
Sample Input
2 1sinx+0cosx+3x+6/sinx 0
Sample Output
NO
YES
思路
:题意为:给出一个序列S,判断它是否收敛。因为对于S里的每一个函数,如sinx,
表示sin1+sin2+``````+sinx是否收敛。是看它的级数是否收敛。由于给出的函数的级数均是发散的,所以只需要判断它们的系数是否全部为0,常数项也要为0.
代码如下:
#include <bits/stdc++.h>
using namespace std;
int t;
bool check(int c){//判断每个系数是否为0
if(c==0) return true;
return false;
}
int main(){
cin>>t;
while(t--){
string s;
cin>>s;
int c=0;
bool flag=true;
for(int i=0;i<s.size();i++){
if(s[i]>='0'&&s[i]<='9'){
c=c*10+(s[i]-'0');//累积整数
}
else {
if(!check(c)){//如果碰到除了数字之外的字符,表明c就是系数了
puts("NO");
c=0;
flag=false;
break;
}
else if(check(c)){
c=0;
}
}
if(i==s.size()-1){//!!!最后一个就是常数函数,特判!
if(!check(c)){
puts("NO");
c=0;
flag=false;
break;
}
}
}
if(flag) puts("YES");
}
return 0;
}