[ create a new paste ] login | about

Link: http://codepad.org/KjloWlzq    [ raw code | output | fork | 2 comments ]

C, pasted on Aug 31:
#include <stdio.h>
#include <conio.h>
 
int  check(int n);
int main()
{
    int i;
    int max = 10000;
 
    printf("cac so co dang 3^k trong khoang [0, %d]:\n", max);
    for(i = 0; i < 10000; i++)
         check(i) ?   printf("%5d", i) : printf("");      
    getch();
    return 0;
}

/* Số có dạng 3^k
3^1 = 3
3^2 = 9
3^3 = 27
3^4 = 81

VD: kiểm tra số 81
81 % 3 = 27 dư 0
27 % 3 = 9 dư 0
9 % 3 = 3 dư 0
3 % 3 = 1 dư 0 => hết

VD: kiểm tra số 12
12 % 3 = 4 dư 0
4 % 3 = 1 dư 1 ==> sai
*/
int check(int n)
{
	int flag=1;
    n = n < 0 ? -n : n;
    if(n < 2)
        return 1;

   /* while(n > 1)
    {
        if(n % 3)  
            return 0;
        n /= 3;
    }
    return 1;*/
	while(n > 1)
	{
		int du = n % 3;
		if(du != 0)  // nếu dư mà khác 0. lập tức báo sai
			flag = 0;
		n /= 3;
	}
	if(flag==1)
      return 1;
    else
      return 0;
}


Output:
1
Line 18: error: conio.h: No such file or directory


Create a new paste based on this one


Comments:
posted by yonnon on Jul 12
#include<bits/stdc++.h>

using namespace std;
int main(){
int n;
cout <<"Enter your sentence: ";
cin >> n;
if( n==1 || n==3){
cout<<"True";
return 0;
}
int number=1;
int temp=0;
for(int i=0; i<=n/2;i++){
//number*=2;
if(pow(3,i)==n){
temp=i;
}

}
if(temp != 0){
cout<<"True";
return 0;

}
else if(temp==0){
cout<<"False";
return 0;
}
cin.ignore();
return 0;

}
reply
posted by cuong.luucb8921 on Aug 28
#include<stdio.h>
#include<stdbool.h>
int main(){
int n;
bool check = false;
do
{
printf("Nhap vap n:\n");
scanf("%d",&n);
if(n < 1)
{
printf("n phai lon hon bang 1\n");
}
}while(n < 1);

int i;

for(i=0;i<=n;i++)
{
if(n==pow(3,i))
{
check = true;
}
}

if(check)
{
printf("Yes");
}
else
{
printf("No");
}
return 0;
}
reply