120 条题解
-
4账号已注销 LV 7 @ 2017-10-14 11:03:04
格式不好重发*2
/* 特别考验细节的模拟。 坑1:未知数系数为1或-1。 坑2:最后一个常数项漏存。 坑3:-0.000。 */ #include<cstdio> #include<cstring> int main() { char c,s[505]; int x=0,y=0,a=0,f=1,f1=1,i; //x为未知数系数(=左),y为常数项(=右),a为正在读入的常数值,f为a的符号,f1为a是否在等号左边。 double ans; scanf("%s",s); for (i=0;i<strlen(s);i++) { if (s[i]>='0'&&s[i]<='9') a=a*10+s[i]-48; if (s[i]=='-') { y+=-1*f*f1*a; //常数项保存,如果前面是未知数此时a为0。 f=-1; a=0; } if (s[i]=='+') { y+=-1*f*f1*a; //同上。 f=1; a=0; } if (s[i]=='=') { y+=-1*f*f1*a; //同上。 f1=-1; f=1; a=0; } if (s[i]>='a'&&s[i]<='z') { c=s[i]; if (a) x+=f*a*f1; else x+=f*f1; //未知数系数保存。 f=1; a=0; } } y+=-1*f*f1*a; //最后一个常数项。 ans=1.0*y/x; //计算结果。 if (ans<=0.000&&ans>-0.0005) printf("%c=0.000",c); //最后一个点会卡掉a=-0.000,注意(-0.0005,0.000]的区间。 else printf("%c=%.3lf",c,ans); return 0; }
-
12024-10-03 15:43:03@
""" https://vijos.org/p/1344 算比较难的题目了, 当成basic calculator去做, 字符串处理建议使用Python写 维护三个变量: parameter, total, variable 处理过程分为两部分: =左边和=右边. =左边按照正常的运算符号去处理, =右边的全部取反, 作为移项操作 然后以左边方程为例, 讨论输入的字符: 1. 首先如果输入的字符是"+"和"-", 直接加入到stack中. 2. 然后输入的字符是0-9的数字, 首先检查stack[-1]是否是数字, 如果是, 需要num*10+digit然后再插入栈顶 插入完之后检查下一位, 如果下一位越界或者不再是数字, 说明当前数字处理完了. 把它拿出来检查stack[-1]是否是"+"和"-". 如果是的, 把符号拿出来并更新当前数字的正负号. 3. 然后处理字母. 字母是未知数, 分三种情况讨论. 第一种情况是stack为空, 这时候说明字母在方程的第一位, 系数是1, 累加到parameter 第二种情况是stack[-1]是"+"或"-", 把栈顶符号取出, 系数分别是1和-1, 累加到parameter 第三种情况是stack[-1]是数字, 数字本身就是方程的系数了, 取出累加到parameter 所有情况都讨论完之后, 把当前字符更新给variable表示方程字符 全部处理完之后, stack里面全是数字, 把他们一个个累加到total, 作为方程的常数 最后-(total/parameter)就是答案, 注意保留三位小数和格式输出 """ if __name__ == "__main__": s = input() div = s.index("=") total = parameter = 0 variable = "" stack = [] for i in range(div): ch = s[i] if ch == " ": continue elif ch == "+" or ch == "-": stack.append(ch) elif ch.isnumeric(): digit = int(ch) # append the number if stack and isinstance(stack[-1], int): num = stack.pop() * 10 + digit stack.append(num) else: stack.append(digit) # check if the number has ended if i+1 >= div or not s[i+1].isnumeric(): num = stack.pop() if len(stack) == 0: stack.append(num) elif isinstance(stack[-1], str): sign = stack.pop() if sign == "+": stack.append(num) else: stack.append(-num) else: variable = ch if len(stack) == 0: parameter += 1 elif isinstance(stack[-1], str): sign = stack.pop() if sign == "+": parameter += 1 else: parameter -= 1 else: parameter += stack.pop() while stack: total += stack.pop() for i in range(div+1,len(s)): ch = s[i] if ch == " ": continue elif ch == "+" or ch == "-": stack.append(ch) elif ch.isnumeric(): digit = int(ch) # append the number if stack and isinstance(stack[-1], int): num = stack.pop() * 10 + digit stack.append(num) else: stack.append(digit) # check if the number has ended if i+1 >= len(s) or not s[i+1].isnumeric(): num = stack.pop() if len(stack) == 0: stack.append(num) elif isinstance(stack[-1], str): sign = stack.pop() if sign == "+": stack.append(num) else: stack.append(-num) else: variable = ch if len(stack) == 0: parameter -= 1 elif isinstance(stack[-1], str): sign = stack.pop() if sign == "+": parameter -= 1 else: parameter += 1 else: parameter -= stack.pop() while stack: total -= stack.pop() res = - (total / parameter) print('%s=%.3f' % (variable, res))
-
12021-08-29 17:03:10@
#include<bits/stdc++.h> using namespace std; char x; long long coe,value,opp=1,cur=0,sign=1; double ans; bool value_gotten=false; int main(){ char c=getchar(); while(true){ if(c>='a' && c<='z'){ x=c; if(cur==0 && !value_gotten){ coe+=opp*sign; value_gotten=false; } else{ coe+=opp*sign*cur; cur=0; sign=1; value_gotten=false; } } else if(c=='-'){ value+=-opp*sign*cur; cur=0; sign=-1; value_gotten=false; } else if(c=='+'){ value+=-opp*sign*cur; cur=0; sign=1; value_gotten=false; } else if(c>='0' && c<='9'){ cur=cur*10+c-'0'; value_gotten=true; } else if(c=='='){ value+=-opp*sign*cur; cur=0; sign=1; opp=-opp; value_gotten=false; } else{ value+=-opp*sign*cur; break; } c=getchar(); } ans=double(value)/coe; printf("%c=%.3lf",x,ans==0?abs(ans):ans); return 0; }
-
12017-12-07 21:35:37@
var
p,k,i,gs,wzs,sz:longint;
s:string;
fh,wz:char;
float:real;
begin
readln(s);
k:=pos('=',s);
fh:='+';
for i:=1 to k-1 do
begin
if s[i] in ['a'..'z'] then
begin
if gs=0 then
gs:=1;
if fh='+' then
inc(wzs,gs) else
dec(wzs,gs);
wz:=s[i];
gs:=0;
end;
if s[i] in ['0'..'9'] then
begin
val(s[i],p);
gs:=gs*10+p;
end;
if s[i]='+' then
begin
if fh='+' then
inc(sz,gs) else
dec(sz,gs);
gs:=0;
fh:='+';
end;
if s[i]='-' then
begin
if fh='+' then
inc(sz,gs) else
dec(sz,gs);
gs:=0;
fh:='-';
end;
end;
if fh='+' then
inc(sz,gs) else
dec(sz,gs);
gs:=0;
sz:=(-1)*sz;
fh:='+';
for i:=k+1 to length(s) do
begin
if s[i] in ['a'..'z'] then
begin
if gs=0 then
gs:=1;
if fh='-' then
inc(wzs,gs) else
dec(wzs,gs);
wz:=s[i];
gs:=0;
end;
if s[i] in ['0'..'9'] then
begin
val(s[i],p);
gs:=gs*10+p;
end;
if s[i]='+' then
begin
if fh='+' then
inc(sz,gs) else
dec(sz,gs);
gs:=0;
fh:='+';
end;
if s[i]='-' then
begin
if fh='+' then
inc(sz,gs) else
dec(sz,gs);
gs:=0;
fh:='-';
end;
end;
if fh='+' then
inc(sz,gs) else
dec(sz,gs);
float:=sz/wzs;
if float=0.00000 then
writeln(wz,'=0.000') else
writeln(wz,'=',sz/wzs:0:3);
end. -
12017-09-05 01:44:30@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
string a;
int main()
{
cin>>a;
double x1=0,x2=0,y1=0,y2=0;
int k=a.find('=',0),lena=a.size()-1,yx=1;
int x=0,flag=1,d;
for(d=0;d<k;++d)
{
if(a[d]>='0'&&a[d]<='9')x=x*10+a[d]-'0';
if(a[d]!='+'&&a[d]!='-'&&(a[d]>'9'||a[d]<'0'))
{
if(flag){cout<<a[d]<<"=";flag=0;}
if(x==0)y1+=2*yx-1;
else y1+=(2*yx-1)*x;
x=0;
}
if(a[d]=='-'&&x!=0)x1+=(2*yx-1)*x,yx=0,x=0;
if(a[d]=='+'&&x!=0)x1+=(2*yx-1)*x,yx=1,x=0;
if(a[d]=='-')yx=0;
if(a[d]=='+')yx=1;
}
if(a[d]=='='&&x!=0)x1+=(2*yx-1)*x;
x=0;yx=1;
for(d=k+1;d<=lena;++d)
{
if(a[d]>='0'&&a[d]<='9')x=x*10+a[d]-'0';
if(a[d]!='+'&&a[d]!='-'&&(a[d]>'9'||a[d]<'0'))
{
if(flag)cout<<a[d]<<"=",flag=0;
if(x==0)y2+=2*yx-1;
else y2+=(2*yx-1)*x;
x=0;
}
if(a[d]=='-'&&x!=0)x2+=(2*yx-1)*x,yx=0,x=0;
if(a[d]=='+'&&x!=0)x2+=(2*yx-1)*x,yx=1,x=0;
if(a[d]=='-')yx=0;
if(a[d]=='+')yx=1;
}
if(d==lena+1&&x!=0)x2+=(2*yx-1)*x;
printf("%.3lf",(x1-x2)/(y2-y1));
return 0;
} -
12017-09-05 01:44:18@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
string a;
int main()
{
cin>>a;
double x1=0,x2=0,y1=0,y2=0;
int k=a.find('=',0),lena=a.size()-1,yx=1;
int x=0,flag=1,d;
for(d=0;d<k;++d)
{
if(a[d]>='0'&&a[d]<='9')x=x*10+a[d]-'0';
if(a[d]!='+'&&a[d]!='-'&&(a[d]>'9'||a[d]<'0'))
{
if(flag){cout<<a[d]<<"=";flag=0;}
if(x==0)y1+=2*yx-1;
else y1+=(2*yx-1)*x;
x=0;
}
if(a[d]=='-'&&x!=0)x1+=(2*yx-1)*x,yx=0,x=0;
if(a[d]=='+'&&x!=0)x1+=(2*yx-1)*x,yx=1,x=0;
if(a[d]=='-')yx=0;
if(a[d]=='+')yx=1;
}
if(a[d]=='='&&x!=0)x1+=(2*yx-1)*x;
x=0;yx=1;
for(d=k+1;d<=lena;++d)
{
if(a[d]>='0'&&a[d]<='9')x=x*10+a[d]-'0';
if(a[d]!='+'&&a[d]!='-'&&(a[d]>'9'||a[d]<'0'))
{
if(flag)cout<<a[d]<<"=",flag=0;
if(x==0)y2+=2*yx-1;
else y2+=(2*yx-1)*x;
x=0;
}
if(a[d]=='-'&&x!=0)x2+=(2*yx-1)*x,yx=0,x=0;
if(a[d]=='+'&&x!=0)x2+=(2*yx-1)*x,yx=1,x=0;
if(a[d]=='-')yx=0;
if(a[d]=='+')yx=1;
}
if(d==lena+1&&x!=0)x2+=(2*yx-1)*x;
printf("%.3lf",(x1-x2)/(y2-y1));
return 0;
} -
02018-08-07 13:14:20@
很长很傻瓜的list解法
简明易懂
只是代码很长// P1022.cpp: 定义控制台应用程序的入口点。 // #include <iostream> #include <cstdio> #include <cstring> #include <list> #include <cmath> #include <cctype> #include <cstdlib> using namespace std; list<char> L, L1, L2; double unk = 0, c = 0; char unkw; int yes = 1; void intial() { if (L.front() != '+' && L.front() != '-') { L.push_front('+'); } int flag = 0; for (list<char>::iterator it = L.begin(); it != L.end(); it++) if (*it >= 'a'&&*it <= 'z') { it--; if (*it<'0' || *it>'9') { it++; L.insert(it, '1'); it--; } it++; } for (list<char>::iterator it = L.begin(); it != L.end(); it++) { if (*it == '=') { flag = 1; it++; if (*it != '+' && *it != '-') { L.insert(it, '+'); } it--; } if (flag == 0) { L1.push_back(*it); } if (flag == 1) { L2.push_back(*it); } } L1.push_back('+'); L2.push_back('+'); } void debug() { cout << "left:"; for (list<char>::iterator it = L1.begin(); it != L1.end(); it++) cout << *it; cout << endl << "right:"; for (list<char>::iterator it = L2.begin(); it != L2.end(); it++) cout << *it; cout << endl; } int ltoi(list<char> tlist) { int ans = 0; int i = 1; int minus = 0; if (tlist.front() == '-')minus = 1; tlist.reverse(); for (list<char>::iterator it = tlist.begin(); it != tlist.end(); it++) { if (*it >= '0'&&*it <= '9') { ans += ((*it) - '0')*i; i *= 10; } } return minus ? (-ans) : (ans); } void deal(list<char> tlist) { if (tlist.back() >= '0'&&tlist.back() <= '9') { c += ltoi(tlist); } else { unk += ltoi(tlist); if (yes) { unkw = tlist.back(); yes = 0; } } } void judge() { int cnt = 0; list<char> curr; list<char>::iterator left, right; for (list<char>::iterator it = L1.begin(); it != L1.end(); it++) { if (*it == '+' || *it == '-') { cnt++; if (cnt == 1) { left = it; } if (cnt == 2) { right = it--; //it++; curr.assign(left, right); cnt = 0; deal(curr); curr.clear(); } } } } void judge2() { int cnt = 0; list<char> curr; list<char>::iterator left, right; for (list<char>::iterator it = L2.begin(); it != L2.end(); it++) { if (*it == '+' || *it == '-') { cnt++; if (cnt == 1) { left = it; } if (cnt == 2) { right = it--; //it++; curr.assign(left, right); if (curr.front() == '+') { curr.pop_front(); curr.push_front('-'); } else { curr.pop_front(); curr.push_front('+'); } cnt = 0; deal(curr); curr.clear(); } } } } int main() { string str; cin >> str; for (int i = 0; i < str.length(); i++) { L.push_back(str[i]); } intial(); // debug(); judge(); judge2(); double x = 0; x = -(c / unk); if (x == 0)x = abs(x); // cout << c << ' ' << unk << endl; cout << unkw << '='; printf("%.3f", x); return 0; }
-
02017-10-27 16:00:39@
比较考细节
分别算出两边的未知数系数和常数,再求未知数#include<iostream> #include<cstring> #include<cstdio> using namespace std; char s[30]; int main() { int i=0,n,flag,sum1=0,xi1=0,xi2=0,sum2=0,now,zi; double ans; cin>>s; n=strlen(s); while(i<n) { flag=1; if(s[i]=='=') break; if(s[i]=='+') { flag=1; i++; } else if(s[i]=='-') { flag=-1; i++; } if(s[i]=='=') break; now=0; while(s[i]>='0'&&s[i]<='9') { now*=10; now+=s[i]-'0'; i++; } if(s[i]>='a'&&s[i]<='z') { zi=s[i]; xi1+=flag*now; i++; } else { sum1+=flag*now; if(s[i]=='=') break; } if(s[i]=='=') break; } i++; while(i<n) { flag=1; if(i>=n) break; if(s[i]=='+') { flag=1; i++; } else if(s[i]=='-') { flag=-1; i++; } if(i>=n) break; now=0; while(s[i]>='0'&&s[i]<='9') { now*=10; now+=s[i]-'0'; i++; } if(s[i]>='a'&&s[i]<='z') { zi=s[i]; xi2+=flag*now; i++; } else { sum2+=flag*now; if(i>=n) break; } if(i>=n) break; } ans=double(double(sum2)-double(sum1))/double(double(xi1)-double(xi2)); cout<<char(zi)<<"="; printf("%.3lf",ans); return 0; }
-
02017-09-17 16:25:48@
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> using namespace std; string a; int main() { cin>>a; double x1=0,x2=0,y1=0,y2=0; int k=a.find('=',0),lena=a.size()-1,yx=1; int x=0,flag=1,d; for(d=0;d<k;++d) { if(a[d]>='0'&&a[d]<='9')x=x*10+a[d]-'0'; if(a[d]!='+'&&a[d]!='-'&&(a[d]>'9'||a[d]<'0')) { if(flag){cout<<a[d]<<"=";flag=0;} if(x==0)y1+=2*yx-1; else y1+=(2*yx-1)*x; x=0; } if(a[d]=='-'&&x!=0)x1+=(2*yx-1)*x,yx=0,x=0; if(a[d]=='+'&&x!=0)x1+=(2*yx-1)*x,yx=1,x=0; if(a[d]=='-')yx=0; if(a[d]=='+')yx=1; } if(a[d]=='='&&x!=0)x1+=(2*yx-1)*x; x=0;yx=1; for(d=k+1;d<=lena;++d) { if(a[d]>='0'&&a[d]<='9')x=x*10+a[d]-'0'; if(a[d]!='+'&&a[d]!='-'&&(a[d]>'9'||a[d]<'0')) { if(flag)cout<<a[d]<<"=",flag=0; if(x==0)y2+=2*yx-1; else y2+=(2*yx-1)*x; x=0; } if(a[d]=='-'&&x!=0)x2+=(2*yx-1)*x,yx=0,x=0; if(a[d]=='+'&&x!=0)x2+=(2*yx-1)*x,yx=1,x=0; if(a[d]=='-')yx=0; if(a[d]=='+')yx=1; } if(d==lena+1&&x!=0)x2+=(2*yx-1)*x; printf("%.3lf",(x1-x2)/(y2-y1)); return 0; }
-
02010-04-10 10:20:13@
var
s,s1,s2,s3:string; c:char;
i,j,l,l1,l2,w:integer; a,b,m:real;
begin
readln(s); l:=length(s); w:=pos('=',s); a:=0; b:=0;
for i:=1 to l do
if ((s[i]'9')) and (s[i]'-')
and (s[i]'=') and (s[i]'+') and (s[i]'.') then begin c:=s[i]; break; end;
s1:=copy(s,1,w-1); s2:=copy(s,w+1,l-w);
l1:=length(s1); l2:=length(s2);
while l1>0 do
begin
i:=2;
while (i -
02009-11-09 13:42:22@
做完难题水题后这道题就很水了
program p1344;
const biao=[ord('a')..ord('z')];
type x=array[1..2] of longint;
var a,b:x;
str,str1,str2,ch:string;
k:longint;
root:real;
procedure init;
var i:longint;
begin
readln(str);
k:=pos('=',str);
for i:=1 to length(str) do if ord(str[i]) in biao then begin ch:=str[i]; break; end;
str1:=copy(str,1,k-1);
str2:=copy(str,k+1,length(str));
if str1[1]'-' then str1:='+'+str1;
str1:=str1+'+';
str2:=str2+'+';
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
end;
procedure calstr(str:string;k:byte);
var i,fh,xs:longint;
d:0..9;
begin
fh:=1; xs:=0;
if ord(str[1]) in biao then str:='+'+str;
for i:=1 to length(str) do
begin
case str[i] of
'0'..'9':begin val(str[i],d); xs:=xs*10+d; end;
'+':begin if ord(str) in biao then begin
if str='+' then inc(a[k],1)
else if str='-' then inc(a[k],-1)
else a[k]:=a[k]+fh*xs
end
else b[k]:=b[k]+fh*xs;
fh:=1; xs:=0;
end;
'-':begin if ord(str) in biao then begin
if str='+' then inc(a[k],1)
else if str='-' then inc(a[k],-1)
else a[k]:=a[k]+fh*xs
end
else b[k]:=b[k]+fh*xs;
fh:=-1; xs:=0;
end;
end;
end;
fh:=1; xs:=0;
end;
begin
init;
calstr(str1,1); calstr(str2,2);
if a[1]-a[2]0 then begin root:=(b[2]-b[1])/(a[1]-a[2]); if root=0 then root:=0; end;
writeln(ch,'=',root:0:3);
end. -
02009-11-05 19:38:03@
用递归的思想
把方程左右两边各分离常数项和一次项,然后整理可变问ax=b的形式,最后解得x -
02009-10-28 21:04:29@
program ex;
var i,j,weizhi,changshu:longint;
s:string;
x:char;
ans:real;procedure main;
var i,j,a,m:longint;
st:string;
begin
for i:=1 to length(s) do
if (s[i] in['0'..'9','+','-','='])=false then
begin
x:=s[i];
break;
end;
i:=1;
while i -
02009-10-25 16:51:48@
var
s,a,b:ansistring;
i,n,k,ma,na,mb,nb,q,m:longint;
ans:real; ch:char;
begin
readln(s);
for i:=1 to length(s) do
if s[i]='=' then
begin k:=i; break; end;
a:=copy(s,1,k-1); b:=copy(s,k+1,length(s)-k);
a:=a+'+'; b:=b+'+';
n:=0; q:=1; i:=1;
while i -
02009-10-24 22:49:50@
program p1344(input,output);
var
s,s1,s2:string;
a:real;
ch:char;
i,c,m,l,b,d,x1,x2,j:integer;
procedure lj(ss:string);
var
i,l:integer;
begin
l:=length(ss);
if (ss[l]>='0')and(ss[l]='0')and(ss[l] -
02009-10-24 16:32:23@
我这个沙茶交了四遍才AC
第一遍调试信息没删干净, 0分
第二遍没处理 x=-0.000 的情况, 83分
第三遍直接在记事本里改, 结果 No Compiled 0分可见我有多沙茶
var
s: -
02009-10-11 13:38:28@
├ 测试数据 01:答案错误... ├ 标准行输出
├ 错误行输出├ 测试数据 02:答案正确... 0ms
├ 测试数据 03:答案正确... 0ms
├ 测试数据 04:答案正确... 0ms
├ 测试数据 05:答案正确... 0ms
---|---|---|---|---|---|---|---|-
Unaccepted 有效得分:83 有效耗时:0ms
为什么受伤的总是我 -
02009-10-07 00:13:05@
今天第5道字符串处理....
烦死我了 -
02009-10-01 22:37:49@
字符串处理 细节题 有点诡异的是‘-0.000’,这个...- -!
program p1344;
var
sys:set of char;
s,temp:string;
can:boolean;
k:char;
top1,top2,x1,x2,num,add,del,i,j:longint;
p:real;
begin
sys:=['+','-'];
read(s);
can:=false;
for i:=1 to length(s) do
if s[i] in ['a'..'z'] then begin
k:=s[i];
break;
end;
temp:='';top1:=0;top2:=0;
for i:=1 to length(s) do
begin
if (not(s[i] in sys))and(s[i]'=')and(ilength(s)) then
begin
temp:=temp+s[i];
continue;
end;
if s[i]='=' then can:=true;
if i=length(s) then temp:=temp+s[i];
if (can=false)or(s[i]='=') then
begin
add:=pos('+',temp);
del:=pos('-',temp);
num:=0;
for j:=1 to length(temp) do
if temp[j] in ['0'..'9'] then
num:=num*10+ord(temp[j])-ord('0');
if temp[length(temp)]k then
begin
if (add=1)or((add=0)and(del=0)) then inc(top1,num)
else dec(top1,num);
end
else
begin
if num=0 then num:=1;
if (add=1)or((add=0)and(del=0)) then inc(x1,num)
else dec(x1,num);
end;
end
else
if (can=true)or(i=length(s)) then
begin
add:=pos('+',temp);
del:=pos('-',temp);
num:=0;
for j:=1 to length(temp) do
if temp[j] in ['0'..'9'] then
num:=num*10+ord(temp[j])-ord('0');
if temp[length(temp)]k then
begin
if (add=1)or((add=0)and(del=0)) then inc(top2,num)
else dec(top2,num);
end
else
begin
if num=0 then num:=1;
if (add=1)or((add=0)and(del=0)) then inc(x2,num)
else dec(x2,num);
end;
end;
temp:=s[i];
if temp='=' then temp:='';
end;
write(k,'=');
p:=(top2-top1)/(x1-x2);
if p0 then writeln(p:0:3)
else writeln('0.000');
end. -
02009-09-11 14:02:31@
注意答案是0的时候
他竟然输出-0.000
白交了一次...