这是我参与更文挑战的第6天,活动详情查看:更文挑战
A+B and C
Given three integers A, B and C in [-263, 263), you are supposed to tell whether A+B > C.
#include <cstdio>
#include <algorithm>
using namespace std;
int sum[1005];
int main() {
int n;
scanf("%d",&n);
for (int i = 1; i <= n; ++i) {
long long a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
long long ab = a+b;
if(ab>c) sum[i] = 1;
else sum[i] = 0;
if(a>0 && b>0 && ab<0) sum[i] = 1;
if(a<0 && b<0 && ab>=0) sum[i] = 0;
}
for (int i = 1;i <= n;++i) {
if(sum[i]) printf("Case #%d: true\n",i);
else printf("Case #%d: false\n",i);
}
return 0;
}
PS:
- long long 的范围时[-2^63^,2^63^),因此两个long long类型的整数相加可能会溢出,需要通过A,B,A+B的符号来判断是否溢出
- long long 存储2^63^的时候会自动变成-2^63^,无法区分左右边界。
A+B in Hogwarts
If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- as Hagrid explained it to Harry, "Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it's easy enough." Your job is to write a program to compute A+B where A and B are given in the standard form of "Galleon.Sickle.Knut" (Galleon is an integer in [0, 107], Sickle is an integer in [0, 17), and Knut is an integer in [0, 29)).
#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
int A=0,B=0,C=0;
scanf("%d.%d.%d",&A,&B,&C);
int a=0,b=0,c=0;
scanf("%d.%d.%d",&a,&b,&c);
A+=a;
B+=b;
C+=c;
B+=(C/29);
C%=29;
A+=(B/17);
B%=17;
printf("%d.%d.%d",A,B,C);
return 0;
}
Ps:审题要清晰。
Kuchiguse
The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often exaggerated artistically in Anime and Manga. For example, the artificial sentence ending particle "nyan~" is often used as a stereotype for characters with a cat-like personality: Itai nyan~ (It hurts, nyan~) Ninjin wa iyada nyan~ (I hate carrots, nyan~) Now given a few lines spoken by the same character, can you find her Kuchiguse?
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
string rev(string str){
int len = str.length();
for (int i = 0; i < len/2;++i){
char temp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = temp;
}
return str;
}
string abc[259];
int main() {
int n;
scanf("%d",&n);
getchar();
for (int i = 1; i <= n; ++i) {
getline(cin,abc[i]);
abc[i] = rev(abc[i]);
}
int short1;
bool a=1;
string res="";
for (int i = 1; i < n; ++i) {
if(abc[i].length()<abc[i+1].length()) short1=abc[i].length();
}
for (int i = 0; i< short1;++i){
char temp;
for(int j = 1; j < n&& a==1;++j){
temp = abc[j][i];
if(abc[j][i]==abc[j+1][i]) continue;
else a=0;
}
if(a==1) res+=temp;
else break;
}
res = rev(res);
if(res == "")
printf("nai");
else
for (int i = 0; i < res.length();++i){
printf("%c",res[i]);
}
return 0;
}
思考和总结
对于二分法来说,最麻烦的可能就是边界的判断了,记得有一次我在比赛的时候,我一直卡在一道题上面,不断提交导致罚时,以至于最后痛失名次。 对于边界的判断,一定要提前想好,算好,测试的时候选择一些边界值来计算。
PS: 可以用algorithm的reverse函数来反转字符串。