Yahoo奇摩知識+ 將於 2021 年 5 月 4 日 (美國東部時間) 終止服務。自 2021 年 4 月 20 日 (美國東部時間) 起，Yahoo奇摩知識+ 網站將會轉為唯讀模式。其他 Yahoo奇摩產品與服務或您的 Yahoo奇摩帳號都不會受影響。如需關於 Yahoo奇摩知識+ 停止服務以及下載您個人資料的資訊，請參閱說明網頁。

# C語言程式 指標

Suppose we have 100 variables and a series of equivalence relations. For instance, 2 3 represents that variable 2 and 3 are equivalent.

If variable 2 is set to a value, then variable 3 is set to that value. Similarly, variable 2 is set when variable 3 is set.After a series of settings, given a sequences of queries, your job is to output the values of the variables.Note that if a variable be queried is not set, then output the value as 0.00.

Input

M(the count of equivalence relations)

Relation 1

Relation 2

Relation M

N(the count of settings)

Setting 1

Setting 2

Setting N

P(the count of queries)

Query 1

Query 2

Query P

Output

The content of queried variables. Each answer is printed using ‘%.2f\n ’.

Sample Input

3

1 3

2 3

4 5

3

1 2.5

2 3.5

4 1.5

5

1 2 3 4 5

Sample Output

3.50

3.50

3.50

1.50

1.50

#include<stdio.h>

int main(void)

{

double *p[101],k[101]={0}; double data[101]={0};

int count[101]={0};int i,j,a,b,queries,settings;

int first,second,third;double *output[101],value;

for(i=0;i<101;i++)

{

p[i]=&k[i];

}

scanf("%d",&first);

for(i=1;i<=first;i++)

{

scanf("%d %d",&a,&b);

if(count[a]==1&&count[b]==0)

{

p[b]=p[a];

}

else{

p[a]=p[b];

count[a]=1;

count[b]=1;

}

}

scanf("%d",&second);

for(i=1;i<=second;i++)

{

scanf("%d",&settings);

scanf("%lf",&data[settings]);

*p[settings]=data[settings];

}

scanf("%d",&third);

for(i=1;i<=third;i++)

{

scanf("%d",&queries);

output[i]=p[queries];

}

for(i=1;i<=third;i++)

{

printf("%.2f\n",*output[i]);

}

return 0;

}

Input

5

1 2

2 3

4 5

5 7

7 1

1

1 2.8

7

1 2 3 4 5 6 7

2.80

2.80

2.80

2.80

2.80

0.00

2.80

### 1 個解答

• 6 年前
最佳解答

#include <stdio.h>

#define N (100)

int main(){

int set[1+N],n,i,j,k,a,b,ax,bx;

double val[1+N]={0.0}, v;

for(i=N;i>=0;--i)set[i]=i;

if(1 != scanf("%d",&n)) return 1;

if(0 >= n) return 2;

for(i=n-1;i>=0;--i) {

if(2!=scanf("%d %d",&a,&b)) return 3;

if(0 >= a || a > N || 0 >= b || b > N) return 4;

k=(a>b)?b:a;

ax = set[a]?set[a]:(1+N);

bx = set[b]?set[b]:(1+N);

k = (k>ax)?ax:k;

k = (k>bx)?bx:k;

for(j=N;j;--j) if(set[j]==ax || set[j]==bx)set[j]=k;

set[a]=set[b]=k;

}

if(1 != scanf("%d", &n)) return 5;

if(0 >= n) return 6;

for(i=n-1;i>=0;--i){

if(2 != scanf("%d %lf",&a, &v)) return 7;

if(0>=a || a>N) return 8;

val[set[a]] = v;

}

if(1 != scanf("%d", &n)) return 9;

for(i=n-1;i>=0;--i){

if(1 != scanf("%d",&a)) return 10;

if(0>=a || a>N) printf("%d value out of range.\n",a);

else printf("%f\n", val[set[a]]);

}

return 0;

}

// test\$ gcc set.c -o set -Wall -O3

\$ ./set

5

1 2

2 3

4 5

5 7

7 1

1

1 2.8

7

1 2 3 4 5 6 7

2.800000

2.800000

2.800000

2.800000

2.800000

0.000000

2.800000

\$ vim set.c