给定一个数字N。任务是找出由给定数字的二进制表示形式形成的所有对的十进制等价物之和。
null
例如:
输入 :N=4 输出 : 4 相当于4的二进制数是100。 所有可能的对都是10,10,00,它们的十进制等价物分别是2,2,0。 所以,2+2+0=4
输入 :N=11 输出 : 13 所有可能的配对是:10,11,11,01,01,11 总和=2+3+3+1+1+3=13
方法:
- 找到N的二进制等价物并将其存储在向量中。
- 运行两个循环来考虑由向量中存储的二进制等效位组成的每对。
- 找到所有对的十进制等价项,并将它们相加。
- 把钱还给我。
以下是上述方法的实施情况:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to find the sum int sumOfPairs( int n) { // Store the Binary equivalent of decimal // number in reverse order vector< int > v; int sum = 0; // Calculate binary equivalent of decimal number while (n > 0) { v.push_back(n % 2); n = n / 2; } // for correct binary representation reverse(v.begin(), v.end()); // Consider every pair for ( int i = 0; i < v.size() - 1; i++) { for ( int j = i + 1; j < v.size(); j++) { // handles all combinations of 01 if (v[i] == 0 && v[j] == 1) sum += 1; // handles all combinations of 11 if (v[i] == 1 && v[j] == 1) sum += 3; // handles all combinations of 10 if (v[i] == 1 && v[j] == 0) sum += 2; } } return sum; } // Driver code int main() { int N = 5; cout << sumOfPairs(N); return 0; } |
JAVA
// Java implementation of above approach import java.util.*; class GFG { public static int sumOfPairs( int n) { // Store the Binary equivalent // of decimal number in reverse order ArrayList<Integer> v = new ArrayList<Integer>(); int sum = 0 ; // Calculate binary equivalent // of decimal number while (n > 0 ) { v.add(n % 2 ); n = n / 2 ; } Collections.reverse(v); for ( int i = 0 ; i < v.size() - 1 ; i++) { for ( int j = i + 1 ; j < v.size(); j++) { // handles all combinations of 01 if (v.get(i) == 0 && v.get(j) == 1 ) sum += 1 ; // handles all combinations of 11 if (v.get(i) == 1 && v.get(j) == 1 ) sum += 3 ; // handles all combinations of 10 if (v.get(i) == 1 && v.get(j) == 0 ) sum += 2 ; } } return sum; } // Driver Code public static void main (String[] args) { int N = 5 ; System.out.print(sumOfPairs(N)); } } // This code is contributed by Kirti_Mangal |
Python3
# Python3 program to find the sum # Function to find the sum def sumofPairs(n) : # Store the Binary equivalent of decimal # number in reverse order v = [] sum = 0 # Calculate binary equivalent of decimal number while n > 0 : v.append(n % 2 ) n = n / / 2 # for correct binary representation v.reverse() # Consider every pair for i in range ( len (v) - 1 ) : for j in range (i + 1 , len (v)) : # handles all combinations of 01 if v[i] = = 0 and v[j] = = 1 : sum + = 1 # handles all combinations of 11 if v[i] = = 1 and v[j] = = 1 : sum + = 3 # handles all combinations of 10 if v[i] = = 1 and v[j] = = 0 : sum + = 2 return sum # Driver Code if __name__ = = "__main__" : N = 5 # function calling print (sumofPairs(N)) # This code is contributed by ANKITRAI1 |
C#
// C# implementation of above approach using System; using System.Collections.Generic; class GFG { public static int sumOfPairs( int n) { // Store the Binary equivalent // of decimal number in reverse order List< int > v = new List< int >(); int sum = 0; // Calculate binary equivalent // of decimal number while (n > 0) { v.Add(n % 2); n = n / 2; } v.Reverse(); for ( int i = 0; i < v.Count - 1; i++) { for ( int j = i + 1; j < v.Count; j++) { // handles all combinations of 01 if (v[i] == 0 && v[j] == 1) sum += 1; // handles all combinations of 11 if (v[i] == 1 && v[j] == 1) sum += 3; // handles all combinations of 10 if (v[i] == 1 && v[j] == 0) sum += 2; } } return sum; } // Driver Code public static void Main (String[] args) { int N = 5; Console.WriteLine(sumOfPairs(N)); } } /* This code contributed by PrinciRaj1992 */ |
Javascript
<script> // Javascript implementation of above approach // Function to find the sum function sumOfPairs(n) { // Store the Binary equivalent of // decimal number in reverse order var v = []; var sum = 0; // Calculate binary equivalent // of decimal number while (n > 0) { v.push(n % 2); n = parseInt(n / 2); } // For correct binary representation v.reverse(); // Consider every pair for ( var i = 0; i < v.length - 1; i++) { for ( var j = i + 1; j < v.length; j++) { // Handles all combinations of 01 if (v[i] == 0 && v[j] == 1) sum += 1; // Handles all combinations of 11 if (v[i] == 1 && v[j] == 1) sum += 3; // Handles all combinations of 10 if (v[i] == 1 && v[j] == 0) sum += 2; } } return sum; } // Driver code var N = 5; document.write(sumOfPairs(N)); // This code is contributed by rrrtnx </script> |
输出:
6
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END