博文

IP address(2007-04-11 14:02:00)

摘要:Problem description Suppose you are reading byte streams from any device, representing IP addresses. Your task is to convert a 32 characters long sequence of '1s' and '0s' (bits) to a dotted decimal format. A dotted decimal format for an IP address is form by grouping 8 bits at a time and converting the binary representation to decimal representation. Any 8 bits is a valid part of an IP address. To convert binary numbers to decimal numbers remember that both are positional numerical systems, where the first 8 positions of the binary systems are: 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1  Input The input will have a number N (1<=N<=9) in its first line representing the number of streams to convert. N lines will follow.  Output The output must have N lines with a doted decimal IP address. A dotted decimal IP address is formed by grouping 8 bit at the time and converting the binary representation to decimal representation.  Sample Input 4000000000000000000000......

阅读全文(2380) | 评论:0

functions(2007-04-11 14:01:00)

摘要:Problem description Recurrence relations are where a function is defined in terms of itself and maybe other functions as well. in this problem, we have two functions of interest: F (N) = F (N - 1) + G (N - 1) ; F (1) = 1 G (N) = G (N - 1) + G (N - 3) ; G (1) = 1, G (2) = 0, G (3) = 1 For a given value of N, compute F (N).  Input Each line of input will have exactly one integer, 57 > = N > 0.  Output For each line of input, output F(N).  Sample Input 1457 Sample Output 132035586497    #include <stdio.h> unsigned long getfn(unsigned long n);void getgn(); unsigned long   a[60]; unsigned long getfn(unsigned long n){ unsigned long i = 0,j = 0,k = 0;   if(n == 1) i = 1;   else  { j = getfn(n - 1);   k = a[n - 1];   i = j + k; }  return i;} void getgn(){ int n,m,i,j;   a[1] = 1;  a[2] = 0;  a[3] = 1;  for(n = 4,i = 0,j = 0; n <......

阅读全文(2541) | 评论:0

self-number(2007-04-11 14:00:00)

摘要:Problem description In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar.) For example, d(75) = 75 + 7 + 5 = 87. Given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d(n), d(d(n)), d(d(d(n))), .... For example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57, and so you generate the sequence 33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ... The number n is called a generator of d(n). In the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and so on. Some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no generators is a self-number. There are thirteen self-numbers less tha......

阅读全文(3997) | 评论:0

区间(2007-04-11 13:57:00)

摘要:区间    Problem description 现给定n个闭区间[ai, bi],1<=i<=n。这些区间的并可以表示为一些不相交的闭区间的并。你的任务就是在这些表示方式中找出包含最少区间的方案。你的输出应该按照区间的升序排列。这里如果说两个区间[a, b]和[c, d]是按照升序排列的的,那么我们有a<=b 请写一个程序:读入这些区间;计算满足给定条件的不相交闭区间;把这些区间按照升序输出.  Input 第一行包含一个整数n,3<=n<=50000,为区间的数目。以下n行为对区间的描述,第i行为对第i个区间的描述,为两个整数1<=ai<=bi<=1000000,表示一个区间[ai, bi]。  Output 你应该输出计算出来的不相交的区间。每一行都是对一个区间的描述,包括两个用空格分开的整数,为区间的上下界。你应该把区间按照升序排序。  Sample Input 55 61 410 106 98 10 Sample Output 1 45 10 Problem Source SDOI  #include <stdio.h>#include <malloc.h> typedef struct Node{ int i;  int j;  struct Node *next;}QNODE;QNODE *head = NULL,*temp = NULL,*ahead = NULL; int enter(){ int n,m,max;  QNODE *qtemp;   head = temp = (QNODE*)malloc(sizeof(QNODE));  head -> next = NULL;  scanf("%d",&m);  for(max = 0,n = 0; n < m; n ++) { scanf("%d %d",&temp -> i,&temp -> j);   if( temp ->......

阅读全文(3489) | 评论:1

最大公约数(2007-04-11 13:56:00)

摘要:求最大公约数,1<=a,b<=100000000;   #include <stdio.h> long test(long a,long b){ long  m,i,j,n;    if ( !(a % b) ) m = b;   else { for( n = 2,i = b / 2 + 2; n < i; n ++)  if( !(b % n) )     { j = b / n;       if( !(a % j ) )   { m = j ;     break;   }    }    }   return m;}   int main(){ int   n,m;  long  a,b;   scanf("%d",&m);  for(n = 0; n < m; n ++) { scanf("%ld %ld",&a,&b);   if( a > b )      printf("%ld\n",test(a,b));   else      printf("%ld\n",test(b,a));        } return 0;}   ......

阅读全文(2554) | 评论:0

奖学金(2007-04-11 13:55:00)

摘要:谁拿了最多奖学金   Problem description 某校的惯例是在每学期的期末考试之后发放奖学金。发放的奖学金共有五种,获取的条件各自不同: 1) 院士奖学金,每人8000元,期末平均成绩高于80分(>80),并且在本学期内发表1篇或1篇以上论文的学生均可获得; 2) 五四奖学金,每人4000元,期末平均成绩高于85分(>85),并且班级评议成绩高于80分(>80)的学生均可获得; 3) 成绩优秀奖,每人2000元,期末平均成绩高于90分(>90)的学生均可获得; 4) 西部奖学金,每人1000元,期末平均成绩高于85分(>85)的西部省份学生均可获得; 5) 班级贡献奖,每人850元,班级评议成绩高于80分(>80)的学生干部均可获得; 只要符合条件就可以得奖,每项奖学金的获奖人数没有限制,每名学生也可以同时获得多项奖学金。例如姚林的期末平均成绩是87分,班级评议成绩82分,同时他还是一位学生干部,那么他可以同时获得五四奖学金和班级贡献奖,奖金总数是4850元。 现在给出若干学生的相关数据,请计算哪些同学获得的奖金总数最高(假设总有同学能满足获得奖学金的条件)。  Input 输入的第一行是一个整数N(1 <= N <= 100),表示学生的总数。接下来的N行每行是一位学生的数据,从左向右依次是姓名,期末平均成绩,班级评议成绩,是否是学生干部,是否是西部省份学生,以及发表的论文数。姓名是由大小写英文字母组成的长度不超过20的字符串(不含空格);期末平均成绩和班级评议成绩都是0到100之间的整数(包括0和100);是否是学生干部和是否是西部省份学生分别用一个字符表示,Y表示是,N表示不是;发表的论文数是0到10的整数(包括0和10)。每两个相邻数据项之间用一个空格分隔。  Output 输出包括三行,第一行是获得最多奖金的学生的姓名,第二行是这名学生获得的奖金总数。如果有两位或两位以上的学生获得的奖金最多,输出他们之中在输入文件中出现最早的学生的姓名。第三行是这N个学生获得的奖学金的总数。  Sample Input 4YaoLin 87 82 Y N 0ChenRuiyi 88 78 N Y 1LiXin 92 88 N N 0ZhangQin 83......

阅读全文(2738) | 评论:0

行编辑器(2007-04-11 13:54:00)

摘要:行编辑器  Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB  Total submit users: 80, Accepted users: 77  Problem 10428 : No special judgement  Problem description 一个简单的行编辑程序的功能是:接受用户从终端输入的程序或数据,并存入用户的数据区。 由于用户在终端上进行输入时,不能保证不出差错,因此,若在编辑程序中,“每接受一个字符即存入用户数据区”的做法显然不是最恰当的。较好的做法是,设立一个输入缓冲区,用以接受用户输入的一行字符,然后逐行存入用户数据区。允许用户输入出差错,并在发现有误时可以及时更正。例如,当用户发现刚刚键入的一个字符是错的时,可补进一个退格符"#",以表示前一个字符无效; 如果发现当前键入的行内差错较多或难以补救,则可以键入一个退行符"@",以表示当前行中的字符均无效。 如果逻辑上的位置已经在行首,则继续输入'#'符号无效。  Input 输入一个多行的字符序列。但行字符总数(包含退格符和退行符)不大于250。  Output 按照上述说明得到的输出。  Sample Input whli##ilr#e(s#*s)   outcha@putchar(*s=#++); Sample Output while(*s)putchar(*s++); Problem Source HNU Contest    #include <stdio.h>#include <string.h>#include <malloc.h> struct str{ char *str;  struct str *next;} *head,*temp; void deal(char a[],char b[]){ int i,j,len;    len = strlen(a);  for(i = 0,j = 0; i < len; i ......

阅读全文(2296) | 评论:0

二进制转十六进制(汇编)(2007-04-11 13:11:00)

摘要:;---------------------------------------------------------;输入二进制数,转为十六进制后输出;--------------------------------------------------------- .model small  .data MAX_BIN  db 100  ;二进制长度限制 REAL_LEN db ?  ;实际的输入 BIN_DATA db 100 dup(?) ;存储输入的01 RADIX_DATA db 1,2,4,8 ;2^x(x=0,1,2,3) MSG_PROM db 'Please input binary code :','$' MSG_ERR  db 'Input error,you must enter 0 or 1 !',13,10,'$' MSG_RLT  db 'The binary code to hex is:','$' BIN_TO_HEX db 100 dup(?).code;---------------------------------------------------------;屏幕输出以 '$' 结尾的字符串,OPR为字符串变量;---------------------------------------------------------macPutTxt macro OPR mov dx,offset OPR mov ah,09h int 21h endm ;---------------------------------------------------------;屏幕打印回车换行符,用到寄存器;---------------------------------------------------------macPutEnt macro  mov dl,0dh mov ah,02h int&nbs......

阅读全文(7858) | 评论:4

中断及驻留程序(汇编)(2007-04-10 13:27:00)

摘要:;---------------------------------------------------------;修改除数为 0 的中断处理程序,并驻留内存;由于 windows 的多任务环境,任务之间相互隔离,该程序在;windows 下并不能真正驻留到内存中作为除0的中断处理程序;--------------------------------------------------------- .model small.stack.code  ;---------------------------------------------------------;结束程序,回到 DOS,用到寄存器 ax,dx;---------------------------------------------------------macExit macro mov ah,4ch int 21h endm ;---------------------------------------------------------;屏幕打印回车换行符,用到寄存器;---------------------------------------------------------macPutEnt macro  push ax push dx mov dl,0dh mov ah,02h int 21h mov dl,0ah mov ah,02h int 21h pop dx pop ax endm ;---------------------------------------------------------;屏幕输出以 '$' 结尾的字符串,OPR为字符串变量;用到寄存器 ax,dx;---------------------------------------------------------macPutTxt macro OPR push ax&nbs......

阅读全文(4390) | 评论:0

发声程序(汇编)(2007-04-09 12:29:00)

摘要:;---------------------------------------------------------;控制扬声器发声,可惜声音不大;--------------------------------------------------------- .model small data segment sntimes dw 10000 ;发声频率 sndelay dw 10000 ;延迟时间data ends code segment assume cs:code,ds:data ;---------------------------------------------------------;结束程序,回到 DOS ;---------------------------------------------------------macExit macro mov ah,4ch int 21h endm ;---------------------------------------------------------; 控制扬声器发声; cx:发声频率 bx:延迟时间;---------------------------------------------------------makeSound proc far push ax push dx mov dx,cx in al,61h  ;61h 为 I/O 地址 and al,11111100b ;第 1 位为控制发声的开关,第 0 位不用mkSound: xor al,2  ;是第 0 位的值 0,1交替 out 61h,al mov cx,bxmkSnDelay: loop mkSnDelay  dec dx jnz ......

阅读全文(6346) | 评论:0