Daily Diaries Of CTK

.

Hãy chọn cho mình một con đường để đi

Nếu bạn không có con đường nào để đi thì cũng không có thành công nào chọn bạn

Hãy để mỗi ngày trôi qua là một ngày ý nghĩa

Luôn cố gắng hết mình vì những ước mơ.

Hãy để ngày hôm nay tốt hơn ngày hôm qua

Bạn sinh ra để làm nên những việc lớn lao.

Hãy tin vào chính bản thân của mình.

Để thực hiện được mơ ước thì bạn phải biết khát khao.

Luôn biết đứng lên sau mỗi lần vấp ngã.

Hạnh phúc không phải là kết quả, đó là quá trình bạn đi tới thành công.

Thứ Sáu, 9 tháng 9, 2016

Thuật toán QuickSort trong danh sách liên kết đơn


#include<iostream>
using namespace std;
int n;
struct Node{
int data;
Node *next;// chua dia chi node ke tiep ma no tro toi
};

struct List{
Node *head;
Node *tail;
};

void Init(List &l){ // k tao List rong
l.head = l.tail = NULL;
}

Node *creatNode(int x ){ //tao thong tin cho node
Node *p = new Node;
if(p == NULL) return NULL;
p->next = NULL;
p->data = x;
return p;
}

bool isEmpty(List l ){ // k tr a xem lieu List co rong hay k.
if(l.head == NULL ) return true;
return false;
}

// chen vao dau List:
void addHead(List &l, int x ){
Node *p = creatNode(x);
if(isEmpty(l)) l.head = l.tail = p;
else{
p->next = l.head; // con tro next cua p tro toi dia chi cua node head(ban dau)
l.head = p; // cap nhat node head( luc sau )
}
}
// ham chen vao cuoi List
void addTail(List &l, int x ){
Node *p = creatNode(x);
if(isEmpty(l)) addHead(l,x);
else{
l.tail->next = p;
l.tail = p;
}
}
// tim kiem
Node *search(List l, int k ){
Node *p = l.head;
while(p!=NULL) {
if(p->data == k ) return p;
else p = p->next;
}
return NULL;
}

void addMid(List &l, int x, int k ){ // chen node co data = x vao sau node co data  = k;
Node *p = search(l,k);
if(p!=NULL){
Node *q= creatNode(x);
Node *r = p->next;
p->next = q;
q->next = r;
}
else cout<<"\nKhong tim thay node co data = k.";
}

void addAtK(List &l, int x, int k ){ // chen vi tri bat ki;
if(isEmpty(l) || k<= 1 ) addHead(l,x);
else if(k>= n) addTail(l,x);
else{
Node *p = creatNode(x);
Node *q = new Node, *w = new Node;
Node *r = l.head;
int dem = 0;
while(r!=NULL){
dem++;
q = r;
if(dem == k ) break;
else r = r->next;
}
w = l.head;
while(w->next!= q) w = w->next;
w->next = p;
p->next = r;
}
}
// nhap vao mot list bat ki
void nhap(List &l){
cout<<"\nnhap so ptu ban dau cua List: "; cin>> n;
for(int i = 1; i<= n; i++ ) addTail(l,i);
}
// xuat thong tin ra man hinh
void xuat(List l ){
Node *p = l.head;
while(p!=NULL){
cout<<" " << p->data;
p = p->next;
}
}

void delHead(List &l ){ // xoa node o dau List
if(!isEmpty(l)){
Node *p = l.head;
l.head = l.head->next; // cap nhat l.head
delete p; // xoa bo node head ban dau
}
}

void delTail(List &l ){
if(!isEmpty(l)){
Node *p = l.head;
Node *q = new Node;
while(p->next != l.tail ) p = p->next; // tim node ngay truoc tail
q = p; // gan node nay cho node q
p = p->next; // p chinh la node tail can xoa
l.tail = q; // cap nhat l.tail
l.tail->next = NULL; // cap nhat node next cho l.tail
delete p;
}
}

void delAtK(List &l, int k ){
if(k<= 1) delHead(l);
else if(k>=n) delTail(l);
else{
int dem = 0;
if(!isEmpty(l)){
Node *p = l.head;
Node *q = new Node;
while(p != NULL){ // tim node thu k.
dem++;
q = p;
if(dem == k ) break; // tim thay thi break
else p= p->next; // k thi tim tiep
}
Node *r = l.head;
while(r->next != q ) r = r->next; // tim node k-1
r->next = q->next; // cho node next cua node k-1 tro toi node k+1;
delete q;
}
}
}

void QuickSort(List &l ){
List l1, l2;
Node *tag, *p;
if(l.head == l.tail ) return;
Init(l1); Init(l2);
tag = l.head;
l.head = l.head->next; // cap nhat lai l.head
tag->next = NULL;// co lap tag
while( l.head!= NULL ){
p = l.head;
l.head = l.head->next;
p->next = NULL;
if(p->data <= tag->data ) addHead(l1,p->data);
else addHead(l2,p->data);
}
QuickSort(l1); // goi de qui sap xep l1, l2
QuickSort(l2);
if(l1.head != NULL ){ // l1 k rong
l.head = l1.head; // lay head cua l1 gan cho head cua l;
l1.tail->next = tag;
} // l1 rong
else l.head = tag;
tag->next = l2.head;
if(l2.head!= NULL ) l.tail = l2.tail;
else l.tail = tag;
}

void menu(){
List l;
Init(l);
nhap(l);
xuat(l);
int k, x, lc;
do{
cout<<"\n______MENU______\n1_Chen dau.\n2_chen cuoi."
<<"\n3_Chen sau vi tri node data = k.\n4_Chen vao vi tri bat ki."
<<"\n5_Xuat Thong tin List.\n6_Xoa phan tu dau List."
<<"\n7_Xoa phan tu o cuoi List.\n8_Xoa node o vi tri k."
<<"\n9_Sap xep QuickSort."
<<"\n0_Thoat.\n_Ban chon ? ";
cin>> lc;
switch(lc){
case 0: break;
case 1: cout<<"\nNhap x: "; cin>> x; addHead(l,x); n++; break;
case 2: cout<<"\nNhap x: "; cin>> x; addTail(l,x); n++; break;
case 3: cout<<"\nNhap x, k: "; cin>> x >> k; addMid(l,x, k);n++; break;
case 4: cout<<"\nNhap x, vi tri k: "; cin>> x >> k; addAtK(l,x,k); n++; break;
case 5: xuat(l); break;
case 6: delHead(l); n--; break;
case 7: delTail(l); n--; break;
case 8: cout<<"\nNhap vi tri k: "; cin>> k; delAtK(l,k); n--; break;
case 9: QuickSort(l); break;
}
}while(lc != 0 );
}

int main(){
menu();
return 0;
}

Thứ Bảy, 3 tháng 9, 2016

Lap trinh game tho va rua tren man hinh console


#include<iostream>
#include<ctime>
#include<cstdlib>
#include<string>

using namespace std;

void delay( float sec ){ // cho nay dung float or int deu dc.
 clock_t be, fin;
 be = clock();
 fin = be;
 while( fin - be <= sec )
  fin = clock();
}

void tho( int &t, int & n, int &toi_dich_t, string &str ){
 n= rand() %10 + 1;
 if( n == 4 ){
  str[t] = '.';
  t += 9;
  if( t >= 70 ) t = 70, toi_dich_t = 1;
  str[t] = 'T';
 }
 if( n == 5 ){
  str[t] = '.';
  t -= 12;
  if( t < 1 ) t = 1;
  str[t] ='T';
 }
 
 if( n == 6 || n == 7 || n == 8 ){
  str[t] = '.';
  t += 1;
  if( t >= 70 ) t = 70, toi_dich_t = 1;
  str[t] = 'T';
 }
 if( n == 9 || n == 10 ){
  str[t] = '.';
  t-= 2;
  if( t < 1 ) t = 1;
  str[t] = 'T';
 }
}

void rua( int &r, int & n, int &toi_dich_r, string &str ){
 n = rand() % 10 + 1;
 if( n <= 5 ){
  str[r] = '.';
  r+= 3;
  if( r >= 70 ) r = 70, toi_dich_r = 1;
  str[r] = 'R';
 }
 if( n == 6 || n == 7 ){
  str[r] = '.';
  r -= 6;
  if( r < 1 ) r= 1;
  str[r] ='R';
 }
 
 if( n == 8 || n == 9 || n == 10 ){
  str[r] = '.';
  r+= 1;
  if( r>= 70 ) r = 70, toi_dich_r = 1;
  str[r] = 'R';
 }
}

void tho_rua(){
 srand( time ( 0 ) );
 int t, r, toi_dich_t, toi_dich_r, n;
 t = r = 1; toi_dich_t = toi_dich_r = 0;
 string str= " "; str+= 'O';
 for( int i = 1; i < 70; i++ ) str += '.';
 cout<<"\t\t\t--------- Bang ----------"<< endl;
 cout<<"\t\t\t------- Ouch!!! -------";
 cout<< endl << str;
 while( toi_dich_t == 0 && toi_dich_r == 0 ){
  rua( r, n, toi_dich_r, str );
  tho( t, n, toi_dich_t, str );
  if( t == r ){
   cout<<"\t\t\t------- Ouch!!! -------";
   str[r] = 'O';
   cout<< endl << str; // in ra vi tri hai ae can nhau.
  }
  if( toi_dich_t == 0 && toi_dich_r == 0 && ( r != t ) ){
   str[t] = 'T'; str[r] = 'R'; // dat lai vi tri rua, tho.
   cout<< endl << str;
  }
  if( toi_dich_t == 1 && toi_dich_r == 1 ){
   cout<< endl << str;
   cout<<"\t\t\t------------ Tho & Rua Dong Hang! ------------";
  }
  if( toi_dich_t == 1 ){
   cout<< endl << str;
   cout<<"\t\t\t-----------Tho Thang! -----------";
  }
  if( toi_dich_r == 1 ){
   cout << endl <<str;
   cout<<"\t\t\t-----------Rua Thang! -----------";
  }
  delay( 100 );
 }
}

int main(){
 tho_rua();
 cout<< endl;
 return 0;
}




Thứ Bảy, 27 tháng 8, 2016

Danh sách liên kết vòng


// Danh sach lien ket don vong.

#include<iostream>
using namespace std;
int n;
struct Node{
int data;
Node *next;// chua dia chi node ke tiep ma no tro toi
};

struct List{
Node *head;
Node *tail;
};

void Init(List &l){ // k tao List rong
l.head = l.tail = NULL;
}

Node *creatNode(int x ){ //tao thong tin cho node
Node *p = new Node;
if(p == NULL) exit(1);
p->next = NULL;
p->data = x;
return p;
}

bool isEmpty(List l ){ // k tr a xem lieu List co rong hay k.
if(l.head == NULL ) return true;
return false;
}

// chen vao dau List:
void addHead(List &l, int x ){
Node *p = creatNode(x);
if(isEmpty(l)) l.head = l.tail = p;
else{
p->next = l.head; // con tro next cua p tro toi dia chi cua node head(ban dau)
l.head = p; // cap nhat node head( luc sau )
}
l.tail->next = l.head; // khep vong don.
}
// ham chen vao cuoi List
void addTail(List &l, int x ){
Node *p = creatNode(x);
if(isEmpty(l)) addHead(l,x);
else{
l.tail->next = p;
l.tail = p;
}
l.tail->next = l.head; // khep vong don
}
// tim kiem
Node *search(List l, int k ){
Node *p = l.head;
do {
if( p->data == k ) return p;
else p = p->next;
} while( p != l.head );
return NULL;
}

void addMid(List &l, int x, int k ){ // chen node co data = x vao sau node co data  = k;
Node *p = search(l,k);
if(p!=NULL){
Node *q= creatNode(x);
Node *r = p->next;
p->next = q;
q->next = r;
}
else cout<<"\nKhong tim thay node co data = k.";
}
/*
void addAtK(List &l, int x, int k ){ // chen vi tri bat ki;
if(isEmpty(l) || k<= 1 ) addHead(l,x);
else if(k>= n) addTail(l,x);
else{
Node *p = creatNode(x);
Node *q = new Node, *w = new Node;
Node *r = l.head;
int dem = 0;
while(r!=NULL){
dem++;
q = r;
if(dem == k ) break;
else r = r->next;
}
w = l.head;
while(w->next!= q) w = w->next;
w->next = p;
p->next = r;
}
} */
// nhap vao mot list bat ki
void nhap(List &l){
cout<<"\nnhap so ptu ban dau cua List: "; cin>> n;
for(int i = 1; i<= n; i++ ) addTail(l,i);
}
// xuat thong tin ra man hinh
void xuat(List l ){
if(l.head){
cout<< "\nDanh sach cac phan tu: \n";
Node *p = l.head;
do{
cout<<" " << p->data;
p = p->next;
}while( p != l.head );
}
else cout<< "\nDanh Sach Rong";
cout<< endl;
}

void delHead(List &l ){ // xoa node o dau List
if(!isEmpty(l)){
if(l.head != l.tail ){
Node *p = l.head;
l.head = l.head->next; // cap nhat l.head
delete p; // xoa bo node head ban dau
l.tail->next = l.head;
}
else l.head = NULL;
}
else return;
}

void delTail(List &l ){
if(!isEmpty(l)){
if(l.head != l.tail ){
Node *p = l.head;
Node *q = new Node;
while(p->next != l.tail ) p = p->next; // tim node ngay truoc tail
q = p; // gan node nay cho node q
p = p->next; // p chinh la node tail can xoa
l.tail = q; // cap nhat l.tail
l.tail->next = l.head;
delete p;
} else l.head = NULL;
}
else return;
}

void delAtK(List &l, int k ){
if( k <= 1) delHead(l);
else if( k >= n ) delTail(l);
else{
int dem = 0;
if(!isEmpty(l)){
Node *p = l.head;
Node *q = new Node;
while(p != NULL){ // tim node thu k.
dem++;
q = p;
if(dem == k ) break; // tim thay thi break
else p= p->next; // k thi tim tiep
}
Node *r = l.head;
while(r->next != q ) r = r->next; // tim node k-1
r->next = q->next; // cho node next cua node k-1 tro toi node k+1;
delete q;
}
}
}

void menu(){
List l;
Init(l);
nhap(l);
xuat(l);
int k, x, lc;
do{
cout<<"\n______MENU______\n1_Chen dau.\n2_chen cuoi."
<<"\n3_Chen sau vi tri node data = k.\n4_Chen vao vi tri bat ki."
<<"\n5_Xuat Thong tin List.\n6_Xoa phan tu dau List."
<<"\n7_Xoa phan tu o cuoi List.\n8_Xoa node o vi tri k."
// <<"\n9_Sap xep QuickSort."
<<"\n0_Thoat.\n_Ban chon ? ";
cin>> lc;
switch(lc){
case 0: break;
case 1: cout<<"\nNhap x: "; cin>> x; addHead(l,x); n++; break;
case 2: cout<<"\nNhap x: "; cin>> x; addTail(l,x); n++; break;
case 3: cout<<"\nNhap x, k: "; cin>> x >> k; addMid(l,x, k);n++; break;
// case 4: cout<<"\nNhap x, vi tri k: "; cin>> x >> k; addAtK(l,x,k); n++; break;
case 5: xuat(l); break;
case 6: delHead(l); n--; break;
case 7: delTail(l); n--; break;
case 8: cout<<"\nNhap vi tri k: "; cin>> k; delAtK(l,k); n--; break;
// case 9: QuickSort(l); break;
}
}while(lc != 0 );
}

int main(){
menu();
return 0;
}

Thứ Sáu, 26 tháng 8, 2016

Tạo queue bằng danh sách liên kết đơn


/*
 * 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.
 */

/*
 * File:   main.cpp
 * Author: KHOA
 *
 * Created on August 26, 2016, 9:10 AM
 */

#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

struct Node {
    int data;
    Node *next;
};

struct List {
    Node *head;
    Node *tail;
};

// khoi tao queue

void Init(List &q) {
    q.head = q.tail = NULL; // gan con tro head va tail cua queue bang NULL
}

// tao Node trong queue

Node *creat_Node(int x) {
    Node *p = new Node;
    if (p != NULL) {
        p->data = x; // dua gia tri cua x vao trong data
        p->next = NULL; // gan phan tu ke tiep bang NULL
        return p; // tra ve con tro p trong queue
    } else {
        return NULL; // neu cap phat khong thanh cong thi tra ve NULL
    }

}

// kiem tra xem queue co rong khong

bool isEmpty(List &q) {
    Node *p = q.head;
    if (p == NULL) return true; // neu phan tu dau bang NULL thi queue rong
    else return false; // neu khong thi khong phai rong
}

// push Node vao trong queue

void push(List &q, int x) {
    Node *r = creat_Node(x);
    if (isEmpty(q)) q.head = q.tail = r;
    else {
        r->next = q.head; // đưa phần tử mới vào trỏ tới next là phần tử đầu
        q.head = r; // cập nhật phần tử mới đưa vào đó là phần tử đầu
    }
}

// pop Node ra khoi queue

void pop(List &q) {
    Node *p = q.head;
    while (p->next->next != NULL) { // tim vi tri phan tu thu 2 tu duoi len
        p = p->next;
    }
    // p la phan tu o vi tri 2 tu duoi len
    Node *r = p->next; // r la phan tu cuoi cung cua queue
    p->next = NULL;
    delete r; // xoa phan tu r
}

// nhap phan tu vao trong queue

void nhap(List &q) {
    int n;
    cout << "Nhap so luong Node trong queue: ";
    cin>>n;
    for (int i = 1; i <= n; i++) {
        int x;
        cout << "Phan tu thu " << i << ": ";
        cin>>x;
        push(q, x); // dua phan tu vao trong queue
    }
}

// xuat cac phan tu ra man hinh

void xuat(List &q) {
    Node *p = q.head;
    while (p != NULL) {
        cout << " " << p->data;
        p = p->next;
    }
}

// tim Node trong queue

Node *search_Node(List &q, int x) {
    Node *p = q.head;
    while (!isEmpty(q)) {
        if (p->data == x) return p; // tra ve con tro p khi tim thay phan tu trong queue
        else p = p->next; // khong thi tim tiep
    }
    return NULL; // neu khong tim duoc tra ve NULL
}

int main() {
    List q;
    Init(q);
    nhap(q);
    xuat(q);
    int lc, x;

    do {
        cout << "\n _ _ _CHUONG TRINH QUEUE_ _ _"
                << "\n 1. Push queue "
                << "\n 2. Pop queue "
                << "\n 3. Tim phan tu trong queue "
                << "\n 0. Thoat ";
        cout << endl;
        cout << "Nhap lua chon: ";
        cin>>lc;
        switch (lc) {
            case 1:
            {
                cout << "Nhap phan tu can push: ";
                cin>>x;
                push(q, x);
                break;
            }
            case 2:
            {
                cout << "Ban da dua 1 phan tu ra khoi queue! ";
                pop(q);
                break;
            }
            case 3:
            {
                cout << "Nhap phan tu can tim kiem: ";
                cin>>x;
                if (search_Node(q, x) != NULL) cout << "Tim thay phan tu trong queue! ";
                else cout << "Khong tim thay phan tu do trong queue! ";
            }
            case 0: break;
        }
        xuat(q);
        cout << "\n";
    } while (lc != 0);

    return 0;
}



Tạo Stack bằng danh sách liên kết đơn


/*
 * 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.
 */

/*
 * File:   khoa.cpp
 * Author: KHOA
 *
 * Created on August 26, 2016, 9:16 AM
 */

#include <iostream>
#include<stdio.h>
#include<stdio.h>
using namespace std;

struct Node {
    int data;
    Node *next;
};

struct List {
    Node *head;
    Node *tail;
};

// khoi tao stack

void Init(List &s) {
    s.head = s.tail = NULL;
}

// kiem tra stack co rong khong

bool isEmpty(List &s) {
    if (s.head == NULL) return true;
    else return false;
}

// tao Node

Node *creat_Node(int x) {
    Node *p = new Node;
    if (p != NULL) {
        p->data = x;
        p->next = NULL;
    } else return NULL;
}

// Push vao stack

void push(List &s, int x) {
    Node *p = creat_Node(x);
    if (isEmpty(s)) {
        s.head = s.tail = p; // neu stack rong thi ta gan p la phan tu dau va cuoi
    } else {
        p->next = s.head; // cho phan tu ke tiep bang phan tu dau
        s.head = p; // cap nhat phan tu dau la phan tu moi them vao
    }
}

// Pop phan tu ra khoi stack

void pop(List &s) {
    Node *p = s.head;
    if (!isEmpty(s)) {
        s.head = p->next;
        delete p;
    } else {
        cout << "\nDanh sach da rong! ";
    }
}

// tim kiem phan tu trong stack

Node *search_Node(List &s, int x) {
    Node *q = s.head;
    while (q != NULL) {
        if (q->data == x) {
            return q;
        } else {
            q = q->next;
        }
    }
    return NULL;
}
// Nhap du lieu cho Stack

void nhap(List &s) {
    int n;
    cout << "nhap so luong phan tu cho stack: ";
    cin>>n;
    for (int i = 1; i <= n; i++) {
        int x;
        cout << "Phan tu " << i << ": ";
        cin>>x;
        push(s, x);
    }
}

// xuat cac phan tu trong stack

void xuat(List &s) {
    Node *p = s.head;
    while (p != NULL) {
        cout << " " << p->data;
        p = p->next;
    }
}

int main() {
    List s;
    Init(s);
    nhap(s);
    xuat(s);
    int x;
    int lc;
    do {
        cout << "\n_ _ _CHUONG TRINH STACK_ _ _";
        cout << endl;
        cout << " 1.Push Stack"
                << "\n 2.Pop Stack"
                << "\n 3.Tim phan tu"
                << "\n 4.Thoat";
        cout << "\nNhap thong tin can kiem tra: ";

        cin>>lc;
        switch (lc) {
            case 1: cout << "Nhap phan tu can Push: ";
                cin>>x;
                push(s, x);
                break;
            case 2: cout << "Pop phan tu trong Stack ";
                pop(s);
                cout << "\n";
                break;
            case 3: cout << "Nhap phan tu can tim kiem: ";
                cin>>x;
                if (search_Node(s, x) != NULL) cout << "Tim thay phan tu!\n";
                else cout << "khong tim thay!\n";
                break;
            case 4: break;
            default: cout << "Ban nhap sai ";
        }
        xuat(s);
        cout << "\n";
    } while (lc != 4);
    return 0;
}

Thứ Ba, 2 tháng 8, 2016

Video tuyệt đẹp về Việt Nam do Bộ Ngoại giao Việt Nam thực hiện



Video tuyệt đẹp về Việt Nma do Bộ Ngoại giao thực hiện

Thứ Hai, 6 tháng 6, 2016

Đếm số lần lặp

Cho trước n số nguyên không âm a1, a2, …, an. Mỗi lần lặp, bạn thay đổi dãy này thành một dãy mới theo cách: phần tử thứ k trong dãy mới bằng trị tuyệt đối của ak –  ak+1. Phần tử cuối cùng sẽ là an – a1. Quá trình lặp sẽ dừng lại khi được một dãy bằng nhau.
Ví dụ với n=4 và bắt đầu với dãy 0  2  5  11 ta sẽ có các lần lặp là:
2  3  6  11
1  3  5  9
2  2  4  8
0  2  4  6
2  2  2  6
0  0  4  4
0  4  0  4
4  4  4  4
Như vậy trong ví dụ này ta sẽ có 8 lần lặp. Hãy viết chương trình các định số lần lặp của một dãy ban đầu

Input

Gồm nhiều bộ test, mỗi bộ test gồm 2 dòng:
  • Dòng 1 ghi số n (2<=n<=20)
  • Dòng 2 ghi n số của dãy ban đầu
Input kết thúc khi n=0

Output

Với mỗi bộ test ghi trên một dòng là số lần lặp theo mẫu dưới đây. Nếu dãy không bằng nhau được sau 1000 lần lặp thì ghi ra dòng “not attained”
#include <iostream>
#include<stdio.h>
using namespace std;

// khai bao ham thuc hien lap
int x1(int n){
    int i, j;
    bool ok;
    long *a=new long[n+5];
    for(i=1; i<=n; i++) cin>>a[i];
    for(j=1; j<=1001; j++){
        ok=true;
        a[n+1] = a[1];
        for(i=1;i<=n;i++){
            a[i] = a[i+1]-a[i];
            if(a[i]<0) a[i] = -a[i];
            if(a[i]!=0) ok = false;
        }
        if(ok) return j-1;
    }
    return 1001;
}
int main()
{
    int T=0;
    int A[30005];
    int n;
    do{
        cout<<"Nhap gia tri cua n: ";
        cin>>n;
        if(n==0) break;
        T++;
        A[T] = x1(n);
    }while(1);
    for(int i=1;i<=T;i++){
        if(A[i]==1001) cout<<"Case "<<i<<" not attained"<<endl;
        else cout<<"Case "<<i<<": "<<A[i]<<" iterations "<<endl;
    }
    return 0;
}