ページ 1 / 1
Search Ⅰ
Posted: 2012年5月11日(金) 05:25
by Search
[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
Re: Search Ⅰ
Posted: 2012年5月11日(金) 07:01
by beatle
フォーラムルールでは丸投げ質問を禁止しておりますので、Searchさんが今までやった部分を提示してください。
さっぱり分からないなら勉強方法からアドバイスを受けるようにして下さい。
それから、Search IとIIの違いは何なのでしょうか。
Re: Search Ⅰ
Posted: 2012年5月11日(金) 07:59
by みけCAT
beatle さんが書きました:それから、Search IとIIの違いは何なのでしょうか。
nとqの範囲が違うようですね。
Re: Search Ⅰ
Posted: 2012年5月11日(金) 15:16
by h2so5
書いてみました
コード:
#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
Re: Search Ⅰ
Posted: 2012年5月15日(火) 15:45
by Search
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
ご丁寧なご回答を感謝致します!
Re: Search Ⅰ
Posted: 2012年5月15日(火) 15:51
by softya(ソフト屋)
h2so5 さんのは、丸投げは禁止なのでサンプルを示すから自力で移植してみてってことです。
と言うことでフォーラムルールに従って頂きますようにお願いします。※ 他の回答者を無視しないでください。