像素旋转 @ 2021-02-23 21:56:57
#include<iostream>
#include<string>
#include<stack>
#include<cmath>
using namespace std;
void getup(string&);
int main(void)
{
int n;
cin >> n;
string a[50];
stack<char> stk;
cin.get();
for (auto i = 0; i < n; i++) {
getup(a[i]);
int digit1 = 0, digit2 = 0, len=0;
if (a[i][0] >= 'a' && a[i][0] <= 'c') {
if (!stk.empty())
stk.pop();
stk.push(a[i][0]);
int temp_j = 0;
for (auto j = 2; j < a[i].length(); j++) {
if (a[i][j] == ' ')
temp_j = j;
}
for (auto j = 2; j < temp_j; j++)
digit1 += (a[i][j] - '0') * pow(10, temp_j - j - 1);
for (auto j = temp_j + 1; j < a[i].length(); j++)
digit2 += (a[i][j] - '0') * pow(10, a[i].length() - j - 1);
len += a[i].length() - 1;
}
else {
len += a[i].length()+1;
int temp_j = 0;
for (auto j = 0; j < a[i].length(); j++)
if (a[i][j] == ' ')
temp_j = j;
for (auto j = 0; j < temp_j; j++)
digit1 += (a[i][j] - '0') * pow(10, temp_j - j - 1);
for (auto j = temp_j + 1; j < a[i].length(); j++)
digit2 += (a[i][j] - '0') * pow(10, a[i].length() - j - 1);
}
//此前len存储的为不包含答案的式子长度
int temp_digit;
int temp_len = 0;
switch (stk.top()) {
case 'a':
temp_digit = digit1 + digit2;
while (temp_digit) {
temp_digit /= 10;
temp_len++;
}
len += temp_len;
cout << digit1 << "+" << digit2 << "=" << digit1 + digit2
<< endl << len << endl;
break;
case 'b':
temp_digit = digit1 - digit2;
if (temp_digit < 0)
len += 1;
while (temp_digit) {
temp_digit /= 10;
temp_len++;
}
len += temp_len;
cout << digit1 << "-" << digit2 << "=" << digit1 - digit2
<< endl << len << endl;
break;
case 'c':
temp_digit = digit1 * digit2;
while (temp_digit) {
temp_digit /= 10;
temp_len++;
}
len += temp_len;
cout << digit1 << "*" << digit2 << "=" << digit1 * digit2
<< endl << len << endl;
break;
}
}
return 0;
}
void getup(string& a) {
char t;
while ((t = cin.get()) != '\n')
a += t;
return;
}
听说getline会re已经改掉了然并卵
by yekgg @ 2021-02-24 18:18:56
蒟蒻的程序
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i;
cin>>n;
char a[n]={' '};
int p[n],q[n];
char buf[500];
char s[100];
cin.getline(s,100);
for(i=0;i<n;i++){
memset(s,0,100);
cin.getline(s,100);
if(s[0]>='0' && s[0]<='9'){
sscanf( s, "%d %d", &p[i], &q[i]);
a[i] = a[i-1];
} else{
sscanf( s, "%c %d %d", &a[i], &p[i], &q[i]);
}
}
for(i=0;i<n;i++){
memset(buf,0,500);
if(a[i]=='a'){
sprintf(buf,"%d+%d=%d",p[i], q[i], p[i]+q[i]);
cout<<buf<<endl;
cout<<strlen(buf)<<endl;
}
else if(a[i]=='b'){
sprintf(buf,"%d-%d=%d",p[i], q[i], p[i]-q[i]);
cout<<buf<<endl;
cout<<strlen(buf)<<endl;
}
else if(a[i]=='c'){
sprintf(buf,"%d*%d=%d",p[i], q[i], p[i]*q[i]);
cout<<buf<<endl;
cout<<strlen(buf)<<endl;
}
}
}
RE点
by 像素旋转 @ 2021-02-28 10:38:15
@yekgg 感谢大佬我以为帖子凉了今天才看到
by 像素旋转 @ 2021-02-28 10:41:30
@yekgg 大佬RE的原因是不能使用getline(cin,string ...)吗?还是说不能使用getchar()。我在自己的ide上运行无误但在洛谷在线的ide上怎么改输入都是运行失败