hdu 1327 Definite Values

本文深入探讨了程序开发的核心内容,包括但不限于前端开发、后端开发、移动开发、游戏开发等细分领域。从代码实现到工具使用,全面覆盖了开发过程中的关键知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=1327

题目描述:

Definite Values

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1202    Accepted Submission(s): 475


Problem Description
A common error in programming is to use variables that have not been initialized before. For example, in C and C++, all variables have an indefinite value after declaration - their value can be anything. Thus, the following program

main()
{
int x;
printf("%d\n",x);
}

could print any number. But even in languages such as Pascal, where all values are initialized to zero, it is useful to give variables definite values before using them, the avoid side effects when your code portion is placed into a different context.

Generally, the problem of deciding for a given program whether all variables have been assigned values before they are read out, is undecidable. But if you, as in this problem, consider only a sequence of assignments, the problem becomes solvable.
 

Input
The input contains several program parts. Each part starts with a number n on a line by itself, the number of lines in the program part. The following n lines contain each an assignment of the form "variable1 = variable2", where the variablei's are lower-case letters.

The input is terminated by a test case starting with n = 0.
 

Output
Assume that before the execution of the given program part, variable a has some definite value, while all other variables are undefined. You have to print the names of the varaibles which have a definite value after the execution of the program part. More specifically, format your output as follows.

For each program part in the input, first print the number of the program, as shown in the sample output. Then print a line containing the names of the variables which have a definite value after the execution of the given program part. Print them in alphabetically sorted order, and leave one blank after each variable name. If none of the variables has a definite value after the execution of the program part, print the word "none".

Print a blank line after each test case.
 

Sample Input
  
4 b = a c = d d = b e = f 1 a = b 0
 

Sample Output
  
Program #1 a b d Program #2 none

题意:

判断一段程序运行后,哪些变量是明确有定义的,哪些变量不是明确有定义的,初始化变量a是有定义的。

题解:

用map映射表,来实时记录变量中的值是否有明确定义,每走一行代码,映射表实时记录各个变量的情况。

注意:

这里hdu的oj的判断有问题,比如样例中输出: a b c
的格式是 a空格b空格c空格
而我第一次做的输出格式是a空格b空格c,结果得到了presentation error的错误。
也就是说一个样例输出一行字符,每个字符后面都必须跟一个空格字符,即使是最后一个字符。
代码:

#include<stdio.h>
#include<string.h>
int map_chars[300];//map['a'] = 1 is definite and 0 is indefinite
int n=0,cases=0;
char in_str[10];
int main()
{
    while(scanf("%d",&n)!=EOF&&n>0)
    {
        printf("Program #%d\n",++cases);
        memset(map_chars,0,sizeof(map_chars));
        map_chars['a']=1;
        for(int i=1;i<=n;i++)
        {
            fgets(in_str,10,stdin);
            while(in_str[0]=='\n') fgets(in_str,10,stdin);
            map_chars[in_str[0]]=map_chars[in_str[4]];
        }
        int flag=0,i=0;
        for(i='a';i<='z';i++)
        {
            if(map_chars[i]>0)
            {
                flag=1;
                printf("%c ",i);
            }
        }
        if(!flag) printf("none");
        printf("\n\n");
    }
    return(0);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值