在 C++ 中将数字转换为仅包含数字 3 和 8
在本教程中,我们将讨论一个程序,将数字转换为仅包含数字 3 和 8。
为此,我们将提供一个随机数。我们的任务是通过从该数字中添加/减去 1 或将该数字的数字转换为任何所需的数字,将其数字仅转换为 3 和 8。
示例
#include <bits/stdc++.h>
using namespace std;
//calculating minimum operations required
int cal_min(long long int num){
//calculating remainder and operations
int rem;
int count = 0;
while (num) {
rem = num % 10;
if (!(rem == 3 || rem == 8))
count++;
num /= 10;
}
return count;
}
int main(){
long long int num = 2341974;
cout << "Minimum Operations: " << cal_min(num);
return 0;
}输出
Minimum Operations: 6
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP