Search Ⅰ

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
Search

Search Ⅰ

#1

投稿記事 by Search » 14年前

[1] 質問

n個の整数を含む集合Sと、q個の異なる整数を含む集合Tを入力とし、Tに含まれる整数の中でSに含まれるものの数Cを出力するプログラムを教えて下さい。

入力ルール
1行目にn、2行目にSを表すn個の整数、3行目にq、4行目にTを表すq個の整数が与えられる。
出力ルール
Cを1行に出力。
制約
n ≤ 10000
q ≤ 500
0 ≤ Sの要素 ≤ 109
0 ≤ Tの要素 ≤ 109

入力例 1

5
1 2 3 4 5
3
3 4 1

出力例 1

3

入力例 2

3
3 1 2
1
5

出力例 2

0

[2] 環境  
 [2.1] OS : Mac 等々
 [2.2] コンパイラ名 : gcc

beatle
記事: 1281
登録日時: 14年前
住所: 埼玉
連絡を取る:

Re: Search Ⅰ

#2

投稿記事 by beatle » 14年前

フォーラムルールでは丸投げ質問を禁止しておりますので、Searchさんが今までやった部分を提示してください。
さっぱり分からないなら勉強方法からアドバイスを受けるようにして下さい。
それから、Search IとIIの違いは何なのでしょうか。

アバター
みけCAT
記事: 6734
登録日時: 15年前
住所: 千葉県
連絡を取る:

Re: Search Ⅰ

#3

投稿記事 by みけCAT » 14年前

beatle さんが書きました:それから、Search IとIIの違いは何なのでしょうか。
nとqの範囲が違うようですね。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
h2so5
副管理人
記事: 2212
登録日時: 15年前
住所: 東京
連絡を取る:

Re: Search Ⅰ

#4

投稿記事 by h2so5 » 14年前

書いてみました

コード:

#import <objc/objc.h>
#import <objc/Object.h>
#import <Foundation/Foundation.h>

@implementation Main
int main()
{
    id pool = [[NSAutoreleasePool alloc] init];
    int i, n, q, c = 0;
    NSMutableArray *S = [[[NSMutableArray alloc] init] autorelease];
    NSMutableArray *T = [[[NSMutableArray alloc] init] autorelease];

    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        int num;
        scanf("%d", &num);
        NSNumber *ip = [[[NSNumber alloc] initWithInt: num] autorelease];
        [S addObject: ip];
    }

    scanf("%d", &q);
    for (i = 0; i < q; i++) {
        int num;
        scanf("%d", &num);
        NSNumber *ip = [[[NSNumber alloc] initWithInt: num] autorelease];
        [T addObject: ip];
    }

    NSEnumerator *e1 = [T objectEnumerator];
    id obj1;
    while (obj1 = [e1 nextObject]) {
        NSEnumerator *e2 = [S objectEnumerator];
        id obj2;
        while (obj2 = [e2 nextObject]) {
            if([obj1 isEqualToNumber: obj2]) {
                c++;
            }
        }
    }

    printf("%d", c);
    [pool release];
    return 0;
}
@end

Search

Re: Search Ⅰ

#5

投稿記事 by Search » 14年前

h2so5 さんへ

ご回答頂きありがとうございました。あれからLinux環境のTerminalで、emacsで書いたA.mというファイルをコンパイルして実行してみたのですが、下記のエラーが出てしまいました。Objective-Cは使ったことがないのですが、もし可能でしたらC言語での実装の仕方をお教えくださると幸いです。

gcc A.m

A.m:3:34: error: Foundation/Foundation.h: No such file or directory
A.m:6: warning: cannot find interface declaration for 'Main'
A.m: In function 'main':
A.m:8: error: 'NSAutoreleasePool' undeclared (first use in this function)
A.m:8: error: (Each undeclared identifier is reported only once
A.m:8: error: for each function it appears in.)
A.m:10: error: 'NSMutableArray' undeclared (first use in this function)
A.m:10: error: 'S' undeclared (first use in this function)
A.m:11: error: 'T' undeclared (first use in this function)
A.m:17: error: 'NSNumber' undeclared (first use in this function)
A.m:17: error: 'ip' undeclared (first use in this function)
A.m:29: error: 'NSEnumerator' undeclared (first use in this function)
A.m:29: error: 'e1' undeclared (first use in this function)
A.m:32: error: 'e2' undeclared (first use in this function)
A.m:35: warning: no '-isEqualToNumber:' method found
A.m:35: warning: (Messages without a matching method signature
A.m:35: warning: will be assumed to return 'id' and accept
A.m:35: warning: '...' as arguments.)
A.m:42: warning: no '-release' method found
A.m: At top level:
A.m:45: error: stray '#' in program
A.m:45: error: syntax error before '<' token
A.m:54: error: syntax error before '*' token
A.m:54: error: 'NSMutableArray' undeclared here (not in a function)
A.m:54: warning: data definition has no type or storage class
A.m:55: error: syntax error before '*' token
A.m:55: warning: data definition has no type or storage class
A.m:57: error: syntax error before string constant
A.m:57: error: conflicting types for 'scanf'
A.m:57: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
A.m:57: warning: data definition has no type or storage class
A.m:58: error: syntax error before '<' token
A.m:60: error: syntax error before string constant
A.m:60: error: conflicting types for 'scanf'
A.m:60: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
A.m:60: warning: data definition has no type or storage class
A.m:61: error: syntax error before '*' token
A.m:61: error: 'NSNumber' undeclared here (not in a function)
A.m:61: error: 'num' undeclared here (not in a function)
A.m:61: warning: data definition has no type or storage class
A.m:62: error: syntax error before '[' token
A.m:65: error: syntax error before string constant
A.m:65: error: conflicting types for 'scanf'
A.m:65: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
A.m:65: warning: data definition has no type or storage class
A.m:66: error: syntax error before '<' token
A.m:68: error: syntax error before string constant
A.m:68: error: conflicting types for 'scanf'
A.m:68: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
A.m:68: warning: data definition has no type or storage class
A.m:69: error: syntax error before '*' token
A.m:69: error: redefinition of 'ip'
A.m:61: error: previous definition of 'ip' was here
A.m:69: warning: data definition has no type or storage class
A.m:70: error: syntax error before '[' token
A.m:73: warning: invalid receiver type 'int *'
A.m:73: warning: no '-objectEnumerator' method found
A.m:73: warning: initialization from incompatible pointer type
A.m:73: error: initializer element is not constant
A.m:73: warning: data definition has no type or storage class
A.m:75: error: syntax error before 'while'
A.m:78: error: syntax error before 'while'
A.m:85: error: syntax error before string constant
A.m:85: error: conflicting types for 'printf'
A.m:85: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
A.m:85: warning: data definition has no type or storage class
A.m:89: warning: '@end' must appear in an @implementation context

ご丁寧なご回答を感謝致します!

アバター
softya(ソフト屋)
副管理人
記事: 11677
登録日時: 15年前
住所: 東海地方
連絡を取る:

Re: Search Ⅰ

#6

投稿記事 by softya(ソフト屋) » 14年前

h2so5 さんのは、丸投げは禁止なのでサンプルを示すから自力で移植してみてってことです。
と言うことでフォーラムルールに従って頂きますようにお願いします。※ 他の回答者を無視しないでください。
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。

閉鎖

“C言語何でも質問掲示板” へ戻る