Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 54

TÍNH TỔNG 2 SỐ NGUYÊN LỚN DƯƠNG

#include<bits/stdc++.h>
using namespace std;

string Sum(string a, string b)


{
while (a.size()<b.size()) a="0" + a;
while (a.size()>b.size()) b="0" + b;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
string result="";

int du=0;
int tong=0;
for (int i=0;i<a.size();i++) {
tong=(a[i]-'0')+(b[i]-'0')+du;
du=tong/10;
result+= (char)('0'+tong%10);
}
if (du) result+="1";
reverse(result.begin(),result.end());
return result;
}
int main()
{
string a,b;
getline(cin,a); getline(cin,b);
string c=Sum(a,b);a
cout << c;
}
NHÂN 2 SỐ NGUYÊN LỚN DƯƠNG
#include<bits/stdc++.h>
using namespace std;
string Nhan2Bignum(string a, string b) {
string c="";
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int lenA=a.size(); int lenB=b.size();
for (int i=0;i<lenA+lenB;i++) c=c+"0";
for (int iB=0;iB<lenB;iB++) {
int iA;
int nho=0;
for (iA=0;iA<lenA;iA++) {
int x=(b[iB]-'0')*(a[iA] '0')+nho+(c[iA+iB]-'0');
c[iA+iB]=x%10+'0';
nho=x/10;
}
if (nho>0) c[iA+iB]=nho+'0';
}
reverse(c.begin(),c.end());
while (c[0]=='0')c.erase(0,1);
return c;
}
int main()
{
string a,b;
getline(cin,a); getline(cin,b);
string d=Nhan2Bignum(a,b);
cout << d;
}
BÀI TÌM XÂU CON CHUNG LỚN NHẤT
#include<bits/stdc++.h>
using namespace std;

string A,B;
(Hàm bên dưới dùng để đếm số ký tự chung của 2 xâu)
bool check(string Z) {
int id1=0, id2=0;
for (int i=0;i<A.size();i++) {
if (A[i]==Z[id1]) id1++;
if (id1>=Z.size()) break;
}
for (int i=0;i<B.size();i++) {
if (B[i]==Z[id2]) id2++;
if (id2>=Z.size()) break;
}
return (id1==Z.size()&&id1==id2);
}
//Tìm xâu lớn hơn sau mỗi lần tìm được 1 sâu result
string Max(string A,string B) {
while (A.size()>0&&A[0]=='0') A.erase(0,1);
while (B.size()>0&&B[0]=='0') B.erase(0,1);
if (A.size()>B.size()) return A;
if (A.size()<B.size()) return B;
return max(A,B);
}

void solve() {
int lenA=A.size();
int lenB=B.size();
int minLen=min(lenA,lenB);
string Result="";
string base="";
if (minLen==lenA) base=A; (Xét xem xâu ngắn hơn là xâu nào)
else base=B;
(xét dãy nhị phân 1 là lấy 0 là không lấy)
for (int i=0;i<(1<<(minLen));i++) {
int temp=i;
string Z=""; (xâu trung gian)
for (int j=0;j<minLen;j++) {
if (temp%2==1) Z+=base[j]; //chú ý chữ j
//thêm ký tự vào Z
(số lẻ bit cuối luôn là 1)
temp/=2; (chia 2 là dịch bit sang phải)
(ví dụ 101/2=10;10/2=1)
}
if (check(Z)) Result = Max(Result, Z); (tìm xâu lớn nhất)
}
if (Result =="") cout << -1;
else cout << Result;
}

int main()
{
getline(cin,A);
getline(cin,B);
solve();
}
BÀI TOÁN CHIA KẸO, CHIA NHÓM LỆCH MIN
#include<bits/stdc++.h>
using namespace std;

int Vet[21](lưu vết hiện tại), n, a[21], VetMin[21](lưu vết min);

long long solve() {


int Min=1e9;
for (int i=0;i<(1<<(n));i++) {
int temp=i;
int Sum1=0, Sum2=0;
for (int j=0;j<n;j++) {
Vet[j]=temp%2;
temp=temp/2;
}
for (int j=0;j<n;j++) {
if (Vet[j]==0) Sum1=Sum1+a[j];
else Sum2=Sum2+a[j];
}
if (Min>abs(Sum1-Sum2)) {
Min=abs(Sum1-Sum2);
for (int j=0;j<n;j++)
VetMin[j]=Vet[j];(Truy vết)
}
}
return Min;
}

int main()
{
cin >> n;
for (int i=0;i<n;i++)
cin >> a[i];
cout << solve() << endl;
}
ĐẾM ĐỘ DÀI XÂU CON CHUNG DÀI NHẤT
#include <iostream>
#include <string.h>
using namespace std;
#define size 101
//Sử dụng Quy hoạch động
int xauConChungDaiNhat(char s1[], char s2[])
{
int L[size][size];
int m = strlen(s1);
int n = strlen(s2);
for (int i = 0; i <= m; i++)
L[i][0] = 0;
for (int j = 0; j <= n; j++)
L[0][j] = 0;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
if (s1[i - 1] == s2[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
return L[m][n];
}
int main()
{
char s1[size], s2[size];
cin>>s1>>s2;
cout<<xauConChungDaiNhat(s1, s2);
return 0;
}
BÀI TOÁN DỊCH BIT XOAY VÒNG K LẦN

#include <iostream>
#include <bitset>
#include <string>
using namespace std;

int main()
{
int n, k;
cin>>n>>k;
bitset<32> mybits(n); //hàm gọi số giữ bit
bitset<32> mybits1=mybits>>(32-k);
bitset<32> mybits2=mybits<<k;
mybits=mybits1|mybits2;
int ab = mybits.to_ulong(); //hàm chuyển bit sang số nguyên
cout<<ab<<endl;
}
BÀI TOÁN CHIA KẸO MIN
#include<bits/stdc++.h>
using namespace std;
#define size 100
//ý tưởng lấy tổng các phần tử chia 2 rồi lập bảng xét từ tổng/2
xuống xem số nào có thể được tạo thành.
int n, T, S, result, a[51], L[1000000];
int main()
{
cin >> n;
for (int i=1;i<=n;i++) {
cin >> a[i];
T=T+a[i];
}
S=T/2;
L[0]=1;
for (int i=1;i<=T/2;i++)
L[i]=0;
for (int i=1;i<=n;i++)
for (int j=S;j>=0;j--) {
if (L[j]==0 && L[j-a[i]]==1 && j>=a[i])
L[j]=1;
}
for (int i=S;i>=0;i--)
if (L[i]==1) {
result=i;
break;
}
cout << (T-2*result);
}
BÀI TOÁN KIỂM TRA VỊ TRÍ MÃ CÓ THỂ ĐI ĐƯỢC
#include<iostream>
using namespace std;

int a, b, c, d;
int solve() {
int co=0;
int vitri;
int B[8]={-2,-2,-1,1,2,2,1,-1};
int C[8]={1,-1,-2,-2,-1,1,2,2};
for (int k=0;k<8;k++)
{
if ( (a+B[k] == c) && (b+C[k]== d))
{
co=1;
vitri=k;
break;
}
}
if (co)
return vitri+1;
return 0;
}

int main ()
{
cin >> a >> b >> c >> d ;
int gt = solve();
cout << gt ;
}
BÀI TOÁN TÌM TỔNG LỚN NHẤT KHI CHỌN CÁC SỐ ĐÃ CHO VÀ CHIA HẾT CHO K
#include<bits/stdc++.h>
using namespace std;

int n, k, Tong=0, TongMax=-2e7, a[26];

long long solve() {


for (long long i=0;i<(1<<(n));i++) {
long long temp=i;
for (int j=0;j<n;j++) {
if (temp%2==1) Tong=Tong+a[j]; //chú ý chữ j
temp=temp/2;
}
if (Tong%k==0 && Tong>TongMax)
TongMax=Tong;
Tong=0;
}
return TongMax;
}

int main()
{
cin >> n >> k;
for (int i=0;i<n;i++) cin >> a[i];
cout << solve();
}
BÀI TOÁN TÌM CHỈ SỐ N VỚI F[N]<=M VÀ CHO TRƯỚC F0, F1, F2
#include<bits/stdc++.h>
using namespace std;
int m;
int solve() {
long long F0=2,F1=4,F2=6,FNew, i=2;
while (true) {
i++;
FNew=2*F0+4*F1+6*F2;
if (FNew==m) {
return i;
}
if (FNew>m) {
break;
}
F0=F1;
F1=F2;
F2=FNew;
}
return i-1;
}

int main()
{
cin >> m;
if (m<4) cout << 0;
else if (m<6) cout << 1;
else if (m<56) cout <<2;
else cout << solve();
}
BÀI TOÁN TÌM XÂU ĐỐI XỨNG DÀI NHẤT TRONG CHUỖI CHO TRƯỚC
Ý tưởng: tìm chuỗi con chung dài nhất của chuỗi đã cho với đối xứng của nó
#include <iostream>
#include <string.h>
using namespace std;
#define size 101
//Sử dụng quy hoạch động
int xauConChungDaiNhat(char s1[], char s2[])
{
int L[size][size];
int m = strlen(s1);
int n = strlen(s2);
for (int i = 0; i <= m; i++)
L[i][0] = 0;
for (int j = 0; j <= n; j++)
L[0][j] = 0;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
if (s1[i - 1] == s2[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
return L[m][n];
}
int main()
{
char s1[size], s2[size];
cin>>s1; strcpy(s2,s1); strrev(s2)//s2 sẽ mang chuỗi đảo ngược;
cout<<xauConChungDaiNhat(s1, s2);
}
BÀI TOÁN BIGNUM
#include <bits/stdc++.h>
using namespace std;
const int base = 1000000000; const int base_digits = 9;
struct bigint {
vector<int> a; int sign;

bigint() :
sign(1) {
}

bigint(long long v) {
*this = v;
}

bigint(const string &s) {


read(s);
}

void operator=(const bigint &v) {


sign = v.sign;
a = v.a;
}

void operator=(long long v) {


sign = 1;
if (v < 0)
sign = -1, v = -v;
for (; v > 0; v = v / base)
a.push_back(v % base);
}

bigint operator+(const bigint &v) const {


if (sign == v.sign) {
bigint res = v;

for (int i = 0, carry = 0; i < (int) max(a.size(),


v.a.size()) || carry; ++i) {
if (i == (int) res.a.size())
res.a.push_back(0);
res.a[i] += carry + (i < (int) a.size() ? a[i] : 0);
carry = res.a[i] >= base;
if (carry)
res.a[i] -= base;
}
return res;
}
return *this - (-v);
}

bigint operator-(const bigint &v) const {


if (sign == v.sign) {
if (abs() >= v.abs()) {
bigint res = *this;
for (int i = 0, carry = 0; i < (int) v.a.size() ||
carry; ++i) {
res.a[i] -= carry + (i < (int) v.a.size() ?
v.a[i] : 0);
carry = res.a[i] < 0;
if (carry)
res.a[i] += base;
}
res.trim();
return res;
}
return -(v - *this);
}
return *this + (-v);
}

void operator*=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = 0, carry = 0; i < (int) a.size() || carry; ++i)
{
if (i == (int) a.size())
a.push_back(0);
long long cur = a[i] * (long long) v + carry;
carry = (int) (cur / base);
a[i] = (int) (cur % base);
//asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur),
"c"(base));
}
trim();
}

bigint operator*(int v) const {


bigint res = *this;
res *= v;
return res;
}
friend pair<bigint, bigint> divmod(const bigint &a1, const
bigint &b1) {
int norm = base / (b1.a.back() + 1);
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.a.resize(a.a.size());

for (int i = a.a.size() - 1; i >= 0; i--) {


r *= base;
r += a.a[i];
int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
int s2 = r.a.size() <= b.a.size() - 1 ? 0 :
r.a[b.a.size() - 1];
int d = ((long long) base * s1 + s2) / b.a.back();
r -= b * d;
while (r < 0)
r += b, --d;
q.a[i] = d;
}

q.sign = a1.sign * b1.sign;


r.sign = a1.sign;
q.trim();
r.trim();
return make_pair(q, r / norm);
}

bigint operator/(const bigint &v) const {


return divmod(*this, v).first;
}
bigint operator%(const bigint &v) const {
return divmod(*this, v).second;
}

void operator/=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = (int) a.size() - 1, rem = 0; i >= 0; --i) {
long long cur = a[i] + rem * (long long) base;
a[i] = (int) (cur / v);
rem = (int) (cur % v);
}
trim();
}

bigint operator/(int v) const {


bigint res = *this;
res /= v;
return res;
}

int operator%(int v) const {


if (v < 0)
v = -v;
int m = 0;
for (int i = a.size() - 1; i >= 0; --i)
m = (a[i] + m * (long long) base) % v;
return m * sign;
}
void operator+=(const bigint &v) {
*this = *this + v;
}
void operator-=(const bigint &v) {
*this = *this - v;
}
void operator*=(const bigint &v) {
*this = *this * v;
}
void operator/=(const bigint &v) {
*this = *this / v;
}

bool operator<(const bigint &v) const {


if (sign != v.sign)
return sign < v.sign;
if (a.size() != v.a.size())
return a.size() * sign < v.a.size() * v.sign;
for (int i = a.size() - 1; i >= 0; i--)
if (a[i] != v.a[i])
return a[i] * sign < v.a[i] * sign;
return false;
}

bool operator>(const bigint &v) const {


return v < *this;
}
bool operator<=(const bigint &v) const {
return !(v < *this);
}
bool operator>=(const bigint &v) const {
return !(*this < v);
}
bool operator==(const bigint &v) const {
return !(*this < v) && !(v < *this);
}
bool operator!=(const bigint &v) const {
return *this < v || v < *this;
}

void trim() {
while (!a.empty() && !a.back())
a.pop_back();
if (a.empty())
sign = 1;
}

bool isZero() const {


return a.empty() || (a.size() == 1 && !a[0]);
}

bigint operator-() const {


bigint res = *this;
res.sign = -sign;
return res;
}

bigint abs() const {


bigint res = *this;
res.sign *= res.sign;
return res;
}

long long longValue() const {


long long res = 0;
for (int i = a.size() - 1; i >= 0; i--)
res = res * base + a[i];
return res * sign;
}

friend bigint gcd(const bigint &a, const bigint &b) {


return b.isZero() ? a : gcd(b, a % b);
}
friend bigint lcm(const bigint &a, const bigint &b) {
return a / gcd(a, b) * b;
}

void read(const string &s) {


sign = 1;
a.clear();
int pos = 0;
while (pos < (int) s.size() && (s[pos] == '-' || s[pos] ==
'+')) {
if (s[pos] == '-')
sign = -sign;
++pos;
}
for (int i = s.size() - 1; i >= pos; i -= base_digits) {
int x = 0;
for (int j = max(pos, i - base_digits + 1); j <= i; j++)
x = x * 10 + s[j] - '0';
a.push_back(x);
}
trim();
}

friend istream& operator>>(istream &stream, bigint &v) {


string s;
stream >> s;
v.read(s);
return stream;
}

friend ostream& operator<<(ostream &stream, const bigint &v) {


if (v.sign == -1)
stream << '-';
stream << (v.a.empty() ? 0 : v.a.back());
for (int i = (int) v.a.size() - 2; i >= 0; --i)
stream << setw(base_digits) << setfill('0') << v.a[i];
return stream;
}

static vector<int> convert_base(const vector<int> &a, int


old_digits, int new_digits) {
vector<long long> p(max(old_digits, new_digits) + 1);
p[0] = 1;
for (int i = 1; i < (int) p.size(); i++)
p[i] = p[i - 1] * 10;
vector<int> res;
long long cur = 0;
int cur_digits = 0;
for (int i = 0; i < (int) a.size(); i++) {
cur += a[i] * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits) {
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int) cur);
while (!res.empty() && !res.back())
res.pop_back();
return res;
}

typedef vector<long long> vll;

static vll karatsubaMultiply(const vll &a, const vll &b) {


int n = a.size();
vll res(n + n);
if (n <= 32) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res[i + j] += a[i] * b[j];
return res;
}

int k = n >> 1;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end());

vll a1b1 = karatsubaMultiply(a1, b1);


vll a2b2 = karatsubaMultiply(a2, b2);

for (int i = 0; i < k; i++)


a2[i] += a1[i];
for (int i = 0; i < k; i++)
b2[i] += b1[i];

vll r = karatsubaMultiply(a2, b2);


for (int i = 0; i < (int) a1b1.size(); i++)
r[i] -= a1b1[i];
for (int i = 0; i < (int) a2b2.size(); i++)
r[i] -= a2b2[i];

for (int i = 0; i < (int) r.size(); i++)


res[i + k] += r[i];
for (int i = 0; i < (int) a1b1.size(); i++)
res[i] += a1b1[i];
for (int i = 0; i < (int) a2b2.size(); i++)
res[i + n] += a2b2[i];
return res;
}

bigint operator*(const bigint &v) const {


vector<int> a6 = convert_base(this->a, base_digits, 6);
vector<int> b6 = convert_base(v.a, base_digits, 6);
vll a(a6.begin(), a6.end());
vll b(b6.begin(), b6.end());
while (a.size() < b.size())
a.push_back(0);
while (b.size() < a.size())
b.push_back(0);
while (a.size() & (a.size() - 1))
a.push_back(0), b.push_back(0);
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = 0, carry = 0; i < (int) c.size(); i++) {
long long cur = c[i] + carry;
res.a.push_back((int) (cur % 1000000));
carry = (int) (cur / 1000000);
}
res.a = convert_base(res.a, 6, base_digits);
res.trim();
return res;
}
};

int main(){
bigint n1, n2;
cin >> n1 >> n2;
cout << "Tong 2 so = " << (n1 + n2) << '\n';
}
GIAI THUA
#include<bits/stdc++.h>
using namespace std;

long long int giaithua(int n)


{
if(n==1)
return 1;
return n*giaithua(n-1);
}

int main()
{
int n;
cin>>n;
if(n==0)
{
cout<<1;
return 0;
}
long long int kq = giaithua(n);
cout<<kq;
}
CHINH HOP C n k
#include<bits/stdc++.h>
using namespace std;

long long int giaithua(int n)


{
if(n==1)
return 1;
return n*giaithua(n-1);
}

int main()
{
int n;
cin>>n;
int k;
cin>>k;
if(n==k)
{
cout<<1;
return 0;
}
long long int kq = giaithua(n)/(giaithua(k)*giaithua(n-k));
cout<<kq;
}
TONG CHUOI KI TU
#include<bits/stdc++.h>
using namespace std;

string A;

void solve(){
int len = A.size();
int i = 0;
int tong = 0;
int so = 0;
while(i<=len)
{
if(A[i]>='0'&&A[i]<='9')
so = so * 10 +(A[i]-'0');
else
{
tong = tong + so;
so = 0;
}
i++;
}
cout<<tong;
}

int main()
{
getline(cin,A);
solve();
}
PHAN TU CO SO LAN XUAT HIEN NHIEU NHAT TRONG MANG
#include<bits/stdc++.h>
using namespace std;

int A[2000];
int n;

void solve(){
int max=0;
int dem=1;
int kq = A[0];
for(int i=0;i<n;i++)
{
if(A[i]==A[i+1])
{
dem++;
if(dem>max)
{
max=dem;
kq=A[i];
}

}
else
{

dem=1;
}
}
cout<<kq;
}

void sapxeptangdan(){
int tg;
for(int i = 0; i < n - 1; i++){
for(int j = i + 1; j < n; j++){
if(A[i] > A[j]){
tg = A[i];
A[i] = A[j];
A[j] = tg;
}
}
}
}

int main()
{
cin>>n;
for(int i = 0;i<n;i++) cin>>A[i];
sapxeptangdan();
solve();
}
LIET KE TAT CA HOAN VI CUA DAY NHI PHAN
#include<bits/stdc++.h>
using namespace std;

int A[2000];
int n;
void xuat()
{
for(int i = 0;i<n;i++)
cout<<A[i];
cout<<endl;
}
void solve(int k){
if(k==n)
xuat();
else{
for(int i = 0;i<=1;i++)
{
A[k]=i;
solve(k+1);
}
}
}

int main()
{
cin>>n;
solve(0);
}
LIET KE TAT CA HOAN VI
#include<bits/stdc++.h>
using namespace std;
int A[2000],B[2000],n;
void xuat()
{
for(int i =0;i<n;i++)
cout<<A[i]<<" ";
cout<<endl;
}
void lietkeHV(int k){
if(k==n)
xuat();
else
{
for(int i = 0;i<n;i++)
if(B[i]==0)
{
A[k]=i;
B[i]=1;
lietkeHV(k+1);
B[i]=0;
}
}
}
int main(){
cin>>n;
lietkeHV(0);
return 0;
}
TIM DUONG DI QUA CAC THANH PHO
#include<bits/stdc++.h>
using namespace std;
int A[2000],B[2000],n,dem;
int C[200][200];
int chiphitoiuu=INT_MAX;

void tinhchiphi(){
int chiphi = 0;
for(int i = 0;i<n;i++)
chiphi = chiphi + C[A[i]][A[i+1]];
chiphi=chiphi+C[A[n-1]][A[0]];
if(chiphi<chiphitoiuu)
{
chiphitoiuu=chiphi;
}
}
void nhap()
{
cin>>n;
for(int i=0;i<n;i++)
for(int j = 0;j<n;j++)
cin>>C[i][j];
}
void xuat()
{
dem++;
for(int i = 0;i<n;i++)
cout<<A[i];
cout<<endl;
}
void solve(int k)
{
if(k==n)
tinhchiphi();
else
{
for(int i = 0;i<n;i++)
{
if(B[i]==0)
{
A[k]=i;
B[i]=1;
solve(k+1);
B[i]=0;
}
}
}
}

int main()
{
dem = 0;
nhap();
solve(0);
cout<<chiphitoiuu;
return 0;
}
DONG NGOAC MO NGOAC DO DAI M DO SAU N
#include<bits/stdc++.h>
using namespace std;
int m,n;
int A[100];
int tinhDoSau();
void lietkeNP(int k);
void xuat()
{
for(int i = 0 ;i<m;i++)
{
if(A[i]==0)
cout<<'(';
else
cout<<')';
}
cout<<endl;
}
int main(){
cin>>m>>n;
lietkeNP(0);
return 0;
}
void lietkeNP(int k)
{
if(k==m){
if(tinhDoSau()==n)
xuat();
}
else
{
for(int i =0;i<=1;i++){
A[k]=i;
lietkeNP(k+1);
}
}
}
int tinhDoSau()
{
int i = 0;
int ngoacmo = 0;
int dosau = 0;
while(i<m)
{
if(A[i]==0)
ngoacmo++;
else
{
if(ngoacmo==0)
return -1; //khong hop le
if(ngoacmo>dosau)
dosau=ngoacmo;
ngoacmo--;
}
i++;
}
if(ngoacmo==0)
return dosau;
else
return -1;
}
CHUYEN TU HE 16 SANG HE 10
#include<iostream>
#include<cstring>
#include<math.h>
using namespace std;
int chuyenTuHe16SangHe10(char A[]);
void nhap(char A[]);
void xuat(int t);
int main()
{
char A[100];
nhap(A);
int t = chuyenTuHe16SangHe10(A);
xuat(t);
}
int chuyenTuHe16SangHe10(char A[])
{
int tam = 0;
int he10 = 0;
int luythua = strlen(A);
luythua--;
for(int i = 0; A[i]!='\0';i++)
{
if(A[i]<='9'&&A[i]>='0')
tam = A[i] - 48;
if(A[i]<='f'&&A[i]>='a')
tam = A[i] - 87;
if(A[i]<='F'&&A[i]>='A')
tam = A[i] - 55;
he10 += tam*pow(16,luythua);
luythua--;
}
return he10;
}
void nhap(char A[])
{
gets(A);
}
void xuat(int t)
{
cout<<t;
}
CHUYEN TU THAP PHAN SANG HE 2, 8, 16
#include<iostream>
#include<math.h>
#include<cstring>
using namespace std;
long long chuyenSangHe2(int n);
int chuyenSangHe8(int n);
char chuyenSangHe16(int n);
void nhap(int &n);
void xuat(int n,long long he2,int he8,int he16);
int main()
{
int n;
nhap(n);
long long he2 = chuyenSangHe2(n);
int he8 = chuyenSangHe8(n);
char he16 = chuyenSangHe16(n);
xuat(n,he2,he8,he16);
}
long long chuyenSangHe2(int n)
{
long long tong = 0;
long long i = 1;
while(n>0)
{
long long sodu=n%2;
tong=tong + (i*sodu);
n=n/2;
i=i*10;
}
return tong;
}
int chuyenSangHe8(int n)
{
int somu = 0;
int tong = 0;
while(n>0)
{
tong += (n%8)*pow(10,somu);
somu++;
n /= 8;
}
return tong;
}
char chuyenSangHe16(int n)
{
int soxuatra = n;
char he16[100];
int i = 0;
while(n>0)
{
int tam = 0;
tam = n % 16;
if(tam<10)
{
he16[i] = tam + 48;
i++;
}
else
{
he16[i] = tam + 55;
i++;
}
n /= 16;
}
cout<<"So "<<soxuatra<<" o he 16 la: ";
for (int j = i - 1; j >= 0; j--)
cout << he16[j];
}
void nhap(int &n)
{
cin>>n;
}
void xuat(int n,long long he2,int he8,int he16)
{
cout<<endl<<"So "<<n<<" o he 2 la: "<<he2<<endl;
cout<<"So "<<n<<" o he 8 la: "<<he8<<endl;
}
COUNTBIT
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
int doiHe2SangHe10(char S[]);
void doiHe10SangHe2(char S[],int n);
void nhap(int A[],int &n);
int chr(int n);
int main()
{
int A[1100],n,m=0;
char S[100];
nhap(A,n);
for (int i=0;i<n;i++)
{
doiHe10SangHe2(S,A[i]);
for (int j=0;j<strlen(S);j++)
if (j%2==0)
S[j]='1';
m=max(m,doiHe2SangHe10(S));
}
cout<<m;
return 0;
}
void doiHe10SangHe2(char S[],int n)
{
int i=0;
while (n!=0)
{
S[i]=chr(n%2);
n/=2;
i++;
}
S[i]='\0';
//strrev(S);
}
int doiHe2SangHe10(char S[])
{
int s=0;
for (int i=0;i<strlen(S);i++)
s+=pow(2,i)*(S[i]-'0');
return s;
}
int chr(int n)
{
if (n<10)
return n+'0';
return n+55;
}
void nhap(int A[],int &n)
{
cin>>n;
for (int i=0;i<n;i++)
cin>>A[i];
}
CHUAN HOA CHUOI
#include<bits/stdc++.h>

using namespace std;

int main()
{
string s;
getline(cin, s);

string result = "";

for (int i=0;i<s.size();i++){


if ('a'<=s[i] && s[i]<='z')
continue;
else if ('A'<=s[i] && s[i]<='Z')
continue;
s[i]=' ';
}

for (int i=0;i<s.size();i++){


if ('a'<=s[i] && s[i]<='z')
result+=s[i];
else if ('A'<=s[i] && s[i]<='Z')
result += (char)(s[i]+32);
else if (s[i]==' '){
if (result.size()==0)
continue;
else if (result[result.size()-1]!=' ')
result+=s[i];
}
}

for (int i=0;i<result.size();i++)


{
if (i==0)
result[i] = (char)(result[i]-32);
else if (result[i-1]==' ')
result[i]=(char)(result[i]-32);
}

if (result[result.size()-1]==' ')
result[result.size()-1]='\0';
cout<<result;
}
ASCII Table

Dec = Decimal Value

Char = Character

'5' has the int value 53

if we write '5'-'0' it evaluates to 53-48, or the int 5

if we write char c = 'B'+32; then c stores 'b'

Dec Char Dec Char Dec Char Dec Char

--------- --------- --------- ----------

0 NUL (null) 32 SPACE 64 @ 96 `

1 SOH (start of heading) 33 ! 65 A 97 a

2 STX (start of text) 34 " 66 B 98 b

3 ETX (end of text) 35 # 67 C 99 c

4 EOT (end of transmission) 36 $ 68 D 100 d

5 ENQ (enquiry) 37 % 69 E 101 e

6 ACK (acknowledge) 38 & 70 F 102 f

7 BEL (bell) 39 ' 71 G 103 g

8 BS (backspace) 40 ( 72 H 104 h

9 TAB (horizontal tab) 41 ) 73 I 105 i

10 LF (NL line feed, new line) 42 * 74 J 106 j

11 VT (vertical tab) 43 + 75 K 107 k

12 FF (NP form feed, new page) 44 , 76 L 108 l

13 CR (carriage return) 45 - 77 M 109 m

14 SO (shift out) 46 . 78 N 110 n

15 SI (shift in) 47 / 79 O 111 o

16 DLE (data link escape) 48 0 80 P 112 p

17 DC1 (device control 1) 49 1 81 Q 113 q

18 DC2 (device control 2) 50 2 82 R 114 r

19 DC3 (device control 3) 51 3 83 S 115 s

20 DC4 (device control 4) 52 4 84 T 116 t

21 NAK (negative acknowledge) 53 5 85 U 117 u

22 SYN (synchronous idle) 54 6 86 V 118 v

23 ETB (end of trans. block) 55 7 87 W 119 w

24 CAN (cancel) 56 8 88 X 120 x

25 EM (end of medium) 57 9 89 Y 121 y

26 SUB (substitute) 58 : 90 Z 122 z

27 ESC (escape) 59 ; 91 [ 123 {

28 FS (file separator) 60 < 92 \ 124 |

29 GS (group separator) 61 = 93 ] 125 }

30 RS (record separator) 62 > 94 ^ 126 ~

31 US (unit separator) 63 ? 95 _ 127 DEL

You might also like