题目描述
One of the cool UCF CS alumni is Dr. Greg, The Palindrome Tipper. A palindrome is a string
that reads the same forward and backward, e.g., madam, abba, 3, 44, 525.
One cool thing about Dr. Greg is that he leaves at least 20% tip when he eats out, e.g., if the meal is 30, Dr. Greg leaves 30, Dr. Greg leaves 6 (30*.20) for tip. If the tip (20%) is not a whole dollar amount, he rounds up the tip to make it a whole number. For example, if the meal is 12, a 20% tip would be12,a202.40 (12*0.20) but Dr. Greg would leave $3 for tip.
Another cool thing about Dr. Greg is that he is a palindrome guru. If his total bill (meal plus tip) is not a palindrome, he will increase the total (by adding to the tip) to make the total a palindrome. He will, of course, add the minimum needed to make the total a palindrome.
The Problem:
Given Dr. Greg’s meal cost, your program should determine the tip amount for him (according to his rules) and the total bill.
输入描述:
The first input line contains a positive integer, n, indicating the number of times Dr. Greg ate out. The meal costs are on the following n input lines, one per line. Each input will contain an integer between 5 and 10000 (inclusive).
输出描述:
At the beginning of each test case, output “Input cost: c” where c is the input cost. Then, on the next output line, print the tip amount and the total bill, separated by one space. Leave a blank line after the output for each test case.
示例1
输入
2
12
84
输出
Input cost: 12
10 22
Input cost: 84
17 101
题目大意:
给定一个金额,金额*0.2等于小费,小费小数部分要四舍五入,小费+金额=总账目,答案要计算输出各金额的小费和总账目,但是总账目如果是回文数直接输出即可,如果总账目不是回文数,需提高小费,使得总账目变为相近的最小的回文数。
解题思路:
本题直接暴力模拟即可
模拟计算的整个过程,最后判断总账目是否是回文数,写一个判断回文的函数即可,如果不是回文数的话,就让总账目循环自增加1,进行递归判断。
AC代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
bool huiwen(int x)
{
int y=0;
int t=x;
while(x>0)
{
y=y*10+x%10;
x=x/10;
}
if(t==y)
return true;
else
return false;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int a;
cin>>a;
int b;
b=ceil(a*0.2);
int t;
t=b+a;
if(huiwen(t))
{
cout<<"Input cost: "<<a<<endl;
cout<<b<<' '<<a+b<<endl;
cout<<endl;
}
else
{
while(!huiwen(t))
{
t++;
}
cout<<"Input cost: "<<a<<endl;
cout<<t-a<<' '<<t<<endl;
cout<<endl;
}
}
return 0;
}
官方代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t, n;
cin>> t;
while(t --){
cin>>n;
int t = ceil(n * 0.2);
for(int i = n + t; i < 13000; i ++){
int s,y = 0;
s = i;
while(s > 0){
y = y * 10 + s % 10;
s = s / 10;
}
if(y == i){
cout<<"Input cost: "<< n << endl <<i - n<<' '<< i << endl <<endl;
break;
}
}
}
return 0;
}