Task
Cho trước một xâu s, trong xâu này có thể chứa chữ số. Hãy tìm và in ra số nhỏ nhất chứa trong xâu.
Input • Một xâu có độ dài không quá 500 ký tự. Các số có thể chứa trong xâu luôn nhỏ hơn hoặc bằng 1000.
Output • In ra số nhỏ nhất xuất hiện trong xâu.
Samples
input 97 Pellentesque 15 sagittis leo metus.
output 15
Code:
#include <bits/stdc++.h>
#include <string.h>
#include <iostream>
using namespace std;
int Min_Array(int a[] , int n)
{
int min = a[0];
for(int i = 0 ; i < n ; i++)
{
if(min > a[i])
min = a[i];
}
return min;
}
int convert(int c)
{
if(c == 48)
return 0;
if(c == 49)
return 1;
if(c == 50)
return 2;
if(c == 51)
return 3;
if(c == 52)
return 4;
if(c == 53)
return 5;
if(c == 54)
return 6;
if(c == 55)
return 7;
if(c == 56)
return 8;
if(c == 57)
return 9;
}
long long convert_number(int a[],int n)
{
long long sum = 0 , k = n - 1;
for(int i = 0 ; i < n ; i++)
{
sum = sum + a[i]*pow(10,k);
k--;
}
return sum;
}
int main()
{
char *s = new char[501];
gets(s);
int x = 0 , b[100000], c[100000] , count = 0, t = 0, k , j = 0,g=0;
for(int i = 0 ; i < strlen(s) ; i++)
{
if(*(s+i) == '.' || *(s+i) == ',' || *(s+i) == ':' || *(s+i) == ' ')
{
if(t >= 1)
{
if(g >= 1)
{
k = convert_number(c,j);
b[x++] = k;
j = 0;
g = 0;
}
count = 0;
}
}
if(*(s+i) >= 'a' && *(s+i) < 'z' || *(s+i) >= 'A' && *(s+i) <= 'Z' || *(s+i) >= '0' && *(s+i) <= '9')
{
count++;
if(*(s+i) >= '0' && *(s+i) <= '9')
{
c[j++] = convert((int)*(s+i));
g++;
}
if(count == 1)
t++;
}
}
if(*(s+strlen(s)-1) != ',' , *(s+strlen(s)-1) != ':' , *(s+strlen(s)-1) != ':' , *(s+strlen(s)-1) != ' ')
{
if(g >= 1)
{
int f = convert_number(c,j);
b[x++] = f;
}
}
cout<<Min_Array(b,x);
return 0;
}