次の二つの条件を満たす自然数を挙げたいです。
1 その数のどこかの桁に数字3、5、9が現れる
2 その数は、3、5、8の公倍数である。
そのために3つの数の公倍数を求めたいのですが、うまくいきません。
@interface ViewController : UIViewController { int answer; NSString * answerstr; NSMutableArray *multiples; NSMutableArray *samemultiples; NSMutableArray *answers; } -(IBAction)ans; @end
-(IBAction)ans{ for(int j = 5; j < 100; j++){ int num3 = 3 * j; int num5 = 5 * j; int num8 = 8 * j; [multiples addObject:[NSString stringWithFormat:@"%d",num3]]; [multiples addObject:[NSString stringWithFormat:@"%d",num5]]; [multiples addObject:[NSString stringWithFormat:@"%d",num8]]; } NSLog(@"%@",multiples); for(int k = 0; k < multiples.count; k++){ if([multiples containsObject:multiples[k]]){ [multiples removeObject:multiples[k]]; NSLog(@"見つかりました!%@",multiples[k]); [samemultiples addObject:multiples[k]]; } } NSLog(@"%@",samemultiples); for(int i = 0; i < samemultiples.count; i++){ answerstr = samemultiples[i]; NSRange range3 = [answerstr rangeOfString:@"3"]; NSRange range5 = [answerstr rangeOfString:@"5"]; NSRange range9 = [answerstr rangeOfString:@"9"]; if (range3.location != NSNotFound && range5.location != NSNotFound && range9.location != NSNotFound) { NSLog(@"answer == %@",answerstr); [answers addObject:answerstr]; } } NSLog(@"%@",answerstr); }
わかる方、宜しくお願いします…
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/26 03:34
2016/12/26 03:53