Definite Values

FJNU.1673

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
",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 will contain multiple lists, one per line. Each element of the list will be separated by a comma followed a space, and the list will be terminated by a period. The input will be terminated by a line containing only a single period.

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

Source
Southwestern Europe 1997, Practice

My Program

#include < iostream >
using   namespace  std;

int  main()
{
    
bool dim[26],flag;
    
char a,c,temp[4];
    
int d=0,i,n,m,t;
    
while(cin>>n)
    
{
        
if(n==0)
            
break;
        flag
=false;
        memset(dim,
false,sizeof(dim));
        dim[
0]=true;
        gets(temp);
        
for(i=0;i<n;i++)
        
{
            gets(temp);
            t
=0;
            
while(temp[t]==' '||temp[t]=='=') t++;
            a
=temp[t];
            t
++;
            
while(temp[t]==' '||temp[t]=='=') t++;
            c
=temp[t];
            
if(dim[c-'a']==true)
                dim[a
-'a']=true;
            
else
                dim[a
-'a']=false;
        }

        cout
<<"Program #"<<++d<<endl;
        
for(i=0;i<26;i++)
            
if(dim[i])
                m
=i;
        
for(i=0;i<26;i++)
            
if(dim[i])
            
{
                flag
=true;
                printf(
"%c ",i+'a');
            }

        
if(!flag)
            cout
<<"none";
        cout
<<endl<<endl;
    }

    
return 0;
}

YOYO's Note:
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄它是华丽的分隔线

【题意简述】

赋值问题。一开始只有a变量有值,给定n个赋值语句,判定最后从a~z中哪些个变量有值。


【粗略分析】

以前就做过的问题……
开个数组标记该变量是否有值,
然后读数据并对该数组进行相应的操作,
最后输出结果即可。

【C++源代码】

#include < iostream >
using   namespace  std;

int  main()
{
    
bool dim[26],flag;
    
char a,c,temp[4];
    
int d=0,i,n,m,t;
    
while(cin>>n)
    
{
        
if(n==0)
            
break;
        flag
=false;
        memset(dim,
false,sizeof(dim));
        dim[
0]=true;
        gets(temp);
        
for(i=0;i<n;i++)
        
{
            gets(temp);
            t
=0;
            
while(temp[t]==' '||temp[t]=='=') t++;
            a
=temp[t];
            t
++;
            
while(temp[t]==' '||temp[t]=='=') t++;
            c
=temp[t];
            
if(dim[c-'a']==true)
                dim[a
-'a']=true;
            
else
                dim[a
-'a']=false;
        }

        cout
<<"Program #"<<++d<<endl;
        
for(i=0;i<26;i++)
            
if(dim[i])
                m
=i;
        
for(i=0;i<26;i++)
            
if(dim[i])
            
{
                flag
=true;
                printf(
"%c ",i+'a');
            }

        
if(!flag)
            cout
<<"none";
        cout
<<endl<<endl;
    }

    
return 0;
}

【注意事项】

※ 比较恶心地拿到了PE:它的每个字符输出都会带一个空格。每组数据都带一行空白行就不用说了。


【点评】

没啥好说的……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值