3.14牛客2021年度训练联盟热身训练赛第二场C.Tip to be Palindrome[回文数]

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述 

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,a2012,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

✖向上取整函数使用成round了

❀在这里我将数字拆开存储为字符串来判断是否为回文

bool isp(int n)
{
    int t=n;
    string s;
    while(t)
    {
        int x=t%10;
        s+=x;
        t/=10;
    }
    //cout<<s<<endl;
    int len=s.size();
    int p=round(len/2);
    for(int i=0;i<p;i++)
    {
        if(s[i]!=s[--len])
            return false;
    }
    return true;
}

瞅瞅榜一代码

bool judge(int x){

    int temp=x,y=0;

    while(temp){

        y*=10;

        y+=temp%10;

        temp/=10;

    }

    return x==y;

}

他们是将x拆分后构造从个位到十位一个对称的y,然后判断x与y是否相等

wa

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
#define Inf 0xfffffff
#define N 501000
#define eps 1e-7
using namespace std;
 
bool isp(int n)
{
    int t=n;
    string s;
    while(t)
    {
        int x=t%10;
        s+=x;
        t/=10;
    }
    //cout<<s<<endl;
    int len=s.size();
    int p=round(len/2);
    for(int i=0;i<p;i++)
    {
        if(s[i]!=s[--len])
            return false;
    }
    return true;
}

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int c;
        cin>>c;
        int tip,total;
        tip=round(c*0.2);
        total=tip+c;
        //cout<<isp(total)<<endl;
        while(!isp(total))
        {
            //cout<<"hjjh";
            total++;
        }
        cout<<"Input cost: "<<c<<endl;
        cout<<total-c<<" "<<total<<endl;
        if(n!=0)
            cout<<endl;
    }
    return 0;
}

ac

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
#define Inf 0xfffffff
#define N 501000
#define eps 1e-7
using namespace std;
 
bool isp(int n)
{
    int t=n;
    string s;
    while(t)
    {
        int x=t%10;
        s+=x;
        t/=10;
    }
    //cout<<s<<endl;
    int len=s.size();
    int p=ceil(len/2);
    for(int i=0;i<p;i++)
    {
        if(s[i]!=s[--len])
            return false;
    }
    return true;
}

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int c;
        cin>>c;
        int tip,total;
        tip=ceil(c*0.2);
        total=tip+c;
        //cout<<isp(total)<<endl;
        while(!isp(total))
        {
            //cout<<"hjjh";
            total++;
        }
        cout<<"Input cost: "<<c<<endl;
        cout<<total-c<<" "<<total<<endl;
        if(n!=0)
            cout<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值