对于这次的笔试,我只想说BiShi。。。几道编程题加一道SQL题
1.找出所有三位数中的水仙花数
public void getNarcissusNums() {
int g=0,s=0,b=0,sum=0;
for(int i=100;i<=999;i++) {
b=i/100;
s=(i-b*100)/10;
g=i-b*100-s*10;
sum = (int) (Math.pow(b,3)+Math.pow(s,3)+Math.pow(g,3));
if (sum == i) {
System.out.println(i);
}
}
}
2.输入2,5,计算2+22+222+2222+22222
public int caculate(int a, int b) {
/*int sum=0,curr=a;
if(b == 1)
sum = a;
if (b > 1) {
sum += a;
for(int i=1;i<b;i++) {
curr = (int) (a*Math.pow(10, i)+curr);
sum += curr;
}
}
System.out.println(sum);
return sum;*/
// 确实上面那方法太复杂了,明明可以简单就实现
int c = 0, sum = 0;
for (int n = 1; n <= b; n++) {
c = (c* 10) + a;
sum += c;
}
System.out.print("sum=" + sum);
return sum;
}
3.从数组中找出重复元素及其所在位置
public class GetRepeatNums {
public static void main(String[] args) throws Exception {
int[] nums = {12, 18, 19, 15, 26, 29, 49, 15, 12, 19, 29, 12, 18};
// map 的键 为 nums 中的整数,值 为 nums 中整数的位置
Map<Integer, List<Integer>> map = new LinkedHashMap<>(); // LinkedHashMap 可以维护键值对 加入 map 的顺序
for (int i = 0; i < nums.length; i++) {
List<Integer> positions = map.get(nums[i]);
if (positions == null) { // 如果 map 的键 中不存在这个整数
positions = new ArrayList<>(1);
map.put(nums[i], positions); // 将这个整数和与其关联的位置 positions 放入 map
}
positions.add(i);
}
for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
List<Integer> positions = entry.getValue();
if (positions.size() > 1) { // 如果一个整数对应的位置数量大于 1,说明这个整数重复
int num = entry.getKey();
printResult(num, positions);
}
}
}
private static void printResult(int num, List<Integer> positions) {
StringBuilder result = new StringBuilder();
result.append(num).append(' ').append('{');
for (Integer position : positions) {
result.append(position).append(',');
}
result.setCharAt(result.length() - 1, '}'); // 把最后一个 , 替换为 }
System.out.println(result);
}
}
4.打印菱形
public class PrintRhombus {
public static void main(String[] args) {
print(7); // 输出7行的菱形
}
public static void print(int size) {
if (size % 2 == 0) {
size++; // 计算菱形大小
}
for (int i = 0; i < size / 2 + 1; i++) {
for (int j = size / 2 + 1; j > i + 1; j--) {
System.out.print(" "); // 输出左上角位置的空白
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print("*"); // 输出菱形上半部边缘
}
System.out.println(); // 换行
}
for (int i = size / 2 + 1; i < size; i++) {
for (int j = 0; j < i - size / 2; j++) {
System.out.print(" "); // 输出菱形左下角空白
}
for (int j = 0; j < 2 * size - 1 - 2 * i; j++) {
System.out.print("*"); // 输出菱形下半部边缘
}
System.out.println(); // 换行
}
}
}
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)