4.3作业

1.循环输入n个元素,计算最大差值,最小和,最大和 (不允许使用排序)

最大差:最大值-最小值

最小和:最小值+第二小值

最大和:最大值+第二大值

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     int n;
  7     printf("请输入元素个数:");
  8     scanf("%d",&n);
  9     int arr[n],i;
 10     for(i=0;i<n;i++)
 11     {
 12         printf("请输入arr[%d]=",i);
 13         scanf("%d",&arr[i]);
 14     }
 15     int max1=arr[0],min1=arr[0];
 16     for(i=1;i<n;i++)
 17     {
 18         if(max1<arr[i]) max1=arr[i];
 19         if(min1>arr[i]) min1=arr[i];
 20     }
 21     printf("最大差为%d\n",max1-min1);
 22     int max2=arr[0],min2=arr[0];
 23     for(i=0;i<n;i++)
 24     {
 25         if(max2<arr[i]&&arr[i]!=max1)
 26         max2=arr[i];
 27         else if(max2==max1)
 28         max2=arr[1];
 29         if(min2>arr[i]&&arr[i]!=min1)
 30         min2=arr[i];
 31         else if(min2==min1)
 32         min2=arr[1];
 33     }
 34     printf("最大和为%d,最小和为%d\n",max1+max2,min1+min2);
 35     return 0;
 36 }                                                                                                                                    

2.循环输入n个元素,交换数组中最大值和最小值

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     int n;
  7     printf("请输入元素个数:");
  8     scanf("%d",&n);
  9     int arr[n],i;
 10     for(i=0;i<n;i++)
 11     {
 12         printf("请输入arr[%d]=",i);     
 13         scanf("%d",&arr[i]);
 14     }
 15     int max=0,min=0;
 16     for(i=1;i<n;i++)
 17     {
 18         if(arr[max]<arr[i]) max=i;
 19         if(arr[min]>arr[i]) min=i;
 20     }
 21     int t=arr[max];
 22     arr[max]=arr[min];
 23     arr[min]=t;
 24     for(i=0;i<n;i++)
 25     printf("%d ",arr[i]);
 26     putchar(10);
 27     return 0;
 28 }

3.循环输入n个元素,输入查找元素key

如果key在数组中出现1次,则输出对应的位置

如果key在数组中出现0次,则提示不存在

如果key在数组中出现多次,则输出出现的次数

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     int n;
  7     printf("请输入元素个数:");
  8     scanf("%d",&n);
  9     int arr[n],i;
 10     for(i=0;i<n;i++)
 11     {
 12         printf("请输入arr[%d]=",i);
 13         scanf("%d",&arr[i]);
 14     }
 15     int key;
 16     printf("请输入查找元素key:");
 17     scanf("%d",&key);
 18     int j,k;                                     
 19     j=0;
 20     for(i=0;i<n;i++)
 21     {
 22         if(arr[i]==key)
 23         {
 24             j++;
 25             k=i;
 26         }
 27     }
 28     if(j==0)
 29     printf("%d不存在\n",key);
 30     else if(j==1)
 31     printf("%d的位置在数组的第%d个",key,k+1);
 32     else
 33     printf("%d在数组中出现了%d次",key,j);
 34     return 0;
 35 }

4.循环输入n个元素,输入flag,

如果flag==1,则实现冒泡排序升序并输出

如果flag==0,则实现简单选择排序降序并输出

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     int n;
  7     printf("请输入元素个数:");
  8     scanf("%d",&n);
  9     int arr[n],i;
 10     for(i=0;i<n;i++)
 11     {
 12         printf("请输入arr[%d]=",i);
 13         scanf("%d",&arr[i]);
 14     }
 15     int flag,j;
 16     printf("请输入flag:");
 17     scanf("%d",&flag);
 18     if(flag==1)
 19     {
 20         for(i=1;i<n;i++)
 21         {
 22             for(j=0;j<n-i;j++)
 23             {
 24                 if(arr[j]>arr[j+1])
 25                 {   int t;
 26                     t=arr[j];
 27                     arr[j]=arr[j+1];
 28                     arr[j+1]=t;
 29                 }
 30             }
 31         }
 32         printf("冒泡排序结果为:");
 33         for(i=0;i<n;i++)
 34         printf("%d ",arr[i]);
 35         putchar(10);
 36     }
 37     else if(flag==0)
 38     {
 39         int m,count=0;
 40         for(i=0;i<n-1;i++)
 41         {
 42             m=i;
 43             for(j=i+1;j<n;j++)
 44             {
 45                 if(arr[m]<arr[j])
 46                 m=j;
 47             }
 48             if(m!=i)
 49             arr[m]=arr[m]+arr[i],arr[i]=arr[m]-arr[i],arr[m]=arr[m]-arr[i];
 50         }
 51         printf("简单排序结果为:");
 52         for(i=0;i<n;i++)
 53         printf("%d ",arr[i]);
 54         putchar(10);
 55     }                                                                      
 56     else
 57     printf("error");
 58     return 0;
 59 }

5.循环输入数组元素,实现数组元素逆置

eg:int arr[]={11,22,33,44}

逆置后的结果是

44 33 22 11

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     int n;
  7     printf("请输入元素的个数:");
  8     scanf("%d",&n);
  9     int i,a[n],b[n];
 10     for(i=0;i<n;i++)
 11     {
 12         printf("a[%d]=",i);
 13         scanf("%d",&a[i]);
 14     }
 15     printf("逆置前的数组为:");
 16     for(i=0;i<n;i++)
 17     {
 18         printf("%d ",a[i]);
 19     }
 20     putchar(10);
 21     for(i=0;i<n;i++)
 22     {
 23         b[n-1-i]=a[i];
 24     }
 25     for(i=0;i<n;i++)
 26     {
 27         a[i]=b[i];                           
 28     }
 29     printf("逆置后的数组为:");
 30     for(i=0;i<n;i++)
 31     {
 32         printf("%d ",a[i]);
 33     }
 34     putchar(10);
 35     return 0;
 36 }

6.循环输入n个数组元素,输入key,请删除所有和key相等的元素

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     int n;
  7     printf("请输入元素的个数:");
  8     scanf("%d",&n);
  9     int i,a[n];
 10     for(i=0;i<n;i++)
 11     {
 12         printf("a[%d]=",i);
 13         scanf("%d",&a[i]);
 14     }
 15     int k,j;
 16     printf("请输入key:");
 17     scanf("%d",&k);
 18     for(i=0;i<n;i++)
 19     {
 20         if(k==a[i])
 21         {
 22             for(j=i;j<n-1;j++)
 23             {
 24                 a[j]=a[j+1];
 25             }
 26             a[n-1]=0;
 27             n=n-1;                         
 28         }
 29     }
 30     printf("删除%d后的数组为:",k);
 31     for(i=0;i<n;i++)
 32     printf("%d ",a[i]);
 33     putchar(10);
 34     return 0;
 35 }

7.循环输入n个数组元素,输入变量k的值,将一个一维数组循环右移k位

  1 #include <stdio.h>                               
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     int n,m,k;
  7     printf("请输入元素的个数为:");
  8     scanf("%d",&n);
  9     int a[n];
 10     int b[n];
 11     int i,j;
 12     for(i=0;i<n;i++)
 13     {
 14         printf("a[%d]=",i);
 15         scanf("%d",&a[i]);
 16         b[i]=a[i];
 17     }
 18     printf("请输入k:");
 19     scanf("%d",&k);
 20     for(j=0;j<k;j++)
 21     {
 22         for(i=0;i<n;i++)
 23         {
 24             m=(i+1)%n;
 25             a[m]=b[i];
 26         }
 27         for(i=0;i<n;i++)
 28         b[i]=a[i];
 29     }
 30     printf("移动了%d位之后的数组排列顺序为:",k);
 31     for(i=0;i<n;i++)
 32     {
 33         printf("%d ",a[i]);
 34     }
 35     putchar(10);
 36    return 0;
 37 }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
设计一个简单的图书管理系统 1.需求分析: 该系统需要实现以下功能: 1.1 图书入库:管理员可以将新的图书添加到系统中,并且需要记录图书的基本信息(如书名、作者、出版社、出版日期、价格等)。 1.2 图书借阅:用户可以通过系统查询图书信息,并借阅自己需要的图书。借阅成功后,需要记录借阅信息(如借阅人、借阅日期、归还日期等)。 1.3 图书归还:用户需要在规定日期内归还借阅的图书,归还成功后需要更新借阅信息。 1.4 图书查询:用户可以通过系统查询图书信息,包括图书基本信息以及借阅情况。 2.类设计: 根据上述需求,我们可以设计以下类: 2.1 Book类:用于记录图书的基本信息。 属性:书名、作者、出版社、出版日期、价格等。 方法:构造方法、toString方法等。 2.2 BorrowRecord类:用于记录图书的借阅信息。 属性:借阅人、借阅日期、归还日期等。 方法:构造方法、toString方法等。 2.3 Library类:用于实现图书管理系统的主要功能。 属性:存储所有图书的列表、存储所有借阅记录的列表等。 方法:添加图书、借阅图书、归还图书、查询图书等。 3.类图: 根据上述类设计,我们可以得到以下类图: ![image-20211214154055436](C:\Users\86186\AppData\Roaming\Typora\typora-user-images\image-20211214154055436.png) 4.代码实现: 4.1 Book类的代码实现: ```java public class Book { private String title; private String author; private String publisher; private String publishDate; private double price; public Book(String title, String author, String publisher, String publishDate, double price) { this.title = title; this.author = author; this.publisher = publisher; this.publishDate = publishDate; this.price = price; } @Override public String toString() { return "Book{" + "title='" + title + '\'' + ", author='" + author + '\'' + ", publisher='" + publisher + '\'' + ", publishDate='" + publishDate + '\'' + ", price=" + price + '}'; } } ``` 4.2 BorrowRecord类的代码实现: ```java public class BorrowRecord { private String borrower; private String borrowDate; private String returnDate; public BorrowRecord(String borrower, String borrowDate, String returnDate) { this.borrower = borrower; this.borrowDate = borrowDate; this.returnDate = returnDate; } @Override public String toString() { return "BorrowRecord{" + "borrower='" + borrower + '\'' + ", borrowDate='" + borrowDate + '\'' + ", returnDate='" + returnDate + '\'' + '}'; } } ``` 4.3 Library类的代码实现: ```java import java.util.ArrayList; public class Library { private ArrayList<Book> books; private ArrayList<BorrowRecord> records; public Library() { books = new ArrayList<>(); records = new ArrayList<>(); } // 添加图书 public void addBook(Book book) { books.add(book); } // 借阅图书 public boolean borrowBook(String title, String borrower, String borrowDate, String returnDate) { for (Book book : books) { if (book.getTitle().equals(title)) { records.add(new BorrowRecord(borrower, borrowDate, returnDate)); return true; } } return false; } // 归还图书 public boolean returnBook(String title) { for (BorrowRecord record : records) { if (record.getTitle().equals(title)) { records.remove(record); return true; } } return false; } // 查询图书 public Book searchBook(String title) { for (Book book : books) { if (book.getTitle().equals(title)) { return book; } } return null; } // 查询借阅记录 public ArrayList<BorrowRecord> searchRecord(String borrower) { ArrayList<BorrowRecord> result = new ArrayList<>(); for (BorrowRecord record : records) { if (record.getBorrower().equals(borrower)) { result.add(record); } } return result; } } ``` 5.测试代码: ```java public class Test { public static void main(String[] args) { Book book1 = new Book("Java基础教程", "张三", "清华出版社", "2021-01-01", 29.9); Book book2 = new Book("Python基础教程", "李四", "人民邮电出版社", "2022-01-01", 39.9); Library library = new Library(); library.addBook(book1); library.addBook(book2); boolean borrowResult = library.borrowBook("Java基础教程", "张三", "2022-01-01", "2022-02-01"); if (borrowResult) { System.out.println("借阅成功!"); } else { System.out.println("借阅失败!"); } boolean returnResult = library.returnBook("Java基础教程"); if (returnResult) { System.out.println("归还成功!"); } else { System.out.println("归还失败!"); } Book searchResult = library.searchBook("Java基础教程"); if (searchResult != null) { System.out.println(searchResult); } else { System.out.println("没有找到该图书!"); } ArrayList<BorrowRecord> recordResult = library.searchRecord("张三"); if (recordResult.size() > 0) { for (BorrowRecord record : recordResult) { System.out.println(record); } } else { System.out.println("没有找到该用户的借阅记录!"); } } } ``` 6.运行结果: ``` 借阅成功! 归还成功! Book{title='Java基础教程', author='张三', publisher='清华出版社', publishDate='2021-01-01', price=29.9} BorrowRecord{borrower='张三', borrowDate='2022-01-01', returnDate='2022-02-01'} ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值