JD9. 幸运数
描述
输入描述
每组数据输入一个数输出描述
每组数据输出一行,小于等于n的幸运数个数。示例1
输入:
21
输出:
3
C++ 解法, 执行用时: 1ms, 内存消耗: 372KB, 提交时间: 2017-11-16
#include <stdio.h> int f(int i) { int sum = 0; while(i) { sum += i % 10; i /= 10; } return sum; } int g(int x) { int result=0; while(x){ result++; x &= (x - 1); } return result; } int main(void) { int num = 0, sum = 0, i; scanf("%d", &num); for (i = 1; i <= num; i++) { if (f(i) == g(i)) sum++; } printf("%d\n", sum); return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-10-04
#include <stdio.h> int f(int i) { int sum = 0; while(i) { sum += i % 10; i /= 10; } return sum; } int g(int x) { int result=0; while(x){ result++; x &= (x - 1); } return result; } int main(void) { int num = 0, sum = 0, i; scanf("%d", &num); for (i = 1; i <= num; i++) { if (f(i) == g(i)) sum++; } printf("%d\n", sum); return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-09-12
#include <iostream> #include <string> #include <algorithm> #include<functional> #include <vector> #include <deque> #include <sstream> #include <cctype> #include <map> #include <set> using namespace std; int f(int a) { int sum = 0; while (a) { sum = sum + a % 10; a = a / 10; } return sum; } int g(int a) { int count = 0; while (a) { a &= (a-1); count++; } return count; } int main() { int num; while (scanf("%d",&num) != EOF) { int luck_num = 0; while (num) { if (f(num) == g(num)) luck_num++; num--; } cout << luck_num << endl; } return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-09-08
#include<iostream> using namespace std; int main() { int num; cin >> num; int n = 0; for (int i = 1; i <= num; i++) { int f = 0, g = 0; int m1 = i; int m2 = i; while (m1) { f += m1 % 10; m1 = m1 / 10; } while (m2) { g++; m2 = (m2 - 1) & m2; } if (g == f) n++; } cout << n << endl; return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-09-08
//// HuaWeiOJ.cpp : 定义控制台应用程序的入口点。 #include <iostream> #include <string> #include <algorithm> #include<functional> #include <vector> #include <deque> #include <sstream> #include <cctype> #include <map> #include <set> using namespace std; int f(int a) { int sum = 0; while (a) { sum = sum + a % 10; a = a / 10; } return sum; } int g(int a) { int count = 0; while (a) { a &= (a-1); count++; } return count; } int main() { int num; while (scanf("%d",&num) != EOF) { int luck_num = 0; for (int i = 1; i <= num;i++) { if (f(i) == g(i)) luck_num++; } cout << luck_num << endl; } return 0; }