C,
pasted
on Aug 22:
|
#include<stdio.h>
#include<conio.h>
#include<math.h>
/*
Kiểm tra số đối xứng
Định nghĩa : Số đối xứng là số đọc từ trái qua phải hay từ phải qua
trái đều ra kết quả như nhau
VD: ban đâu có số abcdef
=> số đảo ngược là fedcba
nếu 2 số đó bằng nhau => đối xứng
Vd : số 123 => đảo ngược là 321
số 12345 => đảo ngược là 54321
123 % 10 = 3 => ok
123 / 10 = 12
12 % 10 = 2 => ok
12 / 10 = 1
1 % 10 = 1 => ok
1 / 10 = 0 = > end
321 = 3*10^2 + 2*10^1 + 1*10^0
54321 = 5*10^4 + 4*10^3 + 3*10^2 + 2*10^1 + 1*10^0
=> start = 10^(số chữ số - 1)
=> end = 10^0
Ý tưởng: Tách ra từng chữ số và lấy chữ số đó bắt đầu nhân cho 10^(x)
với x là số chữ số - 1 và qua mỗi lần thì x sẽ liên tục giảm 1 đơn vị
Cách 2: số 123 ta tách thành các chữ số 3,2,1
0 * 10 + 3 = 3
3 * 10 + 2 = 32
32 * 10 + 1 = 321
*/
int main()
{
int n;
// Nhập n ( n > 0 )
do
{
printf("\nNhap vao n ( n > 0 ): ");
scanf_s("%d", &n);
if (n <= 0)
{
printf("\nGia tri ban nhap vao khong hop le. Xin vui long nhap lai! ");
}
} while (n <= 0);
int SoChuSo = (int)log10((float)n) + 1;
int SoNghichDao = 0;
int themang = n;
printf("\nSo %d co phai la so doi xung hay khong ?\n", n);
while (themang != 0)
{
int ChuSo = themang % 10; // Lấy chữ số ra
themang /= 10; // Bỏ chữ số vừa lấy ra
SoNghichDao += ChuSo * pow(10.0, --SoChuSo);
}
/*while(themang != 0)
{
SoNghichDao = SoNghichDao * 10 + themang % 10;
themang /= 10;
}*/
if(SoNghichDao == n)
{
printf("Dung !");
}
else
{
printf("Sai !");
}
getch();
return 0;
}
|
Output:
|
Line 17: error: conio.h: No such file or directory
|
|
Create a new paste based on this one
Comments:
posted by
lethi200397
on Oct 20
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i = 1, D, N, K = 0;
printf(" nhap vao n = ");
scanf("%d", &n);
N = n;
do
{
D = N % 10;
N = N / 10;
K = K*10 + D;
i++;
}
while( N != 0);
if ( K == n)
{
printf(" n la so doi xung ");
}
else
{
printf(" n khong phai la so doi xung ");
}
printf(" K = %d ", K);
getch();
return 0;
}
reply
posted by
phamduyphuong
on Feb 3
Giới thiệu với các bạn cách khác tìm số đối xứng
Tổng trước = Tổng sau
Tích trước = Tích sau
Số đầu = số cuối
Vd: số 12521
Điều kiện để số 12521 là số đối xứng:
1+2 = 2+1
1*2 = 2*1
1 = 1
Hoặc số 2222 thì
2+2=2+2
2*2=2*2
2=2
Và đây là code của mình:
#include <stdio.h>
void s59n(int n){
int temp =n;
int change;
int i=0;
int key=0;
int count=0;
int totalaf=0; // tổng sau
int totalbe=0; // tổng trước
int multipaf=1; //tích sau
int multipbe=1; //tích trước
int end= n%10; // số cuối
int first; // số đầu
while(n/10>0){
n/=10;
++count;
}
first=n;
while(i<(count+1)/2){
totalaf+=(temp%10);
multipaf*=(temp%10);
temp/=10;
++i;
}
change=temp;
while(change/10>0){
change/=10;
++key;
}
if(key==(count+1)/2){
while(temp/10>0){
temp/=10;
totalbe+=(temp%10);
multipbe*=(temp%10);
}
}
else{
totalbe=temp%10;
multipbe=totalbe;
while(temp/10>0){
temp/=10;
totalbe+=(temp%10);
multipbe*=(temp%10);
}
}
if(totalaf==totalbe && multipaf==multipbe && end==first) //điều kiện số đối xứng
printf("Yes");
else
printf("No");
}
int main() {
int n;
scanf("%d",&n);
s59n(n);
return 0;
}
reply
posted by
yonnon
on Jul 9
vậy là bạn chưa tính tới trường hợp số đó là số kiểu như 1235213 rồi =)) 1+2+3=2+1+3 nhưng nó không phải là số đối xứng nha :v
reply
posted by
yonnon
on Jul 9
Bạn thử lấy mấy số như là 1234553241 hay 5467884765 sẽ thấy code bạn bị sai ngay
reply
posted by
dangtiendat86@gmail.com
on Feb 11
Theo mình thấy kiểm tra số đối xứng là đảo nó lại và kiểm tra xem nó có bằng số ban đầu hay không?
Code:
#include<iostream>
#define ll long long
using namespace std;
main(){
ll n;
cout<<"n="; cin>>n;
ll m=n, p=0;
while (m!=0){
p=p*10+m%10;
m/=10;
}
if (n==p) cout<<"true"<<endl;
else cout<<"false"<<endl;
}
reply
posted by
o0otaivipoko0o
on Mar 20
bool ktra_doixung(SONGUYEN sn)
{
for (int i = 0; i < sn.n / 2; i++)
if (sn.a[i] = sn.a[sn.n - 1 - i])
return true;
return false;
}
case 4:
nhapsonguyen(a);
cout << "day so nguyen cua ban la: ";
xuatsonguyen(a);
if (ktra_doixung(a))
cout << "----------day so doi xung----------" << endl;
else
cout << "----------day so ko doi xung----------" << endl;
cout << endl;
break;
reply
posted by
Ngoctruyen99
on Apr 25
code cua em ngon nay, may code tren xem dau dau qua
int main(){
int n, C, reverse;
do{
printf("Nhap n: ");
scanf("%d", &n);
if(n <= 0)
printf("Nhap n > 0!\n");
}while(n <= 0);
i = 0;
C = n;
reverse = 0;
while(C > 0){
reverse = reverse * 10 + (C % 10);
C /= 10;
}
printf("So dao nguoc la: %d\n", reverse);
if(reverse == n)
printf("%d la so doi xung!\n",n);
else
printf("%d khong phai la so doi xung!\n",n);
reply
posted by
yonnon
on Jul 17
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cout<<"Enter your sentence: ";
cin >> n;
int reverse=0;
int temp;
temp=n;
while(temp>0){
reverse=reverse*10 + temp%10;
temp/=10;
}
if(reverse==n){
cout<<"Day la so doi xung"<<
endl;
}
else{
cout<<"Day khong phai so doi xung"<<
endl;
}
return 0;
}
reply
posted by
cuong.luucb8921
on Aug 27
#include<stdio.h>
int main(){
int n;
int t;
int sodaonguoc=0;
do
{
printf("Nhap vap n:\n");
scanf("%d",&n);
if(n < 1)
{
printf("n phai lon hon bang 1\n");
}
}while(n < 1);
t=n;
while(t!=0)
{
sodaonguoc = sodaonguoc*10 + t%10;
t=t/10;
}
if(sodaonguoc==n)
{
printf("%d la so doi xung",n);
}
else
{
printf("%d khong la so doi xung",n);
}
return 0;
}
reply
posted by
20223768
on Sep 12
#include <iostream>
using namespace std;
int main()
{
int n; cin >>n;
int s=1;
int sobandau=n;
int sodaonguoc=0;
while (n!=0)
{
s=n%10;
sodaonguoc=sodaonguoc*10+s;
n=n/10;
}
if (sodaonguoc==sobandau) cout <<"so da cho la so doi xung";
else cout <<"so da cho khong phai la so doi xung";
return 0;
}
reply