Bài tập C++ số 1
- Câu 1: Mô phỏng phép nhân tay
- Câu 2: Nhập số tiền, in số tờ, mệnh giá
- Câu 3: Đổi số sang số La Mã
- Câu 4: In cách đọc số
- Câu 5: Giải phương trình bậc haiLời Giải
Câu 1: Mô phỏng phép nhân tay
Lập trình in ra màn hình mô phỏng phép nhân tay 2 số nguyên dương có 3 chữ số nhập từ bàn phím. Ví dụ với 2 số nhập vào là 763 và 589 thì phải in ra màn hình như sau:
Code mẫu:#include<stdio.h>#include<conio.h>void main(){int a,b;char dv,chuc,tram;clrscr();printf("Nhap so bi nhan co 3 chu so a="); scanf("%d",&a);printf("Nhap so nhan co 3 chu so b="); scanf("%d",&b);dv=b%10; chuc=b%100/10; tram=b/100;printf("\nMo phong phep nhan tay\n\n");printf("%20d\n",a);printf("%15c%5d\n",'x',b);printf("%20s\n","-------");printf("%20d\n",a*dv);printf("%19d\n",a*chuc);printf("%18d\n",a*tram);printf("%20s\n","-------");printf("%20ld\n",long(a)*b);getch();}Câu 2: Nhập số tiền, in số tờ, mệnh giá
Một người cần rút một số tiền T từ ngân hàng và muốn tổng số tờ ít nhất. Cho biết có các loại tiền mệnh giá 100, 20, 5 và 1. Nhập từ bàn phím số tiền T và in ra số tờ mỗi loại mệnh giá và tổng số tờ nhận được.Code mẫu:#include <stdio.h>#include <conio.h>void main(){int t,t100,t20,t5;clrscr();printf("Nhap so tien t=");scanf("%d",&t);t100=t/100;t-=100*t100; t20=t/20;t-=20*t20; t5=t/5;t-=5*t5;printf("So to cac loai menh gia la :\n");printf("Loai 100 : %d to\n",t100);printf("Loai 20 : %d to\n",t20);printf("Loai 5 : %d to\n",t5);printf("Loai 1 : %d to\n",t);printf("Tong so to cac loai la : %d\n",t+t5+t20+t100);getch();}Câu 3: Đổi số sang số La Mã
Hãy lập trình nhập 1 số nguyên dương nhỏ hơn 1000 và đổi sang số La mã tương ứng, in kết quả ra màn hình.Code mẫu:#include<conio.h>#include<iostream.h>void main(){int n,dv,ch,tr;clrscr();cout<<"Nhap so nguyen duong <1000 :\n"; cin>>n;cout<<"So "<<n<<" doi sang so La ma la :\n";tr=n/100;n=n%100; ch=n/10;dv=n%10;switch (tr){case 1:cout<<"C";break;case 2:cout<<"CC";break;case 3:cout<<"CCC";break;case 4:cout<<"CD";break;case 5:cout<<"D";break;case 6:cout<<"DC";break;case 7:cout<<"DCC";break;case 8:cout<<"DCCC";break;case 9:cout<<"CM";}switch (ch){case 1:cout<<"X";break;case 2:cout<<"XX";break;case 3:cout<<"XXX";break;case 4:cout<<"XL";break;case 5:cout<<"L";break;case 6:cout<<"LX";break;case 7:cout<<"LXX";break;case 8:cout<<"LXXX";break;case 9:cout<<"XC";}switch (dv){case 1:cout<<"I";break;case 2:cout<<"II";break;case 3:cout<<"III";break;case 4:cout<<"IV";break;case 5:cout<<"V";break;case 6:cout<<"VI";break;case 7:cout<<"VII";break;case 8:cout<<"VIII";break;case 9:cout<<"IX";}getch();}Câu 4: In cách đọc số
In ra màn hình cách đọc một số nguyên dương nhỏ hơn 1000000. Ví dụ số 726503 đọc là: bảy mươi hai vạn sáu ngàn năm trăm linh ba.Code mẫu:#include<iostream.h>#include<conio.h>void main(){ char doc[9][5]={"mot","hai","ba","bon","nam","sau","bay","tam","chin"};int van,ngan,tram,chuc,donvi,chv,dv;long so;clrscr();cout<<"Nhap so nguyen duong <1000000 can doc = ";cin>>so;cout<<"Cach doc so "<<so<<" la:\n";van=so/10000; ngan=so/1000%10; tram=so/100%10; chuc=so/10%10; donvi=so%10;if(van){chv=van/10; dv=van%10;if(chv==1)cout<<"muoi ";else if(chv>1)cout<<doc[chv-1]<<" muoi ";if(dv)cout<<doc[dv-1];cout<<" van ";}if((ngan==0)&&van)cout<<" khong ngan ";else if(ngan)cout<<doc[ngan-1]<<" ngan ";if((tram==0)&&(van||ngan))cout<<" khong tram ";else if(tram)cout<<doc[tram-1]<<" tram ";if((chuc==0)&&donvi&&(van||ngan||tram))cout<<"le ";else if(chuc==1)cout<<" muoi ";else cout<<doc[chuc-1]<<" muoi ";if(donvi)cout<<doc[donvi-1];getch();}Câu 5: Giải phương trình bậc hai
Lập trình giải phương trình ax2 + bx + c = 0, các hệ số thực a, b, c nhập từ bàn phím.Code mẫu:#include<stdio.h>#include<conio.h>#include<math.h>void main(){float a,b,c,d;clrscr();printf("Nhap cac he so a, b, c : "); scanf("%f%f%f",&a,&b,&c);if (a){d=b*b-4*a*c;if (d<0) printf("Vo nghiem !");if (d==0) printf("Nghiem kep x=%4.2f",-0.5*b/a);if (d>0){printf("Hai nghiem phan biet :\n");printf("x1=%4.2f",0.5*(-b-sqrt(d))/a);printf(" x2=%4.2f",0.5*(-b+sqrt(d))/a);}}else if (b) printf("Mot nghiem x=%4.2f",-c/b);else if (c) printf("Vo nghiem !");else printf("Vo so nghiem !");getch();}


0 comments:
Post a Comment