Get me outta here!

September 24, 2015

Tổng hợp các bài tập lập trình C (Phần 2)

1. Tính lãi xuất ngân hàng
#include<stdio.h>
#include<conio.h>
void nhaptien(int a[],int n)
{
     int i;
     for(i=0;i<n;i++)
     {
                     printf("thang %d:",i+1);
                     scanf("%d",&a[i]);
     }
}
int main()
{
    int a[100];
    int i,n;
    float lai = 0;
    printf("nhap vao so thang cua tien gui:");
    scanf("%d",&n);
    nhaptien(a,n);
    int tong = a[0];
    printf("\n");
    printf("%d \t\t\t\t %0.3f\n",a[0],tong+lai);
    for(i=1;i<n;i++)
    {
         lai = (tong+lai) * 0.08;
         tong = tong + a[i];
         printf("%d \t\t\t\t %0.3f\n",a[i],tong+lai);
    }
    getch();
    return 1;
}
2. Nhập dữ liệu bằng Struct
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct {
       int sdt;
       char hoten[25];
       float sotien;
}thuebao[100];
void nhap(thuebao a,int n)
{
     int i;
     for(i=0;i<n;i++)
     {
          printf("sdt: ");
          scanf("%d",&a[i].sdt);
          printf("ho ten: ");
          scanf("%s",&a[i].hoten);
          printf("sotien: ");
          scanf("%f",&a[i].sotien);
     }
}
int main()
{
    int i,n;
    thuebao a;
    printf("nhap vao n thue bao: ");
    scanf("%d",&n);
    nhap(a,n);
    for(i=0;i<n;i++)
    printf("%d\t\t\t%s\t\t\t%0.3f\n",a[i].sdt,a[i].hoten,a[i].sotien);
    getch();
    return 1;
}
3. Sắp xếp danh sách thí sinh theo điểm thi
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct {
        int sbd;
        char hoten[25];
        float m1,m2,m3;
        float tong;
        }danhsach[100];
void nhap(danhsach a,int n)
{
     int i;
     for(i=0;i<n;i++)
     {
           printf("sbd: ");
           scanf("%d",&a[i].sbd);
           printf("hoten: ");
           scanf("%s",&a[i].hoten);
           printf("diem m1: ");
           scanf("%f",&a[i].m1);
           printf("diem m2: ");
           scanf("%f",&a[i].m2);
           printf("diem m3: ");
           scanf("%f",&a[i].m3);
           printf("tong %0.3f\n",a[i].m1+a[i].m2+a[i].m3);
     }
}
void sapxep(danhsach a,int n)
{
     int i,j,tam;
     int tong[20];
     for(i=0;i<n;i++)
     {
                     tong[i] = a[i].m1+a[i].m2+a[i].m3;
                     printf("%d\n",tong[i]);
     }
     for(i=0;i<=n;i++)
       for(j=i+1;j<n;j++)
     {
        // tong[i] = a[i].m1+a[i].m2+a[i].m3;
         if(tong[i] < tong[j])
         {
                    tam = tong[i];
                    tong[i] = tong[j];
                    tong[j] = tam;
         }
     }
     for(i=0;i<n;i++)
     printf("%f\n",tong[i]);
}
int main()
{
    int i,n;
    danhsach a;
    printf("nhap vao danh sach thi sinh: ");
    scanf("%d",&n);
    nhap(a,n);
    sapxep(a,n);
    getch();
    return 1;
}
4. Sắp xếp mảng
#include<stdio.h>
#include<conio.h>
void nhap(int a[],int n)
{
     int i;
     for(i=0;i<n;i++)
     {
                     printf("a[%d] = ",i);
                     scanf("%d",&a[i]);
                     }
}
void sapxep(int a[],int n)
{
     int i,j,tam;
     for(i=0;i<=n;i++)
       for(j=i+1;j<n;j++)
     {
                if(a[i] > a[j])
               {
                         tam = a[i];
                         a[i] = a[j];
                         a[j] = tam;
               }
     }
}
int main()
{
    int i,n,a[20];
    printf("nhap vao so phan tu cua mang: ");
    scanf("%d",&n);
    nhap(a,n);
    for(i=0;i<n;i++)
    printf("%5d",a[i]);
    sapxep(a,n);
    printf("\n");
    for(i=0;i<n;i++)
    printf("%5d",a[i]);
    getch();
    return 1;
}


0 comments:

Post a Comment