开科唯识笔试

对于这次的笔试,我只想说BiShi。。。几道编程题加一道SQL题

1.找出所有三位数中的水仙花数

  1. public void getNarcissusNums() {  
  2.     int g=0,s=0,b=0,sum=0;  
  3.     for(int i=100;i<=999;i++) {  
  4.         b=i/100;  
  5.         s=(i-b*100)/10;  
  6.         g=i-b*100-s*10;  
  7.         sum = (int) (Math.pow(b,3)+Math.pow(s,3)+Math.pow(g,3));  
  8.         if (sum == i) {  
  9.             System.out.println(i);  
  10.         }  
  11.     }  
  12. }  

2.输入2,5,计算2+22+222+2222+22222

  1. public int caculate(int a, int b) {  
  2.      /*int sum=0,curr=a; 
  3.      if(b == 1) 
  4.          sum = a; 
  5.      if (b > 1) { 
  6.          sum += a; 
  7.          for(int i=1;i<b;i++) { 
  8.              curr = (int) (a*Math.pow(10, i)+curr); 
  9.              sum += curr; 
  10.          } 
  11.      } 
  12.      System.out.println(sum); 
  13.      return sum;*/  
  14.         // 确实上面那方法太复杂了,明明可以简单就实现  
  15.     int c = 0, sum = 0;  
  16.     for (int n = 1; n <= b; n++) {  
  17.         c = (c* 10) + a;  
  18.         sum += c;  
  19.     }  
  20.     System.out.print("sum=" + sum);  
  21.     return sum;  
  22. }  

3.从数组中找出重复元素及其所在位置

  1. public class GetRepeatNums {  
  2.     public static void main(String[] args) throws Exception {  
  3.         int[] nums = {12181915262949151219291218};  
  4.         // map 的键 为 nums 中的整数,值 为 nums 中整数的位置  
  5.         Map<Integer, List<Integer>> map = new LinkedHashMap<>(); // LinkedHashMap 可以维护键值对 加入 map 的顺序  
  6.   
  7.         for (int i = 0; i < nums.length; i++) {  
  8.             List<Integer> positions = map.get(nums[i]);  
  9.   
  10.             if (positions == null) { // 如果 map 的键 中不存在这个整数  
  11.                 positions = new ArrayList<>(1);  
  12.                 map.put(nums[i], positions); // 将这个整数和与其关联的位置 positions 放入 map  
  13.             }  
  14.   
  15.             positions.add(i);  
  16.         }  
  17.         for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {  
  18.             List<Integer> positions = entry.getValue();  
  19.             if (positions.size() > 1) { // 如果一个整数对应的位置数量大于 1,说明这个整数重复  
  20.                 int num = entry.getKey();  
  21.                 printResult(num, positions);  
  22.             }  
  23.         }  
  24.     }  
  25.   
  26.     private static void printResult(int num, List<Integer> positions) {  
  27.         StringBuilder result = new StringBuilder();  
  28.         result.append(num).append(' ').append('{');  
  29.         for (Integer position : positions) {  
  30.             result.append(position).append(',');  
  31.         }  
  32.         result.setCharAt(result.length() - 1'}'); // 把最后一个 , 替换为 }  
  33.         System.out.println(result);  
  34.     }  
  35.   
  36. }  

4.打印菱形

  1. public class PrintRhombus {  
  2.     public static void main(String[] args) {  
  3.         print(7); // 输出7行的菱形  
  4.     }  
  5.   
  6.     public static void print(int size) {  
  7.         if (size % 2 == 0) {  
  8.             size++; // 计算菱形大小  
  9.         }  
  10.         for (int i = 0; i < size / 2 + 1; i++) {  
  11.             for (int j = size / 2 + 1; j > i + 1; j--) {  
  12.                 System.out.print(" "); // 输出左上角位置的空白  
  13.             }  
  14.             for (int j = 0; j < 2  i + 1; j++) {  
  15.   
  16.                 System.out.print(""); // 输出菱形上半部边缘  
  17.   
  18.             }  
  19.             System.out.println(); // 换行  
  20.         }  
  21.         for (int i = size / 2 + 1; i < size; i++) {  
  22.             for (int j = 0; j < i - size / 2; j++) {  
  23.                 System.out.print(" "); // 输出菱形左下角空白  
  24.             }  
  25.             for (int j = 0; j < 2  size - 1 - 2  i; j++) {  
  26.   
  27.                 System.out.print("*"); // 输出菱形下半部边缘  
  28.   
  29.             }  
  30.             System.out.println(); // 换行  
  31.         }  
  32.     }  
  33.   
  34. }  

5.SQL题

CARD 借书卡:          CNO 卡号,NAME 姓名,CLASS 班级
BOOKS 图书:           BNO 书号,BNAME 书名,AUTHOR 作者,PRICE 单价,QUANTITY 库存册数
BORROW 借书记录:  CNO 借书卡号,BNO 书号,RDATE 还书日期
1、找出借书超过5本的读者,输出借书卡号及所借图书册数
select cno,count() from borrowgroup by cnohaving count()>5;
2、查询当前借了"计算方法"但没有借"计算方法习题集"的读者,输出其借书卡号,并按卡号降序排序输出
select a.cnofrom borrow a,books bwhere a.bno=b.bno and b.bname='计算方法'    
and not exists(select * from borrow aa,books bb 
where aa.bno=bb.bno and bb.bname='计算方法习题集' and aa.cno=a.cno)
order by a.cno desc 
3、将"c01"班同学所借图书的还期都延长一周
update b set rdate=dateadd(day,7,b.rdate)
from card a,borrow b where a.cno=b.cno and a.class='c01' 
4、从books表中删除当前无人借阅的图书记录
delete a from books a where not exists(select * from borrow where bno=a.bno) 
5、如果经常按书名查询图书信息,请建立合适的索引
create index idx_books_bname on books(bname)


posted @ 2018-06-16 11:25  星朝  阅读(1451)  评论(0编辑  收藏  举报