【字符串】洛谷P1957 口算练习题——运用字符串处理函数及sprintf函数
P1957 口算练习题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题解 P1957 【口算练习题】 - shangcheng 的博客 - 洛谷博客 (luogu.com.cn)
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; int n,x,y; char ss[20],ans[20],op; int main(){ cin>>n; rep(i,1,n){ cin>>ss; if(isalpha(ss[0])) { op=ss[0]; cin>>x>>y; } else{ x=atoi(ss); cin>>y; } if(op=='a') sprintf(ans,"%d+%d=%d",x,y,x+y); else if(op=='b') sprintf(ans,"%d-%d=%d",x,y,x-y); else if(op=='c') sprintf(ans,"%d*%d=%d",x,y,x*y); printf("%s\n%d\n",ans,strlen(ans)); } return 0; }
Keep it simple and stupid