Time Limit: 500MS | Memory Limit: 10000K | |
Total Submissions: 71191 | Accepted: 16721 |
本题总的来说并不是很难,但是却很麻烦,我在北大上做题时wrong了n次,最终总算通过了,我不知道大牛们是怎么做的,但是我的做法的确是很麻烦
思想是:就是将输入的自然数a去掉小数点后,然后进行n次大整数相乘,每次的乘数是输入以后去掉小数点的a,被乘数是前面乘过以后的结果。在结束完n次以后将小数点加到合适的位置,并进行前后的除零。
代码:
1 #include < stdio.h >
2 #include < string .h >
3 int main()
4 {
5 int i,j,sum,k,res[ 500 ][ 500 ],flag = 0 ,b,dox,mark,deadline,t;
6 char result[ 500 ],a[ 65 ],tran[ 500 ];
7 while (scanf( " %s " ,a) != EOF)
8 {
9 sum = 0 ;k = 0 ;dox = 0 ;mark = 0 ;
10 memset(result, 0 , sizeof (result));
11 result[ 0 ] = ' 1 ' ;
12 getchar();
13 scanf( " %d " , & b);
14 int len1 = strlen(a);
15 if (a[ 0 ] == ' 0 ' )
16 {
17 for (i = 2 ;a[i] != ' \0 ' ;i ++ )
18 a[i - 2 ] = a[i];
19 len1 -= 2 ;
20 dox = len1;
21 a[len1] = ' \0 ' ;
22 }
23 else
24 {
25 for (i = 0 ;a[i] != ' \0 ' ;i ++ )
26 {
27 if (a[i] == ' . ' )
28 {
29 mark = 1 ;
30 len1 -- ;
31 continue ;
32 }
33 if (mark)
34 {
35 a[i - 1 ] = a[i];
36 dox ++ ;
37 }
38 }
39 a[len1] = ' \0 ' ;
40 }
41 for (t = 1 ;t <= b;t ++ )
42 {
43 memset(res, 0 , sizeof (res));
44 int len2 = strlen(result);
45 strcpy(tran,result);
46 for (i = 0 ;i < len2;i ++ )
47 result[i] = tran[len2 - 1 - i];
48 k = 0 ;sum = 0 ;
49 for (i = 0 ;i < len1;i ++ )
50 for (j = 0 ;j < len2;j ++ )
51 res[i][j] = (a[i] - ' 0 ' ) * (result[j] - ' 0 ' );
52 for (i = len1 - 1 ;i >= 0 ;i -- )
53 {
54 for (j = len2 - 1 ;j >= 0 ;j -- )
55 sum = sum + res[i + len2 - j - 1 ][j];
56 result[k] = sum % 10 + ' 0 ' ;
57 k = k + 1 ;
58 sum = sum / 10 ;
59 }
60 for (i = len2 - 2 ;i >= 0 ;i -- )
61 {
62 for (j = 0 ;j <= i;j ++ )
63 sum = sum + res[i - j][j];
64 result[k] = sum % 10 + ' 0 ' ;
65 k = k + 1 ;
66 sum = sum / 10 ;
67 }
68 if (sum != 0 )
69 {
70 result[k] = sum + ' 0 ' ;k = k + 1 ;
71 }
72 }
73 dox = dox * b;
74 int len = strlen(result);
75 if (dox != 0 )
76 {
77 if (dox <= len - 1 )
78 {
79 for (i = len - 1 ;i >= dox;i -- )
80 result[i + 1 ] = result[i];
81 result[dox] = ' . ' ;
82 len ++ ;
83 }
84 else
85 {
86 result[dox] = ' . ' ;
87 for (i = dox - 1 ;i > len - 1 ;i -- )
88 result[i] = ' 0 ' ;
89 }
90 }
91 deadline = 0 ;
92 len = len - 1 > dox ? len - 1 :dox;
93 for (i = 0 ;i <= len;i ++ )
94 {
95 if (result[i] != ' 0 ' )
96 {
97 if (result[i] == ' . ' )
98 deadline = i + 1 ;
99 else
100 deadline = i;
101 break ;
102 }
103 }
104 mark = 0 ;
105
106 for (i = len;i >= deadline;i -- )
107 if (mark == 1 || result[i] != ' 0 ' )
108 {
109 mark = 1 ;
110 printf( " %c " ,result[i]);
111 }
112 printf( " \n " );
113 }
114 return 0 ;
115 }
116
(注意:) 我在之前wrong了几次,主要是少考虑了几种情况,经过某位大牛的指点才能通过,此大牛给出的例子是 :
4.000 2 结果应该是16 并非16.
10.000 2 结果应该是100 并非是1或者100.