Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

AC了!!!!

Posted by sun152121 at 2012-03-31 21:02:48 on Problem 1001
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
string mul(string s1,string s2);
string pow(string s,long n);
int main()
{
    char str[101];
    int i,j,n,len,p,lens,flag;
    string s1,s;
    while(scanf("%s%d",str,&n)!=EOF)
    {
        flag=0,len=strlen(str);
        for(i=0;i<len-1;i++)    if(str[i]=='.') {   p=i;flag=1;break;  }
        if(!flag)
        {
            s1=str,s=pow(s1,n);
            strcpy(str,s.c_str());
            printf("%s\n",str);
            continue;
        }

        for(i=len-1;i>=0;i--)
        {
            if(str[i]=='0') continue;
            else break;
        }
        if(str[i]=='.')
        {
            str[i]='\0';s1=str,s=pow(s1,n);
            strcpy(str,s.c_str());
            printf("%s\n",str);
            continue;
        }
        else   str[i+1]='\0';
        len=strlen(str);
        for(i=0;i<len-1;i++)    if(str[i]=='.') {   p=i;break;  }
        for(j=i;j<len-1;j++)    str[j]=str[j+1];
        str[j]='\0';
        s1=str,s=pow(s1,n);
        p=len-(p+1),p*=n,lens=p-s.length();
        if(p>s.length())
        {
            for(i=0;i<lens;i++) s.insert(0,"0");
            s.insert(0,".");
        }
        else    s.insert(s.length()-p,".");
        strcpy(str,s.c_str());
        printf("%s\n",str);
    }
    return 0;
}

string mul(string s1,string s2)
{
	string s;
	int i,j,a,b,c,r,k=0,t=0,st;
	i=s1.length(),j=s2.length();
	k=i+j+1;s.clear();
	for(st=0;st<k;st++)	s.append("0");
	for(j=s2.length()-1;j>=0;j--)
	{
		b=s2[j]-'0';
		t++,st=t;
		for(i=s1.length()-1;i>=0;i--)
		{
			a=s1[i]-'0',c=a*b,r=0;
			r+=c/10,c%=10;
			s[k-st]+=c;c=s[k-st]-'0';
			if(c>9)	{	s[k-st]=c%10+'0';r+=c/10; }
			s[k-st-1]+=r;
			st++;
		}
	}
	for(i=0;i<s.length()-1;i++)   if(s[i]!='0')   break;
	s.erase(0,i);
	return s;
}

string pow(string s,long n)
{
    string ans="1";
    if(!n)  return ans;
    while(n>1)
    {
        if(n%2) {ans=mul(ans,s);n--;}
        else
        {
            s=mul(s,s);
            n/=2;
        }
    }
    ans=mul(ans,s);
    return ans;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator