【题意string】#78 A. Help Far Away Kingdom

A. Help Far Away Kingdom
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In a far away kingdom lived the King, the Prince, the Shoemaker, the Dressmaker and many other citizens. They lived happily until great trouble came into the Kingdom. The ACMers settled there.

Most damage those strange creatures inflicted upon the kingdom was that they loved high precision numbers. As a result, the Kingdom healers had already had three appointments with the merchants who were asked to sell, say, exactly 0.273549107 beer barrels. To deal with the problem somehow, the King issued an order obliging rounding up all numbers to the closest integer to simplify calculations. Specifically, the order went like this:

  • If a number's integer part does not end with digit 9 and its fractional part is strictly less than 0.5, then the rounded up number coincides with the number’s integer part.
  • If a number's integer part does not end with digit 9 and its fractional part is not less than 0.5, the rounded up number is obtained if we add 1 to the last digit of the number’s integer part.
  • If the number’s integer part ends with digit 9, to round up the numbers one should go to Vasilisa the Wise. In the whole Kingdom she is the only one who can perform the tricky operation of carrying into the next position.

Merchants found the algorithm very sophisticated and they asked you (the ACMers) to help them. Can you write a program that would perform the rounding according to the King’s order?

Input

The first line contains a single number to round up — the integer part (a non-empty set of decimal digits that do not start with 0 — with the exception of a case when the set consists of a single digit — in this case 0 can go first), then follows character «.» (a dot), and then follows the fractional part (any non-empty set of decimal digits). The number's length does not exceed 1000 characters, including the dot. There are no other characters in the input data.

Output

If the last number of the integer part is not equal to 9, print the rounded-up number without leading zeroes. Otherwise, print the message "GOTO Vasilisa." (without the quotes).

Sample test(s)
input
0.0
output
0
input
1.49
output
1
input
1.50
output
2
input
2.71828182845904523536
output
3
input
3.14159265358979323846
output
3
input
12345678901234567890.1
output
12345678901234567890
input
123456789123456789.999
output
GOTO Vasilisa.


这是一个把浮点数四舍五入成整数的程序,但是如果需要进位的同时整数部分的末位是0,则输出 GOTO Vasilisa。

所以相当于一个翻译题,翻译题意即可……

使用string来存储可以省下不少麻烦。

解题:找到点在哪里,然后点的后一位和前一位决定了全局,后一位决定进不进,进位的话前一位决定输出还是GOTO

Code:

#include <cstdio>
#include <string>
#include <cstring> 
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
// http://codeforces.com/contest/99
// Help Far Away Kingdom
int main()
{
	string s;	cin>>s;
	string ans="";
	int flag=0;
	for(int i=0;i<s.length();i++)
	{
		if(s[i]!='.' && !flag) ans+=s[i];
		else if(s[i]=='.' && !flag) flag=1;
		else 
		{
			int lst=ans[i-2]-'0';
			int num=s[i]-'0';
			if(num<5) break;
			else if(lst!=9) ans[i-2]=ans[i-2]+1;
			else 
			{
				cout<<"GOTO Vasilisa.";
				return 0;	
			}
			break;
		}
	}
	cout<<ans;
	return 0;
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是 C++ 实现,使用了深度优先搜索(DFS)。 ```cpp #include <iostream> #include <vector> #include <cstring> using namespace std; const int N = 110; int n, m; char g[N][N]; bool st[N][N]; int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; bool has_solution, has_multiple; vector<pair<int, int>> unknown; bool check(int x, int y) { if (x < 0 || x >= n || y < 0 || y >= m) return false; if (g[x][y] == '#') return false; return true; } void dfs(int x, int y) { st[x][y] = true; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (check(nx, ny) && !st[nx][ny]) dfs(nx, ny); } } bool is_four_connected() { memset(st, false, sizeof st); int sx = -1, sy = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (g[i][j] == '.') { if (sx == -1) { sx = i, sy = j; dfs(sx, sy); } else if (!st[i][j]) { return false; } } } } return true; } void solve(int u) { if (has_solution) { has_multiple = true; return; } if (u == unknown.size()) { if (is_four_connected()) { has_solution = true; } return; } int x = unknown[u].first, y = unknown[u].second; g[x][y] = '#'; solve(u + 1); if (has_multiple) return; g[x][y] = '.'; solve(u + 1); if (has_multiple) return; g[x][y] = '?'; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> g[i][j]; if (g[i][j] == '?') unknown.push_back({i, j}); } } solve(0); if (!has_solution) cout << "Impossible" << endl; else if (has_multiple) cout << "Ambiguous" << endl; else { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << g[i][j]; } cout << endl; } } return 0; } ``` 时间复杂度为 $O(2^k \cdot nm)$,其中 $k$ 为未知格子数量,因此可以通过本题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值