Bài 164 : Cho mảng một chiều các số nguyên hãy viết hàm tìm giá trị đầu tiên trong mảng thỏa tính chất số gánh.(ví dụ:12321)
Tải code về máy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
| #include<stdio.h>
#include<conio.h>
#define MAX 100
#define bool
void nhapmang(int a[MAX],int &n)
{
do{
printf("Nhap vao so phan tu cua mang:n=");
scanf("%d",&n);
if(n<1||n>MAX)
printf("So ban nhap vao khong hop le!Xin vui long nhap lai!\n");
else
break;
}while(n<1||n>MAX);
for(int i=0;i<n;i++)
{
printf("Nhap vao a[%d]=",i);
scanf("%d",&a[i]);
}
}
void xuatmang(int a[MAX],int n)
{
printf("\n>>>>>>>>>>>>MANG VUA NHAP LA:<<<<<<<<<<<<<<<\n");
for(int i=0;i<n;i++)
{
printf("%4d",a[i]);
}
printf("\n");
}
void lietke(int a[MAX],int n)
{
int Co;
bool Co=false;
int dem,demb=0,demc=0,deme=0,demj=0,demo=0;
printf("Cac gia tri trong mang thoa tinh chat so ganh la:");
for(int i=0;i<n;i++)
{
if(a[i]>=-9&&a[i]<=9)
{
printf("%4d",a[i]);
demb++;
Co=true;
}
if(a[i]>=10&&a[i]<=99)
{
int c=a[i]/10;
int d=a[i]%10;
if(c==d)
{
printf("%4d",a[i]);
demc++;
Co=true;
}
}
if(a[i]>=100&&a[i]<=999)
{
int e=a[i]/100;
int g=a[i]%10;
if(e==g)
{
printf("%4d",a[i]);
deme++;
Co=true;
}
}
if(a[i]>=1000&&a[i]<=9999)
{
int j=a[i]/1000;
int k=a[i]%10;
int l=a[i]/10;
int m=l%10;
int z=a[i]/100;
int t=z%10;
if(j==k&&m==t)
{
printf("%4d",a[i]);
demj++;
Co=true;
}
}
if(a[i]>=10000&&a[i]<=99999)
{
int o=a[i]/10000;
int p=a[i]%10;
int q=a[i]/10;
int r=q%10;
int s=a[i]/1000;
int u=s%10;
if(o==p&&r==u)
{
printf("%4d",a[i]);
demo++;
Co=true;
}
}
}
dem=demb+demc+deme+demj+demo;
if(Co==true)
printf("\nTrong mang co %d gia tri thoa man yeu cau de bai",dem);
else
printf("\nKhong co gia tri nao trong mang thoa man yeu cau de bai");
}
void dautien(int a[MAX],int n)
{
int Co,vitri=0,i,Co1;
bool Co=false;
bool Co1=false;
for(i=0;i<n;i++)
{
if(a[i]>=-9&&a[i]<=9)
{
Co=true;
}
if(a[i]>=10&&a[i]<=99)
{
int c=a[i]/10;
int d=a[i]%10;
if(c==d)
{
Co=true;
}
}
if(a[i]>=100&&a[i]<=999)
{
int e=a[i]/100;
int g=a[i]%10;
if(e==g)
{
Co=true;
}
}
if(a[i]>=1000&&a[i]<=9999)
{
int j=a[i]/1000;
int k=a[i]%10;
int l=a[i]/10;
int m=l%10;
int z=a[i]/100;
int t=z%10;
if(j==k&&m==t)
{
Co=true;
}
}
if(a[i]>=10000&&a[i]<=99999)
{
int o=a[i]/10000;
int p=a[i]%10;
int q=a[i]/10;
int r=q%10;
int s=a[i]/1000;
int u=s%10;
if(o==p&&r==u)
{
Co=true;
}
}
if(Co==true)
{
Co1=true;
vitri=i;
break;
}
}
if(Co1==true)
printf("\nSo dau tien trong mang thoa tinh chat so ganh la:%d tai vi tri:%d",a[i],vitri);
}
void main()
{
int a[MAX],n,NAMSON;
quaylai:nhapmang(a,n);
xuatmang(a,n);
lietke(a,n);
dautien(a,n);
printf("\nBan co muon tiep tuc chay chuong trinh khong ? Neu co bam phim C,nguoc lai bam bat ky phim nao khac de ket thuc\n");
NAMSON=getch();
if(NAMSON=='c'||NAMSON=='C')
goto quaylai;
}
|
Nhận xét
Đăng nhận xét