UVA - 11645 Bits

Description

Download as PDF

Problem J
Bits
Input:
Standard Input

Output: Standard Output

 

A bit is a binary digit, taking a logical value of either "1" or "0" (also referred to as "true" or "false" respectively).  And every decimal number has a binary representation which is actually a series of bits. If a bit of a number is “1” and it's next bit is also “1” then we can say that the number has a 1 adjacent bit. And you have to find out how many times this scenario occurs for all numbers up to N.

 

Examples:

            Number           Binary                         Adjacent Bits

            12                    1100                            1

            15                    1111                            3

            27                    11011                          2

 

Input

For each test case, you are given an integer number (0 <= N <= ((2^63)-2)), as described in the statement. The last test case is followed by a negative integer in a line by itself, denoting the end of input file.

 

Output

For every test case, print a line of the form “Case X: Y”, where X is the serial of output (starting from 1) and Y is the cumulative summation of all adjacent bits from 0 to N.

Sample Input                             Output for Sample Input

0

6

15

20

21

22

-1

Case 1: 0

Case 2: 2

Case 3: 12

Case 4: 13

Case 5: 13

Case 6: 14

 题意:统计0-n中每个数存在两个连续的1的总个数。

思路:和的思路是一样的,也是枚举两个1的位置。 注意当11出现的位置和原数一样的时候,还要考虑+1或者+2的情况,还有就是用两个较大的数来储存结果。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

long long aa,bb;

void cal(long long n) {
	bb += n;
	if (bb >= (1000000000000ll)) {
		aa += bb / (1000000000000ll);
		bb %= (1000000000000ll);
	}
}

int main() {
	int cas = 1;
	long long n;
	long long a,b,c,m;
	while (cin >> n && n >= 0) {
		aa = bb = 0;
		m = 1, a = n;
		for (int i = 0; i < 62; i++) {
			cal((n>>2)*m);
			if ((n & 3) == 3)
				cal((a&((1ll<<i)-1))+1);
			m <<= 1;
			n >>= 1;
		}
		printf("Case %d: ", cas++);
		if(aa) {
			cout << aa;
			printf("%012lld\n",bb);
		}
		else cout<<bb<<endl;
	}
	return 0;
}




### 洛谷 UVA12096 题解及代码实现 洛谷 UVA12096 是一道涉及字符串处理和简单逻辑判断的题目。以下是对该题目的解析以及代码实现。 #### 题目解析 题目要求对输入的一系列字符串进行处理,判断是否存在特定字符,并根据条件输出最小值或特殊标记。在解析过程中,需要特别注意字符串中字符的位置以及可能的特殊情况[^3]。 #### 代码实现 以下是基于 C++ 的代码实现,代码中包含了必要的逻辑判断和边界处理: ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int x; cin >> x; if (!x) exit(0); // 如果输入为 0,则退出程序 string s; cin >> s; int r = -1, d = -1, ans = INT_MAX; for (int i = 0; i < x; i++) { if (s[i] == 'R') r = i; // 记录字符 'R' 的位置 else if (s[i] == 'D') d = i; // 记录字符 'D' 的位置 else if (s[i] == 'Z') { // 如果遇到字符 'Z' ans = 0; // 立即更新答案为 0 break; } if (r != -1 && d != -1) // 如果 'R' 和 'D' 都已出现 ans = min(ans, abs(r - d)); // 更新最小距离 } cout << ans << '\n'; // 输出结果 } int main() { while (1) solve(); // 循环处理多个测试用例 return 0; } ``` #### 注意事项 - 在代码中使用了 `INT_MAX` 来初始化变量 `ans`,确保初始值足够大以正确计算最小值[^3]。 - 特殊字符 `'Z'` 的存在会直接将答案置为 `0`,因此需要优先判断并处理[^3]。 - 对于边界情况(如字符串中不存在 `'R'` 或 `'D'`),代码能够正确处理并返回合理的答案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值