Trang 4 của 5 Đầu tiênĐầu tiên ... 2345 CuốiCuối
Kết quả 31 đến 40 của 49
  1. #31
    Ngày tham gia
    May 2015
    Bài viết
    0
    vì mới học đến bài này nên em đọc bài của các bác mà chả hiểu gì sất. return là gì rồi nhị phân là ntn. @@

  2. #32
    Ngày tham gia
    Feb 2015
    Bài viết
    0
    cuoi cung cũng xong

    <div class="bbcodeblock bbcodequote bbcmspoilerblock">
    <div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>

    show spoiler
    hide spoiler

    </div>
    <div class="quotecontent">
    <div class="bbcm_spoiler_noscript"><blockquote>


    Mã:
    import java.util.arrays;
    import java.util.scanner;
    public class bai1 {
     
        public static void main(string[] args) {
            string[] ds = nhapdanhsach();
            sapxep(ds);
            timkiem(ds);
     
        }
     
        //nhap so-> so>0
        public static int nhapso() {
            scanner input = new scanner(system.in);
            boolean check = true;
            int n = 0;
            while (check) {
                try {
                    n = input.nextint();
                    if (n < 0) {
                        system.out.println("so luong phan tu phai >0, nhap lai!");
                        continue;
                    }
                    check = false;
                } catch (exception e) {
                    system.out.println("chi duoc nhap so nguyen, nhap lai!");
                    input.nextline();
                }
            }
            return n;
        }
     
        //nhap danh sach
            //nhap so luong phan tu
            //nhap ten moi phan tu
        public static string[] nhapdanhsach() {
            system.out.print(" nhap so luong phan tu: ");
            int n = nhapso();
            system.out.println("nhap danh sach:");
            scanner input = new scanner(system.in);
            string[] danhsach = new string[n];
            int i = 0;
            while (i < n) {
                system.out.print(i + 1 + ".");
                danhsach[i] = xoaspace(input.nextline());
                i++;
            }
            return danhsach;
        }
     
        //sap xep(danh sach)
        public static void sapxep(string[] danhsach) {
            arrays.sort(danhsach);
            system.out.println("danh sach da dc sap xep:");
            for (int i = 0; i < danhsach.length; i++) {
                system.out.println((i + 1) + "." + danhsach[i]);
            }
        }
       
        //tim kiem chinh xac ten sv vua nhap & cho bit stt
        public static void timkiem(string[] danhsach) {
            //nhap ten can tim
            system.out.print("nhap ten can tim: ");
            scanner input = new scanner(system.in);
            string ten = xoaspace(input.nextline());
            string str=ten+" khong co trong danh sach!";
           
            //tim kiem nhi phan
            int l = 0;
            int r = danhsach.length;       
            while (l <= r) {
                int m = (l + r) / 2;
                int ss = danhsach[m].compareto(ten);
                if (ss == 0) {
                    str=ten+" co trong danh sach, stt: "+(m+1);               
                    break;
                } else if (ss<0) {
                    l = m + 1;
                } else if (ss >0) {
                    r = m - 1;
                }
            }
            system.out.println(str);
           
        }
     
        //xoa khoang trang du khi nhap
        public static string xoaspace(string str) {
            str = str.replaceall("\\s+", " ");
            str = str.replaceall("^\\s+|\\s+$", "");
            return str;
        }
    }
    </blockquote></div>
    </div>
    </div>

  3. #33
    Ngày tham gia
    Feb 2015
    Bài viết
    0
    Trích dẫn Gửi bởi flysky
    vì mới học đến bài này nên em đọc bài của các bác mà chả hiểu gì sất. return là gì rồi nhị phân là ntn. @@
    hi bạn,

    pp nhị phân là 1 thuật toán để tìm kiếm 1 phần tử 1 cách tối ưu và nhanh nhất tùy theo yêu cầu: http://hus.edu.vn/thcs/bai5.html

    còn return là hàm trả về giá trị của 1 hàm, thường dùng trong if-else, (bác nào rành thì bay vào giải thích giùm mình )
    public static boolean ktra (...)
    {
    .......
    if (...) return true
    else return false
    }

  4. #34
    Ngày tham gia
    Apr 2015
    Bài viết
    9
    hi all,

    mình có đọc 1 số giải thích về return "câu lệnh return cho phép bạn kết thúc việc thực thi của một phương thức trước khi đến cuối phương thức đó. một lí do dùng câu lệnh này là nếu bạn phát hiện ra điều kiện gây lỗi:"

    vậy, các bác có thể giải thích giùm mình sự khác biệt giữa "return và continue" hay không ?

    khi nào dùng hàm return, khi nào dùng continue.

    thanks.

  5. #35
    Ngày tham gia
    Apr 2016
    Bài viết
    0
    Trích dẫn Gửi bởi sky6596
    <div class="bbcodeblock bbcodequote bbcmspoilerblock">
    <div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>

    show spoiler
    hide spoiler

    </div>
    <div class="quotecontent">
    <div class="bbcm_spoiler_noscript"><blockquote>

    public int search(string hoten) {

    int d = 0, c = numberstudent - 1, g = -1;
    while (d <= c) {
    g = (d + c) / 2;
    if (name[g].equals(hoten)) {
    return g + 1;
    }
    if (hoten.compareto(name[g]) > 0) {
    c = g - 1;
    } else {
    d = g + 1;
    }
    }
    return -1;
    }


    </blockquote></div>
    </div>
    </div>
    bài làm của bạn này rất hay, tiếc mỗi cái sai hàm tìm kiếm, do hiểu nhầm về hàm compareto dẫn tới ko tìm ra kết quả.

  6. #36
    Ngày tham gia
    Feb 2015
    Bài viết
    0
    mình code lung tung quá. ai chi mình cách code ngắn gọn, bố trí lại cho gọn gàng dc ko?
    <div class="bbcodeblock bbcodequote bbcmspoilerblock">
    <div class="attribution type"><noscript>nop bai (move your mouse to reveal the content)</noscript>

    nop bai (open)
    nop bai (close)

    </div>
    <div class="quotecontent">
    <div class="bbcm_spoiler_noscript"><blockquote>


    Mã nguồn PHP:
    [color=#000000]
    [/color][color=#ff8000]/* * to change this license header, choose license headers in project properties. * to change this template file, choose tools | templates * and open the template in the editor. */ [/color][color=#0000bb]package bai10continue[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700]; [/color][color=#ff8000]/** * nhập 1 danh sách họ tên sinh viên 1 lớp học vào 1 mảng n phần tử. * sắp xếp theo thứ tự a, b, c ... rồi dùng thuật toán tìm kiếm nhị phân, * tìm kiếm chính xác tên sinh viên vừa nhập và in ra màn hình số thứ tự của sinh viên đó! * @author admin */[/color][color=#007700]public class [/color][color=#0000bb]bai10_1 [/color][color=#007700]{ public [/color][color=#0000bb]bai10_1[/color][color=#007700](){ [/color][color=#0000bb]scanner nhap[/color][color=#007700]=new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhập số sinh viên: "[/color][color=#007700]); [/color][color=#0000bb]int n[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700](); [/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]string arrsv[/color][color=#007700][]=new [/color][color=#0000bb]string[/color][color=#007700][[/color][color=#0000bb]n[/color][color=#007700]]; for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]arrsv[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhập họ tên sinh viên thứ "[/color][color=#007700]+([/color][color=#0000bb]i[/color][color=#007700]+[/color][color=#0000bb]1[/color][color=#007700])+[/color][color=#dd0000]":"[/color][color=#007700]); [/color][color=#0000bb]arrsv[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); } [/color][color=#ff8000]// danh sách sinh viên vừa nhập [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"danh sách sinh viên vừa nhập: "[/color][color=#007700]);{ for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]arrsv[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700](([/color][color=#0000bb]i[/color][color=#007700]+[/color][color=#0000bb]1[/color][color=#007700])+[/color][color=#dd0000]". "[/color][color=#007700]+[/color][color=#0000bb]arrsv[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]); } } [/color][color=#ff8000]// sắp xếp [/color][color=#007700]for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]arrsv[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]-[/color][color=#0000bb]1[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { for ([/color][color=#0000bb]int j [/color][color=#007700]= [/color][color=#0000bb]i[/color][color=#007700]+[/color][color=#0000bb]1[/color][color=#007700]; [/color][color=#0000bb]j [/color][color=#007700]< [/color][color=#0000bb]arrsv[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]j[/color][color=#007700]++) { if([/color][color=#0000bb]arrsv[/color][color=#007700][[/color][color=#0000bb]j[/color][color=#007700]].[/color][color=#0000bb]compareto[/color][color=#007700]([/color][color=#0000bb]arrsv[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]])<[/color][color=#0000bb]0[/color][color=#007700]){ [/color][color=#0000bb]string temp[/color][color=#007700]=[/color][color=#0000bb]arrsv[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]; [/color][color=#0000bb]arrsv[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]=[/color][color=#0000bb]arrsv[/color][color=#007700][[/color][color=#0000bb]j[/color][color=#007700]]; [/color][color=#0000bb]arrsv[/color][color=#007700][[/color][color=#0000bb]j[/color][color=#007700]]=[/color][color=#0000bb]temp[/color][color=#007700]; } } } [/color][color=#ff8000]// danh sách sau khi sắp xếp [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"danh sách sau khi sắp xếp: "[/color][color=#007700]); for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]arrsv[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700](([/color][color=#0000bb]i[/color][color=#007700]+[/color][color=#0000bb]1[/color][color=#007700])+[/color][color=#dd0000]". "[/color][color=#007700]+[/color][color=#0000bb]arrsv[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]); } [/color][color=#ff8000]// tìm kiếm [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhập tên sinh viên cần tìm: "[/color][color=#007700]); [/color][color=#0000bb]string name[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]int left[/color][color=#007700]=[/color][color=#0000bb]0[/color][color=#007700],[/color][color=#0000bb]right[/color][color=#007700]=[/color][color=#0000bb]arrsv[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]-[/color][color=#0000bb]1[/color][color=#007700]; [/color][color=#0000bb]int vt[/color][color=#007700]=[/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]int middle[/color][color=#007700]; do{ [/color][color=#0000bb]middle[/color][color=#007700]=([/color][color=#0000bb]left[/color][color=#007700]+[/color][color=#0000bb]right[/color][color=#007700])/[/color][color=#0000bb]2[/color][color=#007700]; if([/color][color=#0000bb]name[/color][color=#007700].[/color][color=#0000bb]equals[/color][color=#007700]([/color][color=#0000bb]arrsv[/color][color=#007700][[/color][color=#0000bb]middle[/color][color=#007700]])){ [/color][color=#0000bb]vt[/color][color=#007700]=[/color][color=#0000bb]middle[/color][color=#007700]+[/color][color=#0000bb]1[/color][color=#007700]; break; }else if([/color][color=#0000bb]name[/color][color=#007700].[/color][color=#0000bb]compareto[/color][color=#007700]([/color][color=#0000bb]arrsv[/color][color=#007700][[/color][color=#0000bb]middle[/color][color=#007700]])<[/color][color=#0000bb]0[/color][color=#007700]){ [/color][color=#0000bb]right[/color][color=#007700]=[/color][color=#0000bb]middle[/color][color=#007700]-[/color][color=#0000bb]1[/color][color=#007700]; }else [/color][color=#0000bb]left[/color][color=#007700]=[/color][color=#0000bb]middle[/color][color=#007700]+[/color][color=#0000bb]1[/color][color=#007700]; }while([/color][color=#0000bb]left[/color][color=#007700]<=[/color][color=#0000bb]right[/color][color=#007700]); if([/color][color=#0000bb]vt[/color][color=#007700]!=[/color][color=#0000bb]0[/color][color=#007700]){ [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]name[/color][color=#007700]+[/color][color=#dd0000]" ở số thứ tự "[/color][color=#007700]+[/color][color=#0000bb]vt[/color][color=#007700]); }else{ [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]name[/color][color=#007700]+[/color][color=#dd0000]" không có trong danh sách"[/color][color=#007700]); } } public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) { new [/color][color=#0000bb]bai10_1[/color][color=#007700](); }}[/color] 
    </blockquote></div>
    </div>
    </div>

  7. #37
    Ngày tham gia
    Mar 2015
    Bài viết
    0
    <div class="bbcodeblock bbcodequote bbcmspoilerblock">
    <div class="attribution type"><noscript>1 (move your mouse to reveal the content)</noscript>

    1 (open)
    1 (close)

    </div>
    <div class="quotecontent">
    <div class="bbcm_spoiler_noscript"><blockquote>


    Mã nguồn PHP:
    [color=#000000]
    package edu[/color][color=#007700].[/color][color=#0000bb]ducky[/color][color=#007700].[/color][color=#0000bb]basic[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700];public class [/color][color=#0000bb]breakandcontinuedemo [/color][color=#007700]{public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]){[/color][color=#0000bb]scanner in [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]);[/color][color=#0000bb]int n[/color][color=#007700];[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhap so luong sinh vien:"[/color][color=#007700]);[/color][color=#0000bb]n [/color][color=#007700]= [/color][color=#0000bb]integer[/color][color=#007700].[/color][color=#0000bb]parseint[/color][color=#007700]([/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]());[/color][color=#0000bb]string a[/color][color=#007700][] = new [/color][color=#0000bb]string[/color][color=#007700][[/color][color=#0000bb]n[/color][color=#007700]]; [/color][color=#ff8000]//nhap danh sach ho ten[/color][color=#007700]for([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhap ho ten cua sinh vien thu "[/color][color=#007700]+([/color][color=#0000bb]i[/color][color=#007700]+[/color][color=#0000bb]1[/color][color=#007700])+ [/color][color=#dd0000]" :"[/color][color=#007700]);[/color][color=#0000bb]a[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]] = [/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]();} [/color][color=#ff8000]//sap xep danh sach[/color][color=#007700]for([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]length [/color][color=#007700]- [/color][color=#0000bb]1[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++){for([/color][color=#0000bb]int j [/color][color=#007700]= [/color][color=#0000bb]i [/color][color=#007700]+ [/color][color=#0000bb]1[/color][color=#007700]; [/color][color=#0000bb]j [/color][color=#007700]< [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]j[/color][color=#007700]++){if([/color][color=#0000bb]a[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]].[/color][color=#0000bb]compareto[/color][color=#007700]([/color][color=#0000bb]a[/color][color=#007700][[/color][color=#0000bb]j[/color][color=#007700]]) == [/color][color=#0000bb]1[/color][color=#007700]){[/color][color=#0000bb]string temp [/color][color=#007700]= [/color][color=#0000bb]a[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]];[/color][color=#0000bb]a[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]] = [/color][color=#0000bb]a[/color][color=#007700][[/color][color=#0000bb]j[/color][color=#007700]];[/color][color=#0000bb]a[/color][color=#007700][[/color][color=#0000bb]j[/color][color=#007700]] = [/color][color=#0000bb]temp[/color][color=#007700];}}}[/color][color=#ff8000]//in danh sach[/color][color=#007700]for([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++)[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#0000bb]a[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]+[/color][color=#dd0000]" "[/color][color=#007700]);[/color][color=#0000bb]string tencantim[/color][color=#007700];[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhap ten sinh vien can tim:"[/color][color=#007700]);[/color][color=#0000bb]tencantim [/color][color=#007700]= [/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]();[/color][color=#0000bb]label[/color][color=#007700]:for([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++)if([/color][color=#0000bb]tencantim[/color][color=#007700].[/color][color=#0000bb]compareto[/color][color=#007700]([/color][color=#0000bb]a[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]) == [/color][color=#0000bb]0[/color][color=#007700]){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"so thu tu:"[/color][color=#007700]+[/color][color=#0000bb]i[/color][color=#007700]);break [/color][color=#0000bb]label[/color][color=#007700];} [/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700]();}}[/color] 
    </blockquote></div>
    </div>
    </div>

  8. #38
    Ngày tham gia
    Jan 2015
    Bài viết
    0
    chán quá.khóa học này vắng teo oy, không có ai vô bình luận hết

  9. #39
    Ngày tham gia
    Apr 2014
    Bài viết
    0
    ban oi! co sai thuat toan tim kiem thi phai!

    minh nhap vao 3 sinh vien :------ nam an binh------
    tim sinh vien:--------- nam -------- lai ko co

  10. #40
    Ngày tham gia
    Nov 2014
    Bài viết
    0
    có ai làm chuẩn theo yêu cầu rồi ko.mình thấy hầu như các bạn đều làm ko chuẩn theo yêu cầu bài toán.mình thấy tìm kiếm dùng nhị phân thì rắc rối dùng cách khác code nhanh hơn.các bạn xem code thế này dc chưa.


    Mã nguồn PHP:
    [color=#000000]
    package unit10[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700];[/color][color=#ff8000]/* nhập 1 danh sách họ tên sinh viên 1 lớp học vào 1 mảng n phần tử.* sắp xếp theo thứ tự a, b, c ... rồi dùng thuật toán tìm kiếm nhị phân,* tìm kiếm chính xác tên sinh viên vừa nhập và in ra màn hình số thứ tự của sinh viên đó!*/[/color][color=#007700]public class [/color][color=#0000bb]bai1 [/color][color=#007700]{public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) { [/color][color=#0000bb]scanner input [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]); [/color][color=#0000bb]scanner input2 [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]); [/color][color=#0000bb]int n[/color][color=#007700],[/color][color=#0000bb]j[/color][color=#007700]; [/color][color=#0000bb]string v[/color][color=#007700]; [/color][color=#0000bb]int left[/color][color=#007700], [/color][color=#0000bb]right[/color][color=#007700], [/color][color=#0000bb]mid[/color][color=#007700]; [/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]arrsinhvien [/color][color=#007700]= new [/color][color=#0000bb]string[/color][color=#007700][[/color][color=#0000bb]50[/color][color=#007700]]; do{ [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhap so luong sinh vien:"[/color][color=#007700]); [/color][color=#0000bb]n [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700](); if([/color][color=#0000bb]n[/color][color=#007700]<[/color][color=#0000bb]1[/color][color=#007700])[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"ban da nhap sai"[/color][color=#007700]);}while([/color][color=#0000bb]n[/color][color=#007700]<[/color][color=#0000bb]1[/color][color=#007700]); for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]n[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"sinh vien " [/color][color=#007700]+ ([/color][color=#0000bb]i[/color][color=#007700]+[/color][color=#0000bb]1[/color][color=#007700]) + [/color][color=#dd0000]": "[/color][color=#007700]); [/color][color=#0000bb]arrsinhvien[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]] = [/color][color=#0000bb]input2[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); } for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]n[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"sinh vien thu " [/color][color=#007700]+ ([/color][color=#0000bb]i[/color][color=#007700]+[/color][color=#0000bb]1[/color][color=#007700]) + [/color][color=#dd0000]": " [/color][color=#007700]+ [/color][color=#0000bb]arrsinhvien[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]); } [/color][color=#ff8000]// pp chen [/color][color=#007700]for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]1[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]n[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]v [/color][color=#007700]= [/color][color=#0000bb]arrsinhvien[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]; [/color][color=#0000bb]j [/color][color=#007700]= [/color][color=#0000bb]i[/color][color=#007700]; while ( ([/color][color=#0000bb]j[/color][color=#007700]> [/color][color=#0000bb]0[/color][color=#007700]) && ([/color][color=#0000bb]arrsinhvien[/color][color=#007700][[/color][color=#0000bb]j[/color][color=#007700]-[/color][color=#0000bb]1[/color][color=#007700]].[/color][color=#0000bb]compareto[/color][color=#007700]([/color][color=#0000bb]v[/color][color=#007700])> [/color][color=#0000bb]0[/color][color=#007700]) ) { [/color][color=#0000bb]arrsinhvien[/color][color=#007700][[/color][color=#0000bb]j[/color][color=#007700]] = [/color][color=#0000bb]arrsinhvien[/color][color=#007700][[/color][color=#0000bb]j[/color][color=#007700]-[/color][color=#0000bb]1[/color][color=#007700]]; [/color][color=#0000bb]j[/color][color=#007700]--; } [/color][color=#0000bb]arrsinhvien[/color][color=#007700][[/color][color=#0000bb]j[/color][color=#007700]] = [/color][color=#0000bb]v[/color][color=#007700]; } [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"danh sach sinh vien sau khi sap xep"[/color][color=#007700]); for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]n[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"sinh vien thu " [/color][color=#007700]+ ([/color][color=#0000bb]i[/color][color=#007700]+[/color][color=#0000bb]1[/color][color=#007700]) + [/color][color=#dd0000]": " [/color][color=#007700]+ [/color][color=#0000bb]arrsinhvien[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]); } [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhap ten sinh vien can tim "[/color][color=#007700]); [/color][color=#0000bb]string ten [/color][color=#007700]= [/color][color=#0000bb]input2[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]int x[/color][color=#007700]=[/color][color=#0000bb]0[/color][color=#007700]; for([/color][color=#0000bb]int i[/color][color=#007700]=[/color][color=#0000bb]0[/color][color=#007700];[/color][color=#0000bb]i[/color][color=#007700]<[/color][color=#0000bb]n[/color][color=#007700];[/color][color=#0000bb]i[/color][color=#007700]++) { if([/color][color=#0000bb]ten[/color][color=#007700].[/color][color=#0000bb]equals[/color][color=#007700]([/color][color=#0000bb]arrsinhvien[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]))[/color][color=#ff8000]//ten sinh vien trùng với giá trị thứ i+1 [/color][color=#007700]{[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"tim thay ten: "[/color][color=#007700]+[/color][color=#0000bb]ten[/color][color=#007700]+[/color][color=#dd0000]" o vi tri so: "[/color][color=#007700]+([/color][color=#0000bb]i[/color][color=#007700]+[/color][color=#0000bb]1[/color][color=#007700])); [/color][color=#ff8000]//của mảng [/color][color=#0000bb]x[/color][color=#007700]=[/color][color=#0000bb]x[/color][color=#007700]+[/color][color=#0000bb]1[/color][color=#007700];} } if([/color][color=#0000bb]x[/color][color=#007700]>[/color][color=#0000bb]0[/color][color=#007700]) [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]printf[/color][color=#007700]([/color][color=#dd0000]"co %d ten "[/color][color=#007700]+[/color][color=#0000bb]ten[/color][color=#007700]+[/color][color=#dd0000]" trong danh sach"[/color][color=#007700],[/color][color=#0000bb]x[/color][color=#007700]); [/color][color=#ff8000]//có bao nhiêu tên trong danh sách [/color][color=#007700]else [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"ten "[/color][color=#007700]+[/color][color=#0000bb]ten[/color][color=#007700]+[/color][color=#dd0000]" khong co trong danh sach"[/color][color=#007700]); } }[/color] 

Trang 4 của 5 Đầu tiênĐầu tiên ... 2345 CuốiCuối

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •