日历
统计
- 随笔 - 30
- 文章 - 0
- 评论 - 51
- 引用 - 0
导航
常用链接
留言簿(4)
随笔分类
随笔档案
ACM
搜索
最新评论
阅读排行榜
评论排行榜
|
这道题我做的很郁闷,代码也写的不好、很长,方法也一般,有点麻烦,自己都觉得很烂,在提交的时候,发现别人的代码写的很简练,但是不知道自己该怎么改进,希望大家多多提意见,谢谢大家了!
1#include <string.h> 2#include <stdio.h> 3#define LENGTH 100 4 5typedef struct Number 6{ 7 int number; 8 int point; 9 int power; 10}Number; 11 12void ConverTo(char *ch, int *real, int *p) 13{ 14 int b = 1 ,sign = 1; 15 char *c = ch; 16 *real = 0; 17 *p = 0; 18 for( ; *c != '\0' && *c != '.'; c++) 19 *real = (*real)*10 + (*c - '0'); 20 if( *c == '\0') 21 { 22 *p = 0; 23 } 24 else 25 { 26 *p = c-ch; 27 while( *(++c) != '\0') 28 *real = (*real)*10 + (*c - '0'); 29 *p = c-ch-1- *p; 30 } 31} 32 33void Caculate(int *number, int real, int *p, int n, int size) 34{ 35 int sum = 0; 36 int i ,t1, t2; 37 t1 = t2 = 0; 38 while( n-- > 0) 39 { 40 for(i = size-1; i >= 0; i--) 41 { 42 t1 = number[i]*real +t2; 43 number[i] = t1 % 10000; 44 t2 = t1 / 10000; 45 } 46 sum += *p; 47 } 48 *p = sum; 49} 50 51void Convert(char *ch, int *index, int *number, int *left, int right) 52{ 53 int temp; 54 55 while(*left <= right) 56 { 57 temp = number[(*left)++]; 58 ch[(*index)++] = temp/1000 + '0'; 59 temp -= (temp/1000) * 1000; 60 ch[(*index)++] = temp/100 + '0'; 61 temp -= (temp/100) * 100; 62 ch[(*index)++] = temp/10 + '0'; 63 ch[(*index)++] = temp%10 + '0'; 64 65 } 66} 67void ConverToChar(char *ch, int length,int *number, int left, int right, int point) 68{ 69 int last,index, temp, p; 70 index = 0; 71 p = point/4; 72 73 Convert(ch, &index, number, &left, p-1); 74 temp = number[left++]; 75 for(last = 0,p = 1000; last < 4; last++, p /= 10) 76 { 77 if( (4*(left-1) + last) == point) 78 ch[index++] = '.'; 79 ch[index++] = temp/p + '0'; 80 temp -= (temp/p) * p; 81 } 82 Convert(ch, &index, number, &left, right); 83} 84 85void Print(int *number, int p, int size) 86{ 87 int left, right,index; 88 char ch[200]; 89 90 index = (size*4-p)/4; 91 memset(ch, 0, sizeof(ch)); 92 93 for(left = 0; left < index; left++) 94 { 95 if(number[left] == 0) 96 continue; 97 else 98 break; 99 } 100 for(right = size-1; right > index; right--) 101 { 102 if(number[right] == 0) 103 continue; 104 else 105 break; 106 } 107 ConverToChar(ch, sizeof(ch)-1, number, left, right, size*4-p); 108 for(left = 0; ch[left] == '0'; left++) 109 ; 110 for(right = sizeof(ch)-1; ch[right] == '\0' || ch[right] == '0'; right--) 111 ; 112 if(ch[right] == '.') 113 right--; 114 while(left <= right) 115 printf("%c", ch[left++]); 116 printf("\n"); 117} 118 119int main(int argc, char* argv[]) 120{ 121 122 char ch[7]; 123 Number n[LENGTH]; 124 int i = 0, count = 0, p = 0; 125 int number[30]; 126 memset(ch, 0, sizeof(ch)); 127 while(scanf("%s %d", ch, &p) == 2) 128 { 129 ConverTo(ch, &n[count].number, &n[count].point); 130 n[count].power = p; 131 if(!(p<= 25 && p >0) || !(n[count].number> 0 && n[count].number < 99999)) 132 { 133 break; 134 } 135 memset(ch,0, sizeof(ch)); 136 count++; 137 } 138 while(i < count) 139 { 140 memset(number, 0, sizeof(number)); 141 number[sizeof(number)/sizeof(int)-1] = 1; 142 Caculate(number, n[i].number, &n[i].point, n[i].power, sizeof(number)/sizeof(int)); 143 Print(number,n[i].point,sizeof(number)/sizeof(int)); 144 i++; 145 } 146 return 1; 147}
评论:
-
# re: 1001题探讨
Posted @ 2008-06-16 12:33
能否把代码的注释写下
代码功能是做啥的!
如 打印一个3角等! 回复 更多评论
|