poj 1001(注意输出数据的格式和输出的格式,如空格,回车等)

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int a[200],na,xiaoshu;
void multiply(int b){
    int i;
    for(i=0;i<na;i++){
        a[i] *= b;
    }
    for(i=0;i<na;i++){
        if(a[i]>=10){
            a[i+1] += a[i]/10;
            a[i] %= 10; 
        }
    }
    while(a[i]>0){
        if(a[i]>=10){
            a[i+1] += a[i]/10;
            a[i] %= 10;
        }
        i++; 
    }
    na = i;
}
void R(int n,int b){
    int i;
    for(i=1;i<n;i++){
        multiply(b);
    }
}
void display(int n){
    bool flag = true;
    int i,j,xiaoshu_r = xiaoshu*n,tmp = na-xiaoshu_r;
    for(i=0;i<xiaoshu_r;i++)
        if(a[i]!=0)flag = false;
    i = 0;
    j = 0;
    while(i<na&&a[i]==0){
        j++;
        i++;
    }
    if(a[na-1]!=0)
        for(i=0;i<tmp;i++)
            printf("%d",a[na-1-i]);
    if(flag){
        printf("\n");
        return;
    }
    printf(".");
    for(i=tmp;i<na-j;i++)
        printf("%d",a[na-1-i]);
    printf("\n");
}
int main(){
    char r[7];
    int n;
    while(scanf("%s%d",r,&n)==2){
        memset(a,0,sizeof(a));
        int i,j = 0,len = strlen(r);
        for(i=len-1;i>=0;i--){
            if(r[i]=='.'){
                xiaoshu = 5-i;
            }
            else{
                a[j++] = r[i]-'0';
            }
        }
        na = j;
        int b = 0;
        for(i=j-1;i>=0;i--){
            b *= 10;
            b += a[i];
        }
        R(n,b);
        display(n);
    }
    return 0;
}

 

上一篇:LeetCode-162. 寻找峰值


下一篇:162-牛客网C语言刷题6