dcsuibian
dcsuibian
2017-10-12 13:10

jmu ds 顺序表的基本操作

  • java
  • c
  • 顺序表
  • arraylist

图片说明
图片说明

做了n遍,测试点永远过不去
代码写过c和java的

题目的意图不明,不过估计默认情况下元素数量和字母a的数量都是可以满足要求的

 import java.util.*;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        int n;
        Scanner in=new Scanner(System.in);
        n=in.nextInt();
        for(int i=0;i<n;i++) {
            String temp;
            temp=in.next();
            list.add(temp);
        }
        in.close();
        for(int i=0;i<list.size();i++) {
            if(0==i)
                System.out.print(list.get(i));
            else
                System.out.print(" "+list.get(i));
        }
        System.out.println();
        System.out.println(list.size());
        if(list.isEmpty())
            System.out.println("yes");
        else
            System.out.println("no");
        System.out.println(list.get(2));
        for(int i=0;i<list.size();i++) {
            if(list.get(i).equals("a")) {
                System.out.println(i+1);
                break;
            }
        }
        list.add(3,"f");
        for(int i=0;i<list.size();i++) {
            if(0==i)
                System.out.print(list.get(i));
            else
                System.out.print(" "+list.get(i));
        }
        System.out.println();
        list.remove(2);
        for(int i=0;i<list.size();i++) {
            if(0==i)
                System.out.print(list.get(i));
            else
                System.out.print(" "+list.get(i));
        }
        System.out.println();
        return;
    }
}
 #include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#define INF 0x7fffffff
#define MAXN 110
typedef char ElementType;
struct MyStruct
{
    ElementType *arr;
    int cnt;
};
typedef struct MyStruct list;
ElementType MAX(ElementType A, ElementType B)
{
    return A > B ? A : B;
}
ElementType MIN(ElementType A, ElementType B)
{
    return A < B ? A : B;
}
list ReadAndCreate(void)
{
    int n, i;
    char temp[81];
    list l;
    scanf("%d", &n);
    l.cnt = n;
    l.arr = (ElementType*)malloc(n * sizeof(ElementType));
    for (i = 0; i < n; i++) {
        scanf("%s", temp);
        l.arr[i] = temp[0];
    }
    return l;
}
void PrintN(list l)
{
    if (0 == l.cnt)
        return;
    int i;
    for (i = 0; i < l.cnt; i++) {
        if (0 == i)
            printf("%c", l.arr[i]);
        else
            printf(" %c", l.arr[i]);
    }
    printf("\n");
}
void PrintLength(list l)
{
    printf("%d\n", l.cnt);
}
bool IsEmpty(list l)
{
    if (l.cnt <= 0)
        return true;
    else
        return false;
}
void Print3(list l)
{
    if (l.cnt < 3)
        return;
    printf("%c\n", l.arr[2]);
    return;
}
void Print_a_location(list l)
{
    int i;
    bool flag;
    flag = true;
    for (i = 0; i < l.cnt; i++) {
        if ('a' == l.arr[i]) {
            if (flag) {
                printf("%d", i + 1);
                flag = false;
            }
            else
                printf(" %d", i + 1);
        }
    }
    if (!flag)
        printf("\n");
    return;
}
void Insert_f(list *l)
{
    if (l->cnt < 3)
        return;
    int i;
    l->arr = (ElementType*)realloc(l->arr,(++l->cnt) * sizeof(ElementType));
    for (i = l->cnt - 1; i >= 4; i--)
        l->arr[i] = l->arr[i - 1];
    l->arr[3] = 'f';
    return;
}
void Delete3(list *l)
{
    if (l->cnt < 3)
        return;
    int i, j;
    ElementType *t;
    t = l->arr;
    l->arr = (ElementType*)malloc((--l->cnt) * sizeof(ElementType));
    for (i = 0; i < l->cnt; i++) {
        if (i < 2)
            l->arr[i] = t[i];
        else
            l->arr[i] = t[i + 1];
    }
    free(t);
    return;
}
int main()
{
    list l;
    l.arr = NULL;
    l.cnt = 0;
    l = ReadAndCreate();
    PrintN(l);
    PrintLength(l);
    if (IsEmpty(l))
        printf("yes\n");
    else
        printf("no\n");
    Print3(l);
    Print_a_location(l);
    Insert_f(&l);
    PrintN(l);
    Delete3(&l);
    PrintN(l);
    free(l.arr);
    l.cnt = 0;
    getchar();
    getchar();
    return 0;
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答

为你推荐