Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

10's Complement (คอมพลีเมนต์ 10) ของเลขทศนิยม


9's Complement และ 10's Complement ใช้ทำให้การดำเนินการคณิตศาสตร์ในระบบดิจิทัลง่ายขึ้น ใช้ Complement Implementation ลดการใช้ Hardware

หา 9's Complement: ลบตัวเลขด้วย (10^n - 1) โดย n = จำนวนหลัก หรือวิธีง่าย: ลบแต่ละหลักจาก 9

10's Complement: หาง่ายหลังหา 9's Complement แล้วบวก 1 หรือใช้สูตรตรง (10^n - number) โดย n = จำนวนหลัก

ตัวอย่างเลข 456:

9's complement: 999 - 456 = 543
10's complement: 543 + 1 = 544


Input: 456 Output: 544

คำอธิบาย

ทางคณิตศาสตร์:

10's complement = 9's complement + 1
10's complement = 10^i - num
โดย i = จำนวนหลักของ num

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    int i = 0, temp, comp, n;
    n = 456;
    temp = n;
    while (temp != 0) {
        i++;
        temp = temp / 10;
    }
    comp = pow(10, i) - n;
    cout << comp;
    return 0;
}