| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:贴一个改了N次才提交上的考虑了所有异常输入输出的代码给大家看看,附上几组测试数据In Reply To:贴一个改了N次才提交上的考虑了所有异常输入输出的代码给大家看看,附上几组测试数据 Posted by:xiaohui5319 at 2012-05-18 22:28:32 0 的 0次冥应该是未定义吧
> 特别需要注意的几组测试数据是:
> 0.00000 0
> 1.23456 0
> 123456. 0
> 123456. 1
> 1234.00 1
> 123.000 2
> 大家可以试一下这几组异常数据,如果你都跑过了,而且格式完全正确,AC应该就没什么问题了。
>
> 至于鄙人的代码,的确写的又臭又长,被这个题折磨的暂时没什么好心情去改了。
> #include <cstdio>
> #include <iostream>
> #include <cstdlib>
> #include <cstring>
> using namespace std;
>
> char result[200];
>
> void reverse(char * a)
> {
> int len=strlen(a);
> for(int i=0; i<len/2; i++)
> {
> int temp=a[i];
> a[i]=a[len-1-i];
> a[len-1-i]=temp;
> }
> }
>
> void multiply(char * a, char * b, char * result)//一定要保证输入的a和b指针是不同的
> {
> reverse(a);
> reverse(b);
> int a_len, b_len, i, j;
> a_len=strlen(a);
> b_len=strlen(b);
> int * res=new int[a_len+b_len];
> memset(res, 0, (a_len+b_len)*sizeof(int) );
> for(i=0; i<a_len; i++)
> for(j=0; j<b_len; j++)
> {
> res[i+j] += (a[i]-'0')*(b[j]-'0');
> }
> //调整进位
> i=j=0;
> for(i=0; i<a_len+b_len; i++)
> {
> res[i+j+1] += res[i+j]/10;
> res[i+j] = res[i+j]%10;
> }
> //转换为char类型的result
> for(i=0; i<a_len+b_len; i++)
> result[i]=res[i]+'0';
> result[i]='\0';
> reverse(result);
> reverse(a); //我们设定a为输入的R,所以每次乘法颠倒后,应该保证下一次输入的是正序的R
> delete [] res;
> }
> //函数,求R的n次幂,结果放入result中
> void factorial(char * s, char * result, int n)
> {
> char temp[200];
> if(n==0)
> {
> result[0]='1';
> result[1]='\0';
> }
> else if(n==1)
> {
> strcpy(result, s);
> }
> else
> {
> strcpy(temp, s);
> for(int i=1; i<=n-1; i++)
> {
> multiply(s, temp, result);
> strcpy(temp, result);
> }
> }
> }
>
> bool is_float(char * s, bool & hasDotandIsInt, int & dotIndex)
> {
> int len=strlen(s);
> hasDotandIsInt = false;
> for(int i=0; i<len; i++)
> {
> if(s[i]=='.' && i==len-1)
> {
> hasDotandIsInt = true;
> dotIndex = i;
> return false;
> }
> else if(s[i]=='.' && i!=len-1)
> {
> dotIndex = i;
> if(atoi(s+i+1)==0)
> {
> hasDotandIsInt = true;
> return false;
> }
> else
> return true;
> }
> }
> return false;
> }
>
> int main()
> {
> char s[7];
> int n;
> while(cin>>s>>n)
> {
> if(atof(s) == 0.000000)
> {
> printf("0\n");
> continue;
> }
> if(n == 0)
> {
> printf("1\n");
> continue;
> }
> bool isFloat = false, hasDotandIsInt = false;
> int dotIndex;
> isFloat = is_float(s, hasDotandIsInt, dotIndex);
> if(hasDotandIsInt)
> strcpy(s+dotIndex, "\0");
> //还需要处理输入,比如95.123转换为95123,然后在输入
> if(strcmp(s,"0")==0)
> {
> printf("0\n");
> }
> else if(isFloat==true)
> {
> char sd[7]; //经过处理,将小数点去掉后的s
> char number[200];
> memset(number, '0', sizeof(char)*200);
> int i, flo_fla, flo_bits, des_flo_len;
> i=flo_fla=flo_bits=0;
> //首先将最后的0去掉
> i=strlen(s)-1;
> while(s[i]=='0') i--;
> s[i+1]='\0';
> //第二步将小数位去掉,放入sd数组
> i=0;
> while(s[i]!='.') i++;
> flo_fla=i;
> s[flo_fla]='\0';
> strcpy(sd, s);
> strcpy(&sd[flo_fla], &s[flo_fla+1]);
>
> flo_bits=strlen(sd)-flo_fla;
> des_flo_len=n*flo_bits;
>
> factorial(sd, result, n);
> i=0;
> while(result[i]=='0') i++;
> int in_len=strlen(&result[i])-des_flo_len;
> if( in_len<=0 )
> {
> number[0]='.';
> strcpy(&number[ abs(in_len)+1 ], &result[i]);
> }
> else
> {
> number[in_len]='.';
> strcpy(&number[in_len+1], &result[i+in_len]);
> result[i+in_len]='\0';
> strcpy(number, &result[i]);
> number[in_len]='.';
> }
> cout<<number<<endl;
> }else
> {
> factorial(s, result, n);
> int i=0;
> while(result[i]=='0') i++;
> printf("%s\n", &result[i]);
> }
> }
> return 0;
> }
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator