Bài 165 : Hãy tìm giá trị đầu tiên trong mảng một chiều các số nguyên có chữ số đầu tiên là chữ số lẻ.Nếu mảng không tồn tại giá trị như vậy thì hàm sẽ trả về giá trị 0
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
| #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,demd=0,deme=0,demf=0;
printf("Cac gia tri trong mang co chu so dau tien la chu so le la:");
for(int i=0;i<n;i++)
{
if(a[i]>=-9&&a[i]<=9)
{
int b=a[i]%10;
if(b%2!=0)
{
printf("%4d",a[i]);
demb++;
Co=true;
}
}
if(a[i]>=-99&&a[i]<=-10||a[i]>=10&&a[i]<=99)
{
int c=a[i]/10;
if(c%2!=0)
{
printf("%4d",a[i]);
demc++;
Co=true;
}
}
if(a[i]>=-999&&a[i]<=-100||a[i]>=100&&a[i]<=999)
{
int d=a[i]/100;
if(d%2!=0)
{
printf("%4d",a[i]);
demd++;
Co=true;
}
}
if(a[i]>=-9999&&a[i]<=-1000||a[i]>=1000&&a[i]<=9999)
{
int e=a[i]/1000;
if(e%2!=0)
{
printf("%4d",a[i]);
deme++;
Co=true;
}
}
if(a[i]>=-99999&&a[i]<=-10000||a[i]>=10000&&a[i]<=99999)
{
int f=a[i]/10000;
if(f%2!=0)
{
printf("%4d",a[i]);
demf++;
Co=true;
}
}
}
dem=demb+demc+demd+deme+demf;
if(Co==true)
printf("\nTrong mang co ton tai %d gia tri co chu so dau tien la chu so le",dem);
else
printf("\n0");
}
void xulydulieu(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)
{
int b=a[i]%10;
if(b%2!=0)
{
Co=true;
}
}
if(a[i]>=-99&&a[i]<=-10||a[i]>=10&&a[i]<=99)
{
int c=a[i]/10;
if(c%2!=0)
{
Co=true;
}
}
if(a[i]>=-999&&a[i]<=-100||a[i]>=100&&a[i]<=999)
{
int d=a[i]/100;
if(d%2!=0)
{
Co=true;
}
}
if(a[i]>=-9999&&a[i]<=-1000||a[i]>=1000&&a[i]<=9999)
{
int e=a[i]/1000;
if(e%2!=0)
{
Co=true;
}
}
if(a[i]>=-99999&&a[i]<=-10000||a[i]>=10000&&a[i]<=99999)
{
int f=a[i]/10000;
if(f%2!=0)
{
Co=true;
}
}
if(Co==true)
{
Co1=true;
vitri=i;
break;
}
}
if(Co1==true)
printf("\nGia tri dau tien trong mang co chu so dau tien la chu so le 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);
xulydulieu(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