CodeForces 697B Barnicle

本文介绍了一种将科学计数法表示的数字转换为常规小数形式的方法,并提供了两种实现方式的代码示例。

B - Barnicle
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Barney is standing in a bar and starring at a pretty girl. He wants to shoot her with his heart arrow but he needs to know the distance between him and the girl to make his shot accurate.

Barney asked the bar tender Carl about this distance value, but Carl was so busy talking to the customers so he wrote the distance value (it's a real number) on a napkin. The problem is that he wrote it in scientific notation. The scientific notation of some real number x is the notation of form AeB, where A is a real number and B is an integer and x = A × 10B is true. In our case A is between 0 and 9 and Bis non-negative.

Barney doesn't know anything about scientific notation (as well as anything scientific at all). So he asked you to tell him the distance value in usual decimal representation with minimal number of digits after the decimal point (and no decimal point if it is an integer). See the output format for better understanding.

Input

The first and only line of input contains a single string of form a.deb where ad and b are integers and e is usual character 'e' (0 ≤ a ≤ 9, 0 ≤ d < 10100, 0 ≤ b ≤ 100) — the scientific notation of the desired distance value.

a and b contain no leading zeros and d contains no trailing zeros (but may be equal to 0). Also, b can not be non-zero if a is zero.

Output

Print the only real number x (the desired distance value) in the only line in its decimal notation.

Thus if x is an integer, print it's integer value without decimal part and decimal point and without leading zeroes.

Otherwise print x in a form of p.q such that p is an integer that have no leading zeroes (but may be equal to zero), and q is an integer that have no trailing zeroes (and may not be equal to zero).

Sample Input

Input
8.549e2
Output
854.9
Input
8.549e3
Output
8549
Input
0.33e0
Output
0.33

科学计数转直接表达,有点麻烦。

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;

int tr(char str[]){
    int len=strlen(str);
    int ans=0;
    for(int i=0;i<len;i++){
        ans*=10;
        ans+=str[i]-'0';
    }
    return ans;
}
int main()
{
    char str[1000];memset(str,0,sizeof(str));
    while(cin>>str){
        char str10[1000];
        char num[1000];
        char ans2[1000];int p=-1;char ans[1000];
        memset(num,0,sizeof(num));
        memset(str10,0,sizeof(str10));
        memset(ans,0,sizeof(ans));
        memset(ans2,0,sizeof(ans2));
        int len=strlen(str);
        bool flag=1;int ppp=-1,pppp=-1,pso;
        for(int i=0;i<len;i++){
            if(str[i]=='e') {flag=0;continue;}
            if(str[i]=='.') pso=i;
            if(flag)
                num[i]=str[i];
            else
                str10[++ppp]=str[i];

        }
        int len10=tr(str10);
        //cout<<num<<endl;

        int pp=-1;
        int lennum=strlen(num);
        int newpso=pso+len10;
    //cout<<newpso<<endl;
    //cout<<lennum<<endl;
        if(newpso>=lennum){
            for(int i=0;i<=newpso;i++){
                if(i<lennum)
                    if(num[i]!='.')
                        ans[++pp]=num[i];
                    else
                        continue;
                else
                    ans[++pp]='0';
            }
        }else{
            for(int i=0;i<lennum;i++)
                if(num[i]!='.')
                ans[++pp]=num[i];
            strcpy(num,ans);
            pp=-1;
            lennum=strlen(num);
            for(int i=0;i<lennum;i++){
                ++pp;
                if(pp==newpso){
                    ans[pp]='.';
                    i--;
                }
                else
                    ans[pp]=num[i];
            }
        }
        //cout<<ans<<endl;
        int lenans=strlen(ans);
        flag=0;
        for(int i=0;i<lenans;i++){
            if(ans[i]!='0'||ans[i]=='.') flag=1;
            if(flag){
                ans2[++p]=ans[i];
            }
        }
        if(ans2[0]=='.'){
            ans[0]='0';
            int len=strlen(ans2);
            for(int i=0;i<len;i++)
                ans[i+1]=ans2[i];
        }else{
            strcpy(ans,ans2);
        }
        len=strlen(ans);
        flag=0;bool flag2=0;
        for(int i=0;i<len;i++)
            if(!flag){
                if(ans[i]=='.')
                flag=1;
            }else{
                if(ans[i]!='0'){
                    flag2=1;
                    break;
                }
            }
        if(flag2)
            cout<<ans<<endl;
        else{
            for(int i=0;i<len;i++){
                if(ans[i]=='.') break;
                cout<<ans[i];
            }
            cout<<endl;
        }
    }
}
也有简单的写法(转) 见http://blog.youkuaiyun.com/shyazhut/article/details/52224422

#include<stdio.h>  
#include<cstring>  
const int MYDD=1103;  
  
int main() {  
    int a,k,b;  
    char d[MYDD];  
    scanf("%d.",&a); //两个scanf不可合为一个  
    scanf("%[^e]%ne%d",d,&k,&b);  
    //note: "%[^e]" 读入任意多的字符,直到遇到 "=" 停止  
    if(k==1&&d[0]==48&&!b)  printf("%d\n",a);  
    //note: 这里要是改为字符型判断d[0]=='0'时间复杂度增加一倍,可达到 30ms   
    else if(b>=k)                printf("%d%s%.*d\n",a,d,b-k,0);  
    else                        printf("%d%.*s.%s\n",a,b,d,d+b);  
                        // 参数 b 输出字符串 d 的个数  
    return 0;  
}  






### 关于 Codeforces 1853B 的题解与实现 尽管当前未提供关于 Codeforces 1853B 的具体引用内容,但可以根据常见的竞赛编程问题模式以及相关算法知识来推测可能的解决方案。 #### 题目概述 通常情况下,Codeforces B 类题目涉及基础数据结构或简单算法的应用。假设该题目要求处理某种数组操作或者字符串匹配,则可以采用如下方法解决: #### 解决方案分析 如果题目涉及到数组查询或修改操作,一种常见的方式是利用前缀和技巧优化时间复杂度[^3]。例如,对于区间求和问题,可以通过预计算前缀和数组快速得到任意区间的总和。 以下是基于上述假设的一个 Python 实现示例: ```python def solve_1853B(): import sys input = sys.stdin.read data = input().split() n, q = map(int, data[0].split()) # 数组长度和询问次数 array = list(map(int, data[1].split())) # 初始数组 prefix_sum = [0] * (n + 1) for i in range(1, n + 1): prefix_sum[i] = prefix_sum[i - 1] + array[i - 1] results = [] for _ in range(q): l, r = map(int, data[2:].pop(0).split()) current_sum = prefix_sum[r] - prefix_sum[l - 1] results.append(current_sum % (10**9 + 7)) return results print(*solve_1853B(), sep='\n') ``` 此代码片段展示了如何通过构建 `prefix_sum` 来高效响应多次区间求和请求,并对结果取模 \(10^9+7\) 输出[^4]。 #### 进一步扩展思考 当面对更复杂的约束条件时,动态规划或其他高级技术可能会被引入到解答之中。然而,在没有确切了解本题细节之前,以上仅作为通用策略分享给用户参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值