nyoj 稍具技巧的题(二)

2014-11-24 11:24:02 · 作者: · 浏览: 1
++写了好长,人家两句读入输出,换一下格式ok

scanf("%x%c%x", &a, &c, &b);
if(c == '+')
printf("%o\n", a+b);
if(c == '-')
printf("%o\n", a-b);

[cpp]
#include
#include
using namespace std;
int main()
{
int N;
string str;
cin>>N;
while(N--)
{
cin>>str;
char ch;
int num1 = 0;
int num2 = 0;
int temp;
int oper = str.find('+');
if(oper == -1)
oper = str.find('-');
for(int i=0;i {
ch = str.at(i);
num1 *= 16;
if(ch >= '0' && ch <= '9')
temp = ch - '0';
else if(ch >= 'a' && ch <= 'f')
temp = ch - 'a' + 10;
num1 += temp;
}

cout<
for(int i=oper+1;i {
ch = str.at(i);
num2 *= 16;
if(ch >= '0' && ch <= '9')
temp = ch - '0';
else if(ch >= 'a' && ch <= 'f')
temp = ch - 'a' + 10;
num2 += temp;
}

cout<
ch = str.at(oper);
if(ch == '+')
{
num1 += num2;
}
else if(ch == '-')
{
num1 -= num2;
}
cout< str = "";
while(num1)
{
str += ('0'+(num1 % 8));
num1 /= 8;
}
string s(str.rbegin(),str.rend());
cout << s < }
return 0;
}
[cpp]
#include
#include
using namespace std;
int main()
{
int N;
cin>>N;
while(N--)
{
int a, b;
char c;
scanf("%x%c%x", &a, &c, &b);
if(c == '+')
printf("%o\n", a+b);
if(c == '-')
printf("%o\n", a-b);
}
return 0;
}
荷兰国旗问题 nyoj 268
用三个数分别记下三个字母出现的次数,然后依序输出就行了,他们提供的最优代码也是这样写的

[cpp]
#include
#include
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
string s1;
int a=0,b=0,c=0;
cin>>s1;
for(int i=0;i {
if(s1.at(i) == 'R')
{
a++;
}
else if(s1.at(i) == 'W')
b++;
else
c++;
}
for(int i=0;i cout<<"R";
for(int i=0;i cout<<"W";
for(int i=0;i cout<<"B";
cout<
}
return 0;
}
正三角形的外接圆面积 nyoj 274
看一个叫做“宝”的仁兄的代码,我觉得很好,收集一下:

[cpp]
#include
using namespace std;
#define PI 3.1415926
int main()
{
int m;
cin>>m;
while(m--)
{
double s,a;
cin>>a;
s=PI*a*a/3.0;
cout.setf(ios_base::fixed);
cout.precision(2);
co