1、ACMICPC南太平洋赛区试题解析12003年ACM-ICPC南太平洋赛区试题解析-1 ACM国际大学生程序设计竞赛由国际计算机学界著名的ACM学会(Association for Computer Machinery)主办,是世界上规模最大、水平最高的国际大学生程序竞赛。本文作为CSTC团队之ACM培训的启动工程训练内容,请从此题入手,开始你们痛并快乐着的ACM之旅。 此题虽为ACM比赛用题,却非常简单,没有什么特别之处,不牵扯数据结构,实为初学者建立自信心的灵丹妙药。 英文原文 P1 - ACM SPPC 1 of 2 Saturday, 20/09/2003 PROBLEM 1 - WA
2、CMIAN NUMBERS In the supposedly uninhabited Wacmahara Desert, a tribe of unusual people has been discovered. The Wacmians have only 2 fingers and a thumb on each hand, and have invented their own numbering system. The digits they use and the symbols they use for digits are quite unusual, but anthrop
3、ologists have been able to represent them as follows: % represents 0 ) represents 1 represents 2 represents 3 ? represents 4 represents 5 $ represents -1 (yes, they even have a negative digit) As you may expect, their system is base 6 where each place value is 6 times the value to its right, as in t
4、he following examples: )% is 1*62+3*6+0 = 36+18+0 = 54 ?$ is 4*63+(1)*62+2*6+2 = 86436+12+2 = 842 $ is (1)*62+2*6+2 = 36+12+2 = -22 Your task is to take Wacmian numbers and represent them as standard base 10 numbers. P1 - ACM SPPC 2 of 2 Saturday, 20/09/2003 INPUT FORMAT Input consists of Wacmian nu
5、mbers, one per line. Each number consists of a sequence of 1 to 10 Wacmian digits. A single # on a line by itself indicates the end of input. SAMPLE INPUT: )% ?$ $ % # OUTPUT FORMAT Output will be the corresponding decimal numbers, one per line. SAMPLE OUTPUT: 54 842 -22 0 这里我们提供出来本题的中文对照: Wacmian 数
6、字 在假设的Wacmahara无人沙漠里,一个非普通人组成的部落被发现了。Wacmians的每个手上仅有两个手指和一个拇指,并且他们发明了自己的数字系统。他们使用的数字和用来表示数字的符号都很奇特,但是人类学家已经能够以用下面的方法描述它们: % - 0 ) - 1 - 2 - 3 ? - 4 - 5 $ - -1 (没错,他们甚至有负数) 如你所愿,他们的系统是以6为基础的,每位上的数值达到6就像该位的左边进位,如同随后的例子: )% 表示 1*62+3*6+0 = 36+18+0 = 54 ?$ 表示 4*63+(-1)*62+2*6+2 = 864-36+12+2 = 842 $ 表示
7、(-1)*62+2*6+2 = -36+12+2 = -22 你的任务是把Wacmian数字解释成标准的10进制数字。 输入格式 输入包括Wacmian数字,每行一个。每个数字由1至10个有顺序的Wacmian数字组成。一个单独的#占用一行,它标志输入结束。输入举例: )% ?$ $ % # 输出格式 输出相应的是进制数字,每行一个。 输出举例: 54 842 -22 0本题的解答如下: #include #include int GetData(int *digit,FILE *fp) char temp; int i=0; /使用temp记录从input.in文件中逐个读出的字符 temp
8、 = fgetc(fp); /如果为回车符,表示该数的读取已经结束了 while(temp !=n) switch (temp) case %: digiti = 0;break; case ): digiti = 1;break; case : digiti = 2;break; case : digiti = 3;break; case ?: digiti = 4;break; case /: digiti = 5;break; case $: digiti = -1;break; case #: return -1; / 让函数返回 -1 表示input.in文件结束 /digit10用
9、来统计数中的位数 digit10 = i+1; i+; /数组下标增加 1 /读入下一个字符 temp = fgetc(fp); return 0; void main() /题目中给每个数最多只有10位 /所以数组定义11位 /前10位保存每位的数字 /最后一位统计位数 int digit11; long number = 0; /考虑到极值状况,采用long类型 long temp = 1; int i,j; FILE *fpInput,*fpOutput; fpInput = fopen( input.in,r); fpOutput = fopen(output.out,w); digi
10、t10 = 0; while(GetData(digit,fpInput) != -1) for(i=0; idigit10; i+) for(j=0; jdigit10-i-1; j+) temp *= 6; number += (digiti*temp); temp = 1; /将每一位转换成10进制后都需要为temp初始化 /以便下一位的转换时使用 fprintf(fpOutput,%ldn,number); number = 0; /每个数字转换完成后number同样需要重新初始化 fclose(fpInput); fclose(fpOutput); posted on 2004-06
11、-12 19:57 monkeyking 阅读(790) 评论(1) 编辑收藏 引用 网摘 所属分类: 原创 FeedBack: # 2003年ACM-ICPC南太平洋赛区试题解析-12004-09-13 16:43 | lili#include #include intGetData(int*digit,FILE*fp) chartemp; inti=0; /使用temp记录从input.in文件中逐个读出的字符 temp=fgetc(fp); /如果为回车符,表示该数的读取已经结束了 while(temp!=n) switch(temp) case%:digiti=0;break; cas
12、e):digiti=1;break; case:digiti=2;break; case:digiti=3;break; case?:digiti=4;break; case/:digiti=5;break; case$:digiti=-1;break; case#:return-1;/让函数返回-1表示input.in文件结束 /digit10用来统计数中的位数 digit10=i+1; i+;/数组下标增加1 /读入下一个字符 temp=fgetc(fp); return0; voidmain() /题目中给每个数最多只有10位 /所以数组定义11位 /前10位保存每位的数字 /最后一位统
13、计位数 intdigit11; longnumber=0;/考虑到极值状况,采用long类型 longtemp=1; inti,j; FILE*fpInput,*fpOutput; fpInput=fopen(input.in,r); fpOutput=fopen(output.out,w); digit10=0; while(GetData(digit,fpInput)!=-1) for(i=0;idigit10;i+) for(j=0;jdigit10-i-1;j+)temp*=6; number+=(digiti*temp); temp=1; /将每一位转换成10进制后都需要为temp初始化 /以便下一位的转换时使用 fprintf(fpOutput,%ldn,number); number=0;/每个数字转换完成后number同样需要重新初始化 fclose(fpInput); fclose(fpOutput);
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2