第 3 章 条件判断与循环
1.整除判断
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int x,y;cin >> x >> y;
if(x>y) cout<< ">";
else if(x == y) cout << "=";
else cout << "<";
return 0;
}
2.⽐较⼤⼩
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int m,n; cin >> m>>n;
double a = (double)m/n;
if(a == m/n) cout << "YES";
else cout << "NO";
return 0;
}
- 输出绝对值
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
double n;cin >> n;
double a = abs(n);
printf("%.2f",a);
return 0;
}
- 奇偶数判断
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;cin >> n;
if(n %2 !=0) cout << "odd";
else cout << "even";
return 0;
}
- 有⼀⻔课不及格的学⽣
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int cnt = 0;
int n;
while(cin >> n){
if(n < 60) cnt++;
}
if(cnt == 1) cout << 1;
else cout<<0;
return 0;
}
- 等差数列末项计算
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int a1,a2,n;cin >> a1 >> a2 >> n;
cout << a1+(n-1)*(a2-a1);
return 0;
}
- 最⼤值输出
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c; cin >> a >> b >> c;
int d = a>b?a:b;
int m = d >c?d:c;
cout << m;
return 0;
}
- 特殊计算
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
long long x,y;cin >> x >> y;
if(y%x == 0) cout << x+y;
else cout << y-x;
return 0;
}
- Apples Prologue / 苹果和⾍⼦
cpp
#include<bits/stdc++.h>
using namespace std;
int m,t,s;
int main()
{
cin>>m>>t>>s;
if(t==0)
{
cout<<0<<endl;
return 0;
}
if(s%t==0)
cout<<max(m-s/t,0)<<endl;
else
cout<<max(m-s/t-1,0)<<endl;
return 0;
}
- 闰年的判断
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;cin >> n;
if((n%4==0&&n%100!=0)||(n%400 == 0)) cout << 1;
else cout << 0;
return 0;
}
- 晶晶赴约会
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int data;cin >> data;
if(data%2==0 || data == 7) cout << "YES";
else cout << "NO";
return 0;
}
- 三⻆形判断
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;cin>>a>>b>>c;
int maxx = max(a,max(b,c));
int minn = min(a,min(b,c));
int mid = a+b+c-maxx-minn;
if(minn+mid>maxx) cout << 1;
else cout << 0;
return 0;
}
- 判断能否被 3,5,7 整除
cpp
#include <bits/stdc++.h>
using namespace std;
int n;
int main(){
cin >> n;//输入
if (n % 3 == 0 && n % 5 == 0 && n % 7 == 0) cout << "3 5 7";//3,5,7都能整除
else if (n % 3 == 0 && n % 5 == 0) cout << "3 5";//整除3,5
else if (n % 5 == 0 && n % 7 == 0) cout << "5 7";//整除5,7
else if (n % 3 == 0 && n % 7 == 0) cout << "3 7";//整除3,7
else if (n % 3 == 0) cout << 3;//仅整除3
else if (n % 5 == 0) cout << 5;//仅整除5
else if (n % 7 == 0) cout << 7;//仅整除7
else cout << 'n';//3,5,7无法整除
return 0;//好习惯
}
- 数的性质
cpp
#include <iostream>
using namespace std;
int main() {
int x; cin >> x;
bool p1 = x%2 == 0, p2 = x>4 && x<=12;
cout << (p1&&p2) << " " << (p1||p2) << " " << (p1!=p2) << " " << (!p1&&!p2);
return 0;
}
- 四季
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int x;cin>>x;
int d = x%100;
if(d>=3&&d<=5) cout << "spring";
else if(d >=6 && d <=8) cout << "summer";
else if(d >=9 && d <=11) cout << "autumn";
else cout<< "winter";
return 0;
}
- 简单计算器
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
char c;
cin>>a>>b;
cin>>c;
if(b==0&&c=='/'){
cout<<"Divided by zero!"<<endl;
return 0;
}
if(c=='+')
cout<<a+b<<endl;
else if(c=='-')
cout<<a-b<<endl;
else if(c=='*')
cout<<a*b<<endl;
else if(c=='/')
cout<<a/b<<endl;
else
cout<<"Invalid operator!"<<endl;
return 0;
}
- 反向输出每⼀位
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;cin >> s;
reverse(s.begin(),s.end());
cout << s;
return 0;
}
- 数位之和
cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;cin >> s;
int num = 0;
for(int i = 0;i <s.size();i++){
if(s[i]-'0'>=0 &&(s[i]-'0'<=9)) num+=(s[i]-'0');
}
cout << num;
return 0;
}
- 求和1
cpp
#include <iostream>
using namespace std;
int main(){
int n,i;
cin>>n;
long sum=0;
for(i=1;i<=n;i++)
sum=sum+i;
cout<<sum<<endl;
return 0;
}
- 含 k 个 3 的数
cpp
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
long long m;
int k,cnt3 = 0;
cin>>m>>k;
while(m){
if(m % 10 == 3)++cnt3;
m /= 10;
}
if(cnt3 == k)cout << "YES";
else cout << "NO";
return 0;
}
- ⻆⾕猜想
cpp
#include <bits/stdc++.h>
using namespace std;
long long n;
int main() {
cin >> n;
while (n != 1) {
if (n % 2 != 0) {
cout << n << "*3+1=" << n * 3 + 1 << endl;
n = n * 3 + 1;
} else {
cout << n << "/2=" << n / 2 << endl;
n = n / 2;
}
}
cout << "End" << endl;
return 0;
}
- 计算多项式的值
cpp
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
double x,ans = 1;
int n;
int main(){
cin >> x >> n;
ans = 1*(1-pow(x,n+1))/(1-x)*1.0;//公式
printf ("%.2f\n",ans);
return 0;
}
- 求和2(⾮OJ):计算1~100之间3的倍数的数字之和
cpp
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
for(int i = 1; i <= 100; i++)
{
if(i % 3 == 0)
{
sum += i;
}
}
cout << sum << endl;
return 0;
}
- 求平均年龄
cpp
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
double sum = 0;//累加器设double变量
for (int i = 1; i <= n; i ++) {
int x;
cin >> x;
sum += x;//累加答案
}
printf ("%.2lf", sum/n);//保留两位小数
return 0;
}
- 奥运奖牌计数
cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,c,d,b,e=0,f=0,g=0;//e,f,g分别是金银铜牌总数,a为天数
cin>>a;
for(int i=0;i<a;++i)
{
cin>>c>>d>>b;//输入:金银铜牌数量
e+=c;
f+=d;
g+=b;
}
cout<<e<<' '<<f<<' '<<g<<' '<<e+f+g;//金、银、铜牌总数及总奖牌数
return 0;
}
- 鸡尾酒疗法
cpp
#include <stdio.h>
int n, a, b;
int main(void) {
scanf("%d %d %d", &n, &a, &b);
int c, d; double x = 1.0 * b / a, y;
while (n-- != 1) {
scanf("%d %d", &c, &d);
y = 1.0 * d / c;
if(y - x >= 0.05) puts("better");
else
if(x - y >= 0.05) puts("worse");
else puts("same");
}
return 0;
}
- 救援
cpp
#include <iostream>
#include <stdio.h>
using namespace std;
int n;
double ans;
int main() {
cin>>n;
for (int i=1;i<=n;i++) {
if (i%2==0) {//判断奇偶
ans-=1.0/i;
} else {
ans+=1.0/i;
}
}
printf("%.4lf",ans);
}
- 计算分数加减表达式的值
cpp
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
double a=1,b=1,c,s=0;
int i,n;
cin>>n;
for(i=1;i<=n;++i)
{
c=a+b;
a=b;
b=c;
//斐波那契部分;
s=s+b/a;//求和;
}
printf("%0.4lf\n",s);
return 0;
}
- 求分数序列和
cpp
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
double a=1,b=1,c,s=0;
int i,n;
cin>>n;
for(i=1;i<=n;++i)
{
c=a+b;
a=b;
b=c;
//斐波那契部分;
s=s+b/a;//求和;
}
printf("%0.4lf\n",s);
return 0;
}
- 统计正整数的位数(⾮OJ):输⼊⼀个正整数,计算这个整数是⼏位数?
cpp
#include <iostream>
using namespace std;
int main()
{
int n, cnt = 0;
cin >> n;
while(n > 0)
{
cnt++;
n /= 10;
}
cout << cnt;
return 0;
}
- 球弹跳⾼度的计算
cpp
#include<bits/stdc++.h>
using namespace std;
double h;
int main(){
scanf("%lf",&h);//输入h
printf("%lf %lf",h*767.0/256.0,h/1024.0);//代式子 输出所经过米数和第十次弹跳高度
return 0;
}
- 质因数分解
cpp
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=2;i<=n;i++)
if(n%i==0) {
cout<<n/i<<endl;
break;
}
return 0;
}
- 乘法表34. 包含数字9的数
cpp
#include<iostream>
using namespace std;
int main(){
cout << "1*1= 1" << endl;
cout << "1*2= 2 2*2= 4" << endl;
cout << "1*3= 3 2*3= 6 3*3= 9" << endl;
cout << "1*4= 4 2*4= 8 3*4=12 4*4=16" << endl;
cout << "1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25" << endl;
cout << "1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36" << endl;
cout << "1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49" << endl;
cout << "1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64" << endl;
cout << "1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81" << endl;
return 0;
}
- 斐波那契数列
cpp
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
int a;
cin>>a;
int x[40];
x[1]=x[2]=1;
for(int i=3;i<=a;i++)
x[i]=x[i-1]+x[i-2];
cout<<x[a]<<endl;
}
return 0;
}
- 求出 e 的值
cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
double e=1.0,sum=1.0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
e*=i;
sum+=1/e;
}
printf("%0.10lf",sum);
return 0;
}
- 三⻆形
cpp
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
// 外层循环控制行数
for (int i = 1; i <= n; i++)
{
// 内层循环打印i个星号
for (int j = 1; j <= i; j++)
{
cout << "*";
}
// 每行结束换行
cout << endl;
}
return 0;
}
- 画矩形
cpp
#include <cstdio>
int a, b, f;
char c;
int main(){
scanf("%d %d %c %d", &a, &b, &c, &f);
for (int i = 0; i < a; i++){
for (int j = 0; j < b; j++){
if (i != 0 && i != a - 1 && j != 0 && j != b - 1)
printf("%c", f == 0?' ':c);
else printf("%c", c);
}
printf("\n");
}
return 0;
}
- 第 n ⼩的质数
cpp
#include<iostream>
using namespace std;
int n,now=1;//now表示现在的数。
bool check(int x){
for(int i=2;i*i<=x;i++){//枚举到x的平方根就可以了,降低时间复杂度。
if(x%i==0)return 0;//若除了1和x本身还有别的因数,则x不是质数,返回0。
}
return 1;//否则返回1。
}
int main(){
cin>>n;
while(n!=0){
now++;
if(check(now)){
n--;
}
}
cout<<now;
return 0;
}
- ⽔仙花数
cpp
#include <iostream>
using namespace std;
int main()
{
// 遍历所有三位数
for (int num = 100; num <= 999; num++)
{
int a = num / 100; // 百位
int b = num / 10 % 10; // 十位
int c = num % 10; // 个位
// 判断水仙花条件
if (a*a*a + b*b*b + c*c*c == num)
{
cout << num << endl;
}
}
return 0;
}