【模拟】Codeforces 691C Exponential notation

题目链接:

  http://codeforces.com/problemset/problem/691/C

题目大意:

  输入一个数,把它表示成a·10b形式(aEb)。输出aEb,1<=a<10,b如果为1要省略Eb

题目思路:

  【模拟】

  如果字符串没有‘.'我就在最后加上一个'.'方便处理。

  先把头尾多余的0去掉,然后把这个数按照'.'拆成两半,统计整数部分的位数zs。

  接着统计'.'后面的0的个数xs,再把所有数字放到一个数组里,再把头多余的0去掉(0.0000xx)。

  之后按照zs和sx的情况分类输出即可。注意细节。

 1 //
 2 //by coolxxx
 3 //#include<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<stack>
10 #include<queue>
11 #include<set>
12 #include<bitset>
13 #include<memory.h>
14 #include<time.h>
15 #include<stdio.h>
16 #include<stdlib.h>
17 #include<string.h>
18 //#include<stdbool.h>
19 #include<math.h>
20 #define min(a,b) ((a)<(b)?(a):(b))
21 #define max(a,b) ((a)>(b)?(a):(b))
22 #define abs(a) ((a)>0?(a):(-(a)))
23 #define lowbit(a) (a&(-a))
24 #define sqr(a) ((a)*(a))
25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
26 #define mem(a,b) memset(a,b,sizeof(a))
27 #define eps (1e-8)
28 #define J 10
29 #define mod 1000000007
30 #define MAX 0x7f7f7f7f
31 #define PI 3.14159265358979323
32 #define N 1000004
33 using namespace std;
34 typedef long long LL;
35 int cas,cass;
36 int n,m,lll,ans;
37 int zs,xs,e;
38 int a[N];
39 char s[N];
40 int main()
41 {
42     #ifndef ONLINE_JUDGE
43 //    freopen("1.txt","r",stdin);
44 //    freopen("2.txt","w",stdout);
45     #endif
46     int i,j,k;
47     int h,t,p;
48 //    for(scanf("%d",&cass);cass;cass--)
49 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
50     while(~scanf("%s",s))
51 //    while(~scanf("%d",&n))
52     {
53         n=strlen(s);mem(a,0);
54         for(p=0;p<n && s[p]!='.';p++);
55         if(p==n)s[n++]='.';
56         for(h=0;h<n && s[h]=='0';h++);
57         for(t=n-1;t>=0 && s[t]=='0';t--);
58         zs=p-h;
59         for(i=p+1;i<t && s[i]=='0';i++);
60         xs=i-p;
61         if(h==p && t==p){puts("0");continue;}//0.0
62         for(i=t;i>=h;i--)
63         {
64             if(s[i]=='.')continue;
65             a[++a[0]]=s[i]-'0';
66         }
67         while(!a[a[0]] && a[0]>1)a[0]--;
68         printf("%d",a[a[0]]);
69         for(j=1;j<a[0];j++)
70             if(a[j]!=0)break;
71         if(a[0]-1>=j)putchar('.');
72         for(i=a[0]-1;i>=j;i--)
73             printf("%d",a[i]);
74         if(zs>0)
75         {
76             if(zs!=1)printf("E%d
",zs-1);
77         }
78         else
79         {
80             printf("E%d
",-xs);
81         }
82         puts("");
83     }
84     return 0;
85 }
86 /*
87 //
88 
89 //
90 */
View Code
原文地址:https://www.cnblogs.com/Coolxxx/p/5814126.html