6:点和直线
描述 |
---|
判断一个点是否在一条直线上 |
输入 |
第1行是一个正整数n,表示测试案例的数量 从第2行到第n+1行,每行有五个数字a、b、c、d、e(不一定是整数,c和d不会都为0),其中a和b是点的横坐标和纵坐标,c、d、e构成了一条直线cx+dy=e。 |
输出 |
如果点(a,b)在直线cx+dy=e上,则输出true,否则输出false。 每组案例输出完都要换行。 |
样例输入复制样例 |
1 1 1 1 1 2 |
样例输出 |
true |
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
double a, b, c, d, e;
cin >> n;
while (n--)
{
cin >> a >> b >> c >> d >> e;
if (c == 0)
{
if (abs(d * b - e) <= 1e-6) cout << "true" << endl;
else cout << "false" << endl;
}
else if (d == 0)
{
if (abs(c * a-e )<= 1e-6) cout << "true" << endl;
else cout << "false" << endl;
}
else if (abs(c * a + d * b-e) <= 1e-6) cout << "true" << endl;
else cout << "false" << endl;
}
return 0;
}
知识点:浮点数相加减是不准确的,所以让相减的数趋近于一个比较小的数1e-6(0.000001)
8:疯狂的程序员
描述 |
---|
有一个程序员给自己定了个目标,第1天写1个代码,之后两天(第2、3天)里,每天写2个代码;之后3天(第4、5、6天)里,每天写3个代码……当连续N天每天写N个代码后,程序员会在之后的连续N+1天里,每天写N+1个代码。 给定一个天数,问从第一天开始的这些天里,程序员一共写了多少个代码。 |
输入 |
第1行是一个正整数n,表示测试案例的数量。 从第2行到第n+1行,每行有1个正整数,表示天数。 |
输出 |
针对每组测试案例,输出程序员写了多少个代码。 每组案例输出完都要换行。 |
样例输入复制样例 |
1 3 |
样例输出 |
5 |
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
int a;
cin >> a;
int cnt = 1,sum=0,b=0;
for (int i = 1; true; i++)
{
for (int j = 1; j <= cnt; j++)
{
sum += cnt;
b++;
if (a == b) break;
}
cnt++;
if (a == b) break;
}
cout << sum << endl;
}
return 0;
}
11:伪素数
描述 |
---|
判断一个数字a是否是m-伪素数。 |
输入 |
第1行是一个正整数n,表示测试案例的数量。 |
输出 |
针对每组案例,如果a是m-伪素数,则输出yes,否则输出no。每组案例输出完都要换行。 |
样例输入复制样例 |
2 12 2 12 4 |
样例输出 |
no yes |
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
long long int a;
int m, cnt = 0;
cin >> a >> m;
if (a == 2 )
{
cout << "yes" << endl;
continue;
}
for (int i = 2; i <= sqrt(a); i++)
{
if (a % i == 0) cnt++;
}
int b = sqrt(a), c;
if (a == b * b) c = cnt * 2 - 1;
else c = cnt * 2;
if (c <= m) cout << "yes" << endl;
else cout << "no" << endl;
}
return 0;
}
知识点:break,不跳if,跳for,while,Switch。。。。,只跳一层
12:双向箭头
描述 |
---|
根据a和b的值输出由*组成的双向箭头形状。 其中a是正奇数,代表图案有几行;b是正整数,代表双向箭头中间的横线有多长。 |
输入 |
只有一组案例,由两个正整数a和b组成。 |
输出 |
根据a和b的值输出由*组成的双向箭头形状。每行最后不要有多余空格。最后一行星号输出完有个换行。 |
样例输入复制样例 |
5 3 |
样例输出 |
|
#include<iostream>
using namespace std;
int main()
{
int n, b;
cin >> n >> b;
for (int i = 0; i <= n / 2; i++)
{
for (int j = 0; j < n / 2 - i; j++)
{
cout << " ";
}
for (int j = 0; j < i + 1; j++)
{
cout << "*";
}
for (int j = n / 2 + 1; j <= n / 2 + b; j++)
{
if (i == n / 2) cout << "*";
else cout << " ";
}
for (int j = n / 2 + b + 1; j < n / 2 + b + 2 + i; j++)
{
cout << "*";
}
cout << endl;
}
for (int i = 0; i < n / 2; i++)
{
for (int j = 0; j < i + 1; j++)
{
cout << " ";
}
for (int j = 0; j < n / 2 - i; j++)
{
cout << "*";
}
for (int j = n / 2 + 1; j < n / 2 + b + 1; j++)
{
cout << " ";
}
for (int j = n / 2 + b + 1; j < n + b - i; j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
13:完全平方数-3
描述 |
---|
数学上,若一个数能表示成某个整数的平方的形式,则称这个数为完全平方数。 计算a到b之间所有完全平方数的和。 |
输入 |
一个正整数n,表示案例的数量。 每组案例由2个整数a和b组成。(-900000000<=a<=b<=900000000) |
输出 |
针对每组案例,输出一个长整数(long long int),表示a到b之间(包含a和b)所有完全平方数的和。 每组案例输出完都要换行。 |
样例输入复制样例 |
2 4 10 -1 5 |
样例输出 |
13 5 |
提示说明 |
第一组样例中,4到10之间的完全平方数有4、9,总和是13。 第二组样例中,-1到5之间的完全平方数有0、1、4,总和是5。 |
#include<iostream>
#include<cmath>
using namespace std;
bool isSquare(long long int a)
{
if (a < 0) return false;
int b = (int)(sqrt(a) + 0.0001);
if (b * b == a) return true;
else return false;
}
int main()
{
int a, b;
int n;
cin >> n;
while (n--)
{
long long int sum = 0;
cin >> a >> b;
if (a < 0)
{
a = 0;
}
if (b < 0)
{
b = 0;
}
long long int a1 = sqrt(a), b1 = sqrt(b);
if (!isSquare(a)) a1++;
for (int i = a1; i <= b1; i++)
{
sum = sum + i * i;
}
cout << sum << endl;
}
return 0;
}
14:平方和-3
描述 |
---|
已知一个正整数x,问x能否凑成三个互不相同的正整数的平方和。 |
输入 |
一个正整数n,表示测试案例的数量。 每组案例由一个正整数x组成(x不大于1e+8)。 |
输出 |
针对每组案例,如果x可以表示成三个互不相同的正整数的平方和,那么输出Yes,否则输出No。 每组案例输出完都要换行。 |
样例输入复制样例 |
2 30 10 |
样例输出 |
Yes No |
提示说明 |
30=1*1+2*2+5*5 |
#include<iostream>
#include<cmath>
bool isSquare(int a)
{
if (a < 0) return false;
int b = (int)(sqrt(a) + 0.0001);
if (b * b == a) return true;
else return false;
}
bool f(int a)
{
for (int i = 1; i * i <= sqrt(a); i++)
{
for (int j = i + 1; j * j <= (a - i * i) / 2; j++)
//i+1的原因,排除了像27=1*1+1*1+5*5这样1重复的案例
{
int m = a - i * i - j * j;
if (isSquare(m)) return true;
}
}
return false;
}
using namespace std;
int main()
{
int n,a;
cin >> n;
while (n--)
{
int m;
cin >> a;
if (f(a)) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}