3 条题解
-
2
题解
这道题的解法需要我们结合我们解方程的方法:即首先把未知数移动到等式左侧,常数移动到等式右侧,最后除以下。 时间仓促,可能不太全面,凑合看看,欢迎Copy
#include <bits/stdc++.h> bool flag, havenum; char s[100010], ansx; int son, mother, pre, lor = 1, equal, len; int main() { scanf("%s", s + 1); // 由于这里用的是字符数组 len = strlen(s + 1); s[0] = '+'; s[len + 1] = '+'; len++; for (int i = 1; i <= len; i++) { if (s[i] >= 'a' && s[i] <= 'z') ansx = s[i]; // 记录未知数名 if (s[i] == '=' || s[i] == '-' || s[i] == '+') { havenum = 0; if (i == 1 || i - 1 == equal) { pre = i; continue; } int x = 0; if (isdigit(s[i - 1])) flag = 0; else flag = 1; for (int j = pre + 1; j <= i - flag - 1; j++) { x = (x << 1) + (x << 3) + (s[j] ^ '0'); // 这里其实是 x * 10 + (s[j] - '0') havenum = 1; } if (havenum == 0) x = 1; if (flag == 0) son += x * lor * -1 * (s[pre] - 44) * -1; // 常数 else mother += x * lor * (s[pre]- 44) * -1; // 未知数的系数 pre = i; if (s[i] == '=') lor = -1, s[i] = '+', equal = i; // 过等号要变号,lor = -1 } } double ansy = double(son) / double(mother); // 除出未知数 if (fabs(ansy) - 0.0 < 0.000001) // 误差归0 ansy = 0.0; printf("%c=%.3lf", ansx, ansy); // .3f输出 return 0; }
#include <bits/stdc++.h> bool flag, havenum; char s[100010], ansx; int son, mother, pre, lor = 1, equal, len; int main() { scanf("%s", s + 1); len = strlen(s + 1); s[0] = '+'; s[len + 1] = '+'; len++; for (int i = 1; i <= len; i++) { if (s[i] >= 'a' && s[i] <= 'z') ansx = s[i]; if (s[i] == '=' || s[i] == '-' || s[i] == '+') { havenum = 0; if (i == 1 || i - 1 == equal) { pre = i; continue; } int x = 0; if (isdigit(s[i - 1])) flag = 0; else flag = 1; for (int j = pre + 1; j <= i - flag - 1; j++) { x = (x << 1) + (x << 3) + (s[j] ^ '0'); havenum = 1; } if (havenum == 0) x = 1; if (flag == 0) son += x * lor * -1 * (s[pre] - 44) * -1; else mother += x * lor * (s[pre]- 44) * -1; pre = i; if (s[i] == '=') lor = -1, s[i] = '+', equal = i; } } double ansy = double(son) / double(mother); if (fabs(ansy) - 0.0 < 0.000001) ansy = 0.0; printf("%c=%.3lf", ansx, ansy); return 0; }
-
0
个位大佬帮忙看看,思路就是模拟,咋改改
#include <bits/stdc++.h> using namespace std; char s[100010]; char x; int a; double ans; double sum; double sum1; int main(){ cin>>s; int len=strlen(s); for (int i=0;i<len;i++){ if (s[i]>='a'&&s[i]<='z'){ x=s[i]; break; } if (s[i]=='='){ a=i; } } for (int i=0;i<len;i++){ if (s[i]==x){ ans=0; for (int j=i-1;j>=0;j--){ if (s[j]>='0'&&s[j]<='9'){ double u=s[j]-'0'; ans=ans*10+u; if (j==0){ if (s[j]>='0'&&s[j]<='9'){ sum1+=ans; } } } else{ if (s[j]=='+'){ if (j<a){ sum1+=ans; } else{ sum1-=ans; } } if (s[j]=='-'){ if (j<a){ sum1-=ans; } else{ sum1+=ans; } } break; } } } else if (s[i]>='0'&&s[i]<='9'){ double n=0; for (int j=i-1;j>=0;j--){ if (s[j]=='-'){ if (j<a){ sum+=n; break; } else{ sum-=n; break; } } else if (s[j]=='+'){ if (j<a){ sum-=n; break; } else{ sum+=n; break; } } else if (s[j]=='='){ sum+=n; break; } else{ double o=s[j]-'0'; n=n*10+o; } } } } cout<<x<<"="; if (sum==0){ cout<<"0.000"; } else{ printf("%.3lf\n",sum*1.0/sum1); } return 0; }
-
0
核心思路:将未知数左移,常数右移,最后相除
#include <cstdio> int val1 , val2 , n ; //val1存储常数,val2存储未知数系数 bool left = true , tf = true; //left是否在左 tf是正负数 //对于常数左减右加,未知数相反 char chr , chx; //chr存储输入的字符,chx存储未知数字符 int main(){ chr = getchar() ; while( ('0' <= chr && chr <= '9') || ('a' <= chr && chr <= 'z') || chr == '+' || chr == '-' || chr == '=' ){ //string a ; //cin >> a ; //for(int i = 0 ; i < a.length ; i ++) //也可以 if(chr == '=') val1 -= n * (tf ? 1 : -1) , n = 0 , left = false , tf = 1; //判定'=' 右边(val1)增加n,清空n,接下来就是右边 //默认符号正号,tf = 1 //这里一定左边,所以不用判断左右 else if('0' <= chr && chr <= '9') n = (n << 3) + (n << 1) + chr - '0' ; //正常的数,这里与正负号无关 //只需要+ 10 + 新个位数 else if('a' <= chr && chr <= 'z') val2 += (n == 0 ? 1 : n) * (left ? 1 : -1) * (tf ? 1 : -1), n = 0 , chx = chr; //未知数 //默认为1而不是0 //左加右减,判断正负 //清空n , 未知数变量修改 //这时清空了n,若接下运算符或空 //加减0等于寂寞 else if(chr == '+') val1 += n * (left ? -1 : 1) * (tf ? 1 : -1) , tf = 1 , n = 0 ; //常数 //左减右加,判断正负 //下一个是正数,清空n else if(chr == '-') val1 += n * (left ? -1 : 1) * (tf ? 1 : -1) , tf = 0 , n = 0 ; //常数 //左减右加,判断正负 //下一个是负数,清空n chr = getchar() ; } val1 += n * (left ? -1 : 1) * (tf ? 1 : -1) ; //结尾剩余的加上 if(val1 == 0) printf("%c=0.000" , chx) ; //特判0 防止出现-0.000 else printf("%c=%.3lf" , chx , (1.0 * val1) / val2) ; //正常输出 return 0 ; }
- 1
信息
- ID
- 1751
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 138
- 已通过
- 50
- 上传者