题目链接:http://codeforces.com/problemset/problem/328/A

一开始单纯地直接判断给出的序列是等差还是等比,连这一句“You should also print 42 if the next element of progression is not integer ”  都直接忽略,因为 Output 中没说,只是描述里讲了(要积累经验啊!!),其实是大错特错!

这个题目的关键是,当判断出给定的序列是等比时,求出下一个数(假设为result)是否是小数的判断(等差就没有这个问题,因为都是整数)。这里我采取的办法是把浮点型的result(用来保存待求的下一个数)转化为字符型s(字符型能够方便地看到是否存了小数点),然后通过strchr找到小数点的位置,判断小数点后的第一个数和字符型最后一个数是否是0,如果都为0,说明不是小数(类似这种表示:10000.000000,因为转化为字符型的s时是以 %lf 的格式来转的,默认情况下它会保留到小数点后6位),否则为小数。

 #include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std; int main()
{
int a, b, c, d, flag;
double a1, b1, c1;
char *p, s[];
while (cin >> a >> b >> c >> d)
{
flag = ;
if (b - a == c - b && c - b == d - c)
{
printf("%d\n", (d-c)+d);
flag = ;
}
a1 = (double)b * 1.0 / a; // 强制类型转换,把整型转化为双精度,考虑到公比有可能是小数
b1 = (double)c * 1.0 / b;
c1 = (double)d * 1.0 / c;
// cout << a1 << " " << b1 << " " << c1 << endl;
if (a1 == b1 && b1 == c1 && !flag)
{
double result = a1 * d;  // 保存要求的下一个数
sprintf(s, "%lf", result);
// cout << "s = " << s << endl;
p = strchr(s, '.');  // 特别要注意得出的p不是小数点的位置
int pos = p - s; // 找到小数点的位置
// cout << "pos+1 = " << pos+1 << endl;
// cout << "s[strlen(s)] = " << s[strlen(s)-1] << endl;
if (s[pos+] == '' && s[strlen(s)-] == '')  // 这两个条件可以判断出求出的下一个数是否是小数
{
printf("%.0lf\n", result);  // 不是小数,而是整数(类似123.000这种表示)
}
else  // 等比序列中求出的下一个数是小数
{
printf("42\n");
}
flag = ;
}
if (!flag)  // 既不是等差又不是等比
printf("42\n");
}
return ;
}

codeforces A. IQ Test 解题报告的更多相关文章

  1. Codeforces Round 665 赛后解题报告(暂A-D)

    Codeforces Round 665 赛后解题报告 A. Distance and Axis 我们设 \(B\) 点 坐标为 \(x(x\leq n)\).由题意我们知道 \[\mid(n-x)- ...

  2. Codeforces Round 662 赛后解题报告(A-E2)

    Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...

  3. Codeforces Round #277.5 解题报告

    又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...

  4. codeforces B. Simple Molecules 解题报告

    题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...

  5. codeforces 591A. Wizards' Duel 解题报告

    题目链接:http://codeforces.com/problemset/problem/591/A 题目意思:其实看下面这幅图就知道题意了,就是Harry 和 He-Who-Must-Not-Be ...

  6. codeforces 582A. GCD Table 解题报告

    题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...

  7. codeforces 581C. Developing Skills 解题报告

    题目链接:http://codeforces.com/problemset/problem/581/C 题目意思:给出 n 个数:a1, a2, ..., an (0 ≤ ai ≤ 100).给出值 ...

  8. codeforces 577B. Modulo Sum 解题报告

    题目链接:http://codeforces.com/problemset/problem/577/B 题目意思:就是给出 n 个数(a1, a2, ..., an) 和 m,问能不能从这 n 个数中 ...

  9. codeforces 540B.School Marks 解题报告

    题目链接:http://codeforces.com/problemset/problem/540/B 题目意思:给出 k 个test的成绩,要凑剩下的 n-k个test的成绩,使得最终的n个test ...

随机推荐

  1. 移动H5页面,keyup事件不好使用处理解决

    1.mouse事件换成touch,pointer事件,keyup换成textInput事件

  2. sdibt 1244 烦人的幻灯片

    在这个OJ站还没号,暂时没提交,只是过了样例 真不愧是烦人的幻灯片,烦了我一小时 ---更新:OJ测试完毕,AC 烦人的幻灯片问题 Time Limit: 1 Sec  Memory Limit: 6 ...

  3. Tomcat Can't load AMD 64-bit .dll on a IA 32

    Java.lang.UnsatisfiedLinkError: C:\apache\apache-tomcat-7.0.14\bin\tcnative-1.dll: Can't load AMD 64 ...

  4. vim YouCompleteMe

    http://www.ithao123.cn/content-1906969.html http://www.it165.net/os/html/201503/12190.html

  5. 在Vs2012 中使用SQL Server 2012 Express LocalDB打开Sqlserver2012数据库

    http://www.cnblogs.com/huangtailang/p/4221164.html 背景:个人电脑中使用的是VS2012,数据库为2008R2,最近需要打开一个SqlServer20 ...

  6. spring事务学习(转账案例)(一)

    一.创建数据库并插入数据 create database spring_transaction; use spring_transaction; create table account( id in ...

  7. javascript学习随笔(二)原型prototype

    JavaScript三类方法: 1.类方法:2.对象方法:3.原型方法;注意三者异同 例: function People(name){ this.name=name; //对象方法 this.Int ...

  8. web classpath 路径说明

    classpath路径在每个J2ee项目中都会用到,即WEB-INF下面的classes目录,所有src目录下面的java.xml.properties等文件编译后都会在此,所以在开发时常将相应的xm ...

  9. Logistic Regression and Gradient Descent

    Logistic Regression and Gradient Descent Logistic regression is an excellent tool to know for classi ...

  10. 复选框全选、全不选和反选的效果实现VIEW:1592

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...