//Code tim vi tri so may man
//Thuat toan Bac Dang Sieu
//Code by me
//De
/* Lucky Numbers*
Lucky Numbers are only numbers 4 or 7
EX: 4, 7, 44, 47, 74, 77,...
Question: Find location of Lucky number
Ex:
Input: 447
Output: 8 */
#include<stdio.h>
#include<math.h>
//Ham dem chu so cua so n
int countLN(int n)
{
int count=0, temp;
while (n>0)
{
n/=10;
count++;
}
return count;
}
//ham kiem tra so n co phai so may man hay khong?
bool checkLN(int n)
{
bool check = true;
int temp;
while (n>0)
{
temp = n%10;
if (temp!=7 && temp!=4)
{
check = false;
break;
}
n/=10;
}
return check;
}
int main()
{
long n;
int temp, i, location=0;
//control so may man n
do{
printf("Input your Lucky Number: ");
scanf("%ld",&n);
if (checkLN(n)==false)
{
printf("\nSo ban vua nhap khong phai so may man, moi ban nhap lai!\n");
}
} while(checkLN(n)==false);
int count = countLN(n);
int a[count];
//dua tung chu so cua n vao mang
if(checkLN(n)==true)
{
for (i=count-1;i>=0;i--)
{
a[i]=n%10;
n/=10;
}
}
//di den vi tri dau tien cua so
//Vi du 444 co 3 chu so se
//la 1+2+2^n-1 se la 1+2+3=7
for(i=1;i<=count;i++)
{
location+=pow(double(2),i-1);
}
//tim vi tri so may man do
for (i=0;i<count;i++)
{
if (a[i]>4)
{
location+=pow(double(2),count-i-1);
}
}
printf("\nSo may man ban vua nhap o vi tri thu: %d",location);
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.