patB1024/A1073 scientic notation

#include <iostream>
#include <cstring>
using namespace std;


int main(int argc, char** argv) {
string ss;
cin>>ss;
string::iterator it=ss.begin();
string se="E";   string sp=".";
int pos=ss.find(se);
int mul=0;                           //e后面的数字; 
for(int i=pos+2;i<ss.size();i++){
mul=mul*10+(ss[i]-'0');
}


if(ss[pos+1]=='-'){                        //指数为负数的时候; 
if(*it=='-') cout<<"-";{
ss.erase(it);
int pospt=ss.find(sp);
cout<<"0.";
for(int i=0;i<mul-1;i++) cout<<"0";
cout<<*it;
for(int i=pospt+1;i<=pos-2;i++){
cout<<ss[i];
}
}
      if(*it=='+'){
      int pospt=ss.find(sp);
cout<<"0.";
for(int i=0;i<mul-1;i++) cout<<"0";
cout<<*(it+1);
for(int i=pospt+1;i<=pos-1;i++){
cout<<ss[i];

}
}

  if(ss[pos+1]=='+'){                            //指数为正数的时候; 
  int countpre=0;
  int pospt=ss.find(sp);            //点的位置; E的位置pos; 
  countpre=pos-pospt-1;
  int countzero=mul-countpre;
 
  /**
  统计一下,.到E前有多少个数字; countpre; 
 
*/ 

  if(*it=='-'){
  cout<<"-";
ss.erase(it);
      cout<<*it;
     
      if(countpre>mul){
for(int i=pospt;i<=pospt+mul-1;i++){
cout<<ss[i];
}
cout<<".";
for(int i=pospt+mul;i<=pos-2;i++) cout<<ss[i];
}
else{
for(int i=pospt;i<=pos-2;i++){          //.往后到E前; 
cout<<ss[i];

for(int u=0;u<countzero;u++) cout<<"0";
}

}
      if(*it=='+'){                                  //与上面类似,但是已经ac了,不改了,但是这个地方确实有问题; 
      cout<<*(it+1);
for(int i=pospt+1;i<=pos-1;i++){
cout<<ss[i];

for(int u=0;u<countzero;u++) cout<<"0";
}
}
return 0;

}



阅读更多

Polish notation

12-01

Problem DescriptionnReverse Polish notation (RPN) is a method for representing expressions in which the operator symbol is placed after the arguments being operated on. nPolish notation, in which the operator comes before the operands, was invented in the 1920s by the Polish mathematician Jan Lucasiewicz. nIn the late 1950s, Australian philosopher and computer scientist Charles L. Hamblin suggested placing the operator after the operands and hence created reverse polish notation. nnRPN has the property that brackets are not required to represent the order of evaluation or grouping of the terms. nRPN expressions are simply evaluated from left to right and this greatly simplifies the computation of the expression within computer programs. nAs an example, the arithmetic expression (3+4)*5 can be expressed in RPN as 3 4 + 5 *. nnReverse Polish notation, also known as postfix notation, contrasts with the infix notation of standard arithmetic expressions in which the operator symbol appears between the operands. So Polish notation just as prefix notation.nnNow, give you a string of standard arithmetic expressions, please tell me the Polish notation and the value of expressions.n nnInputnThere're have multi-case. Every case put in one line, the expressions just contain some positive integers(all less than 100, the number of integers less than 20), bi-operand operators(only have 3 kinds : +,-,*) and some brackets'(',')'.nyou can assume the expressions was valid.n nnOutputnEach case output the Polish notation in first line, and the result of expressions was output in second line.nall of the answers are no any spaces and blank line.the answer will be not exceed the 64-signed integer.n nnSample Inputn1+2-3*(4-5)n1+2*(3-4)-5*6n nnSample OutputnCase 1:n- + 1 2 * 3 - 4 5n6nCase 2:n- + 1 * 2 - 3 4 * 5 6n-31n

Known Notation

08-24

Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in Marjar University. He is learning RPN recent days.nnTo clarify the syntax of RPN for those who haven't learnt it before, we will offer some examples here. For instance, to add 3 and 4, one would write "3 4 +" rather than "3 + 4". If there are multiple operations, the operator is given immediately after its second operand. The arithmetic expression written "3 - 4 + 5" in conventional notation would be written "3 4 - 5 +" in RPN: 4 is first subtracted from 3, and then 5 added to it. Another infix expression "5 + ((1 + 2) × 4) - 3" can be written down like this in RPN: "5 1 2 + 4 × + 3 -". An advantage of RPN is that it obviates the need for parentheses that are required by infix.nnIn this problem, we will use the asterisk "*" as the only operator and digits from "1" to "9" (without "0") as components of operands.nnYou are given an expression in reverse Polish notation. Unfortunately, all space characters are missing. That means the expression are concatenated into several long numeric sequence which are separated by asterisks. So you cannot distinguish the numbers from the given string.nnYou task is to check whether the given string can represent a valid RPN expression. If the given string cannot represent any valid RPN, please find out the minimal number of operations to make it valid. There are two types of operation to adjust the given string:nnInsert. You can insert a non-zero digit or an asterisk anywhere. For example, if you insert a "1" at the beginning of "2*3*4", the string becomes "12*3*4".nSwap. You can swap any two characters in the string. For example, if you swap the last two characters of "12*3*4", the string becomes "12*34*".nThe strings "2*3*4" and "12*3*4" cannot represent any valid RPN, but the string "12*34*" can represent a valid RPN which is "1 2 * 34 *".nnInputnnThere are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:nnThere is a non-empty string consists of asterisks and non-zero digits. The length of the string will not exceed 1000.nnOutputnnFor each test case, output the minimal number of operations to make the given string able to represent a valid RPN.nnSample Inputnn3n1*1n11*234**n*nSample Outputnn1n0n2n

没有更多推荐了,返回首页