20.R的n次幂scanf memset

输入
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

输出
The output will consist of one line for each line of input giving the exact value of Rn. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don’t print the decimal point if the result is an integer.

样例
输入样例 1 复制
#R n
95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12

#include<stdio.h>
#include<string.h>
#include<math.h>
#define p 7
#define len 120
int main(){
    int mult(int*a,int*b);
    int n,i,j,k,location,flag;
    int an[len],bn[len];
    char sz[p];
    /**scanf扫描n变量返回整数n,注意需要使用&符号,就是直接引用变量,改变变量值**/
    while(scanf("%s %d",sz,&n)==2){
            memset(an,0,sizeof(an));//分配内存,指向an,内存大小为sizeof(an),元素初始化为0的Sas码
            memset(bn,0,sizeof(bn));
            an[0]=1;
            location=-1;
        for(i=0;i<p-1;i++){
            if(sz[i]=='.'){
                location=i;//memory the location of the dot
                location=n*(p-2-i);//get the new location
                for(j=i;j<p-1;j++)
                    sz[j]=sz[j+1];//remove the dot
                    sz[p-1]='\0';
            }
        }
        for(i=0;i<strlen(sz);i++)
            bn[i]=sz[strlen(sz)-1-i]-'0';
             for(i=0;i<n;i++)
            mult(an,bn);
        if(location==-1){
        flag=0;
        for(i=len-1;i>=0;i--){
            if(flag)printf("%d",an[i]);
        else if(an[i]){
            printf("%d",an[i]);
            flag=1;
            }
        }
         if(!flag)printf("0");
         printf("\n");
        }//整数情况
        else//小数
        {
            for(i=0;;i++){
                 k=i;
                if(an[i]!=0)break;
            }
           for(i=len-1;i>=k;i--){
               if(an[i])break;
           }
           if(i<location){
            printf(".");
            for(i=location-1;i>=k;i--)printf("%d",an[i]);
            printf("\n");
           }
           else{
                if(k<location){
            flag=0;
            for(i=len-1;i>=k;i--){
            if(flag){
                    printf("%d",an[i]);
                    if(i==location&&i!=k)printf(".");
            }
            else if(an[i]){
            printf("%d",an[i]);
            if(i==location&&i!=k)printf(".");
            flag=1;
            }
        }
                    printf("\n");
           }
           else{
            flag=0;
            for(i=len-1;i>=location;i--){
            if(flag){
                    printf("%d",an[i]);
            }
            else if(an[i]){
            printf("%d",an[i]);
            flag=1;
            }
        }
          if(!flag)printf("0");
          printf("\n");
           }
           }
        }
    }
}
int mult(int*a,int*b){
    int i,j,k,l;
    int c[len];
    memset(c,0,sizeof(c));
    for(i=0;i<len;i++){
        k=0;
        for(j=0;j<len-i;j++){
            l=c[i+j]+a[j]*b[i]+k;
            c[i+j]=l%10;
            k=l/10;
        }
    }
    for(i=0;i<len;i++)a[i]=c[i];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值