题目是这样的:
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
This is a simple question.
Given you two hexadecimal digits x and y, you
should determine whether 2x+10 is greater than 3y+5.
输入描述:
Each test contain multiple test cases. The first line contains
the number of test cases t (1≤t≤100). Description of the test cases follow.
The description of each test case consists of two
hexadecimal digits x, y separated by spaces.
It's guarantee that x ,y only consists of number '0'-'9'
and capital letters 'A'-'F' and there are no leading zeros.
1≤x,y<16^1000
输出描述:
For each test case, if 2x+10 > 3y+5, print "Yes",
Otherwise print "No".
示例1
输入
3
A B
F0 E11
FF0123FFFFFFFFFF 01FEA23FFF
输出
No
No
Yes
我的代码是这样的
#include<stdio.h>
#include<string.h>
int turn(char a, int t)
{
if (a == 'A' || a == 'a') return 10 * t;
else if (a == 'B' || a == 'b') return 11 * t;
else if (a == 'C' || a == 'c') return 12 * t;
else if (a == 'D' || a == 'd') return 13 * t;
else if (a == 'E' || a == 'e') return 14 * t;
else if (a == 'F' || a == 'f') return 15 * t;
else return (a - '0') * t;
}
char retu(int t)
{
if (t >= 0 && t <= 9) return t + '0';
else return 'A'+t-10;
}
int main()
{
int t;
scanf("%d", &t);
getchar();
while (t--)
{
char a[1005] = { 0 }, b[1005] = { 0 };
char ta[1005] = { 0 }, tb[1005] = { 0 };
char ch;
int answer=0;
for(int g=0;;)//处理前导零
{
scanf("%c",&ch);
if(ch==' ') break;
else
{
if(answer==0)
{
if(ch=='0') continue;
else answer=1;
}
if(answer==1)
{
a[g]=ch;
g++;
}
}
}
answer=0;
for(int g=0;;)//处理前导零
{
scanf("%c",&ch);
if(ch=='\n') break;
else
{
if(answer==0)
{
if(ch=='0') continue;
else answer=1;
}
if(answer==1)
{
b[g]=ch;
g++;
}
}
}
int loa = strlen(a);
int lob = strlen(b);
int ia[1005] = { 0 }, ib[1005] = { 0 };
for (int i = 0; i <= loa; i++)//处理x
{
if(i < loa)
ia[i] += turn(a[i], 2);
if (i == 0)
ia[i] += 10;//转成运算后的数字
while (ia[i] >= 16)//判断进位
{
ia[i] -= 16;
ia[i + 1] += 1;
}
if(i < loa || i == loa && ia[i] != 0)//防止添加前导0影响长度
ta[i] = retu(ia[i]);
}
for (int i = 0; i <= lob; i++)//处理y
{
if(i < lob)
ib[i] += turn(b[i], 3);//同x
if (i == 0)
ib[i] += 5;
while (ib[i] >= 16)
{
ib[i] -= 16;
ib[i + 1] += 1;
}
if(i < lob || i == lob && ib[i] != 0)
tb[i] = retu(ib[i]);
}
int lowa = strlen(ta), lowb = strlen(tb);
if (lowa > lowb)//长度大的必然大
printf("Yes\n");
else if (lowa < lowb)
printf("No\n");
else
{
int u;
for (u = lowa - 1; u >= 0; u--)//由于存的时候是从位置0开始存的,所以位置0是最小位,应从最后一位开始比
{
if (ta[u] > tb[u])
{
printf("Yes\n");
break;
}
else if(ta[u] < tb[u])
{
printf("No\n");
break;
}
else continue;
}
if (u == -1)//相等的情况
printf("No\n");
}
}
}
报错是这样的.:
#include<stdio.h>
#include<string.h>
int turn(char a, int t) {
if (a >= '0' && a <= '9') return (a - '0') * t;
else return (a - 'A' + 10) * t ;
}
char retu(int t) {
if (t >= 0 && t <= 9) return t + '0';
else return 'A' + t - 10;
}
char a[1005] = { 0 }, b[1005] = { 0 };
//char ta[1005] = { 0 }, tb[1005] = { 0 };
int ia[1005] = { 0 }, ib[1005] = { 0 };
int main() {
int t;
scanf("%d", &t);
while (t--) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(ia, 0, sizeof(ia));
memset(ib, 0, sizeof(ib));
scanf("%s %s", a, b);
int loa = strlen(a);
int lob = strlen(b);
int lia = 0, lib = 0;
for (int i = loa-1; i >=0; i--) { //处理x
ia[lia] += turn(a[i], 2);
if (i == loa - 1)
ia[lia] += 5;//转成运算后的数字
ia[lia + 1] += ia[lia] / 16;
ia[lia] %= 16;
lia++;
}
if(ia[lia]>0){
lia++;
}
while(ia[lia] >= 16) {
ia[lia + 1] += ia[lia] / 16;
ia[lia] %= 16;
lia++;
}
while(ia[lia]==0) lia--;
for (int i = lob-1; i >=0; i--) { //处理y
ib[lib] += turn(b[i], 3);
ib[lib + 1] += ib[lib] / 16;
ib[lib] %= 16;
lib++;
}
if(ia[lib]>0){
lib++;
}
while(ib[lib] >= 16) {
ib[lib + 1] += ib[lib] / 16;
ib[lib] %= 16;
lib++;
}
while(ib[lib]==0) lib--;
// int lowa = strlen(ta), lowb = strlen(tb);
if (lia > lib)//长度大的必然大
printf("Yes\n");
else if (lia < lib)
printf("No\n");
else {
int u;
for (u = lia - 1; u >= 0; u--) { //由于存的时候是从位置0开始存的,所以位置0是最小位,应从最后一位开始比
if (ia[u] > ib[u]) {
printf("Yes\n");
break;
} else if(ia[u] < ib[u]) {
printf("No\n");
break;
} else continue;
}
if (u == -1)//相等的情况
printf("No\n");
}
}
return 0;
}
题目提示了
64bit IO Format: %lld
所以应该用long long类型
#include <string>
#include <iostream>
#include <cassert>
inline char to_char(int n)
{
const char *a = "0123456789ABCDEF";
return a[n];
}
inline int from_char(char ch)
{
if (ch >= '0' && ch <= '9')
return ch - '0';
else
return ch - 'A' + 10;
}
// return a > b
inline bool greater_than(const std::string &a, const std::string &b)
{
if (a.length() > b.length())
return true;
if (b.length() > a.length())
return false;
const char *p = a.c_str();
const char *q = b.c_str();
while (*p && *q)
{
int n = from_char(*p);
int m = from_char(*q);
if (n > m)
return true;
if (m > n)
return false;
p++;
q++;
}
return false;
}
// return a * x + b
std::string f(const std::string &x, int a, int b)
{
std::string r;
int c = 0;
for (auto itr = x.rbegin(); itr != x.rend(); ++itr)
{
int n = from_char(*itr);
n = a * n + c;
if (itr == x.rbegin())
n += b;
c = n / 16;
r.push_back(to_char(n % 16));
}
if (c != 0)
r.push_back(to_char(c));
return std::string(r.rbegin(), r.rend());
}
int main()
{
std::string x, y;
int t;
std::cin >> t;
for (int i = 0; i < t; i++)
{
std::cin >> x >> y;
std::string rx = f(x, 2, 10);
std::string ry = f(y, 3, 5);
if (greater_than(rx, ry))
std::cout << "Yes\n";
else
std::cout << "No\n";
}
return 0;
}