Copyright (C) IOI日本委員会 1996. All rights reserved.


問題1.

 下記のプログラムにおいて、a=99, b=78 を入力したときの出力結果を答えよ。[1点]


QuickBASICプログラム(1)

DEFINT A-Z
DECLARE SUB compute (a, b, x, y, d)

    PRINT "a,b="; : INPUT a, b
    CALL compute((a), (b), x, y, d)
    PRINT a; "*";
    IF x >= 0 THEN PRINT x;  ELSE PRINT "("; x; ")";
    PRINT "+"; b; "*";
    IF y >= 0 THEN PRINT y;  ELSE PRINT "("; y; ")";
    PRINT "="; d
END

SUB compute (a, b, x, y, d)
    x1 = 1: y1 = 0: x = 0: y = 1
    DO WHILE (a MOD b) <> 0
       q = a \ b
       w = a: a = b: b = w - q * b
       w = x1: x1 = x: x = w - q * x
       w = y1: y1 = y: y = w - q * y
    LOOP
    d = b
END SUB


Cプログラム(1)

#include <stdio.h>

void compute(int a, int b, int *x, int *y, int *d)
{
    int q,x1,y1,w;
   
    x1 = 1; y1 = 0; *x = 0; *y = 1;
    while (a%b != 0) {
        q = a/b;
        w = a;   a = b;   b = w-q*b;
        w = x1; x1 = *x; *x = w-q*(*x);
        w = y1; y1 = *y; *y = w-q*(*y);
    }
    *d = b;
}

void main()
{
    int a,b,x,y,d;

    printf("a,b="); scanf("%d%d", &a,&b);
    compute(a,b,&x,&y,&d);
    printf("%d*", a);
    if (x>=0) printf("%d", x); else printf("(%d)", x);
    printf("+%d*", b);
    if (y>=0) printf("%d", y); else printf("(%d)", y);
    printf("=%d\n", d);
}


Pascalプログラム(1)

program problem1;
var a,b,x,y,d:integer;

    procedure compute(a,b:integer; var x,y,d:integer);
    var q,x1,y1,w:integer;
    begin
        x1 := 1; y1 := 0; x := 0; y := 1; 
        while (a mod b)<>0 do begin
            q := a div b;
            w := a;   a := b; b := w-q*b;  
            w := x1; x1 := x; x := w-q*x;  
            w := y1; y1 := y; y := w-q*y
        end;
        d := b
    end; {compute}

begin
    write('a,b='); readln(a,b);
    compute(a,b,x,y,d);
    write(a,'*');
    if x>=0 then write(x) else write('(',x,')');
    write('+',b,'*');
    if y>=0 then write(y) else write('(',y,')');
    writeln('=',d)
end.


JOI'96へ戻る

JOIホームページへ戻る