gpt4 book ai didi

ios - 重复数字数组

转载 作者:行者123 更新时间:2023-12-01 19:42:14 25 4
gpt4 key购买 nike

我在创建一个程序时遇到了麻烦,该程序在数组中输入数字并通过字符串返回它们。我下面的代码

main.m文件:

#import <Foundation/Foundation.h>
#import "NumberRepeater.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {
NumberRepeater *repeater1 = [[NumberRepeater alloc]
initWithNumbers: @[@[@1, @10]]];

[repeater1 initWithNumbers:@[@[@1, @10]]];
NSLog(@"%@", [repeater1 process]);

NumberRepeater *repeater2 = [[NumberRepeater alloc]
initWithNumbers: @[@[@1, @2], @[@2, @3]]];

NSLog(@"%@", [repeater2 process]);

NumberRepeater *repeater3 = [[NumberRepeater alloc]
initWithNumbers: @[@[@10, @4], @[@34, @6], @[@92, @2]]];

NSLog(@"%@", [repeater3 process]);
}
return 0;
}

我的NumberRepeater.h文件中的代码:
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NumberRepeater : NSObject
- (instancetype)initWithNumbers:(NSArray *)numbers;

- (NSString *)process;


@end

NS_ASSUME_NONNULL_END

我的NumberRepeater.m文件中的代码:
#import <Foundation/Foundation.h>
#import "NumberRepeater.h"
@implementation NumberRepeater

- (instancetype)initWithNumbers:(NSArray *)numbers {
NumberRepeater *repeater1 = [[NumberRepeater alloc]
initWithNumbers: @[@[@1, @10]]];
[repeater1 initWithNumbers:@[@[@1, @10]]];
NSLog(@"%@", repeater1);

return 0;
}

- (NSString *)process {


return 0;
}

@end

我的预期输出应为:
1111111111
11, 222
10101010, 343434343434, 9292

这绝对不是我需要解决的学校作业。我希望有一个解决方案,但是对此的指导会很棒。我做完类似的问题并得到所需的输出后,感觉差不多快要掌握了,但显然我很想念,我只是不知道什么。我了解我的处理方法缺少代码,只是我不完全知道该放在哪里。任何帮助将不胜感激!

最佳答案

•我需要根据您的问题为您解释数组语法,

  • 首先,您可以通过这种方式声明数组@[@1,@2,....]NSNumber数组
    对象为Int Value,因为数组仅包含id对象,因此我们不能直接使用Int,而是使用NSnumber @ 1
  • 数组内的
  • 对象可以是Array自身,因此我们可以声明array对象的array
    像这个例子@[@[@10, @4], @[@3, @6], @[@2, @9]]这个数组数组
    @[@[],@[],....etc]

  • •根据您的问题,我给您 NSArray两个项目
    请给我一个字符串,其中第一项重复(第二项)次
       Example :

    Input @[@1,@2] repeat number#1 two times
    Output should be 11

    Input @[@1,@10] repeat number#1 10 times
    Output should be 1111111111

    Input @[@[@1,@3],@[@3,@4]] repeat number#1 3 times , number#3 4 time
    Output should be 111 3333

    解决方案

    NumberRepeater.h
    #import <Foundation/Foundation.h>

    NS_ASSUME_NONNULL_BEGIN

    @interface NumberRepeater : NSObject {
    NSArray * numbers; // private number array will be initialized during initialization of object
    }
    - (instancetype)initWithNumbers:(NSArray *)numbers;

    - (NSString *)process;
    @end

    NS_ASSUME_NONNULL_END

    NumberRepeater.m
    #import "NumberRepeater.h"
    #import <Foundation/Foundation.h>


    // NumberRepeater Extention for set private numbers
    @interface NumberRepeater ()
    @property (nonatomic,retain) NSArray *numbers;
    @end


    @implementation NumberRepeater
    @synthesize numbers;

    - (instancetype)initWithNumbers:(NSArray *)numbers {

    self = [super init];
    if (self) {
    self.numbers = numbers;
    }
    return self;
    }

    - (NSString *)process {

    // if numbers have No Data
    if (numbers.count == 0) { return @"0"; }

    // Array of array each array have two element first is number to repeat and second have number of repetation
    // Example @[@1, @2] repeat number(1) TWO times so out put should be 11
    // @[@[@1, @2], @[@2, @3]]

    NSMutableString * output = [NSMutableString new];

    for (NSArray*item in numbers) {

    // Array should be with this Format
    // Example @[@1, @2] repeat number(1) TWO times

    // assert item have tow items only like that @[@1,@2]
    NSAssert(item.count == 2,@"Array Should have Two items only");

    NSNumber *numberToRepeat = item[0]; // repeat
    NSNumber *repetitionCount = item[1]; //repetition Count

    NSMutableString * repeatdString = [NSMutableString new];

    for (NSInteger i= 0; i < [repetitionCount integerValue] ; i++) {
    [repeatdString appendString:[numberToRepeat stringValue]];
    }

    [output appendString:repeatdString];
    [output appendFormat:@" "]; // space on join with muti array

    }


    return output;
    }
    @end

    主目录
    #import <Foundation/Foundation.h>
    #import "NumberRepeater.h"

    int main(int argc, const char * argv[]) {
    @autoreleasepool {
    NumberRepeater *repeater1 = [[NumberRepeater alloc]
    initWithNumbers: @[@[@1, @10]]];

    NSLog(@"%@", [repeater1 process]);

    NumberRepeater *repeater2 = [[NumberRepeater alloc]
    initWithNumbers: @[@[@1, @2], @[@2, @3]]];

    NSLog(@"%@", [repeater2 process]);

    NumberRepeater *repeater3 = [[NumberRepeater alloc]
    initWithNumbers: @[@[@10, @4], @[@34, @6], @[@92, @2]]];

    NSLog(@"%@", [repeater3 process]);
    }
    return 0;
    }

    关于ios - 重复数字数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53644724/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com