当前位置:网站首页>洛谷入门3【循环结构】题单题解
洛谷入门3【循环结构】题单题解
2022-07-02 04:34:00 【阳懿】
目录
【入门3】循环结构 - 题单 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
找最小值
- 注意找最小值,最小值应初始化为一个较大的数;
- 找最大值,最大值应初始化为一个较小的数。
int n,a[100],min=1000; //初始化为较大的数。
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
{
if (a[i] < min)
min = a[i];
}
cout << min;分类平均
int n, k,sum1=0,sum2=0;
double ave1=0, ave2=0;
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
if (i%k == 0)
{
ave1 += i;
sum1++;
}
if (i%k != 0)
{
ave2 += i;
sum2++;
}
}
ave1 /= sum1;
ave2 /= sum2;
printf("%.1lf %.1lf", ave1, ave2);一尺之棰
int a,sum=1;
cin >> a;
while (1)
{
if (a == 1)
break;
a /= 2;
sum++;
}
cout << sum;数字直角三角形
- 前导0 可以用%02d
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int main() {
int n, sum = 0, k;
cin >> n;
k = n;//设置k 不然下面的i<=(n的表达式)这里的n也跟着变化。
//注意这个中间量设置,以后还会有很多题涉及到这种原值变化,但是后来的运算还要用到原值,这时,一定要一开始用一个变量等于原值。
for (int i = 1; i <= (n+1)*n/2; i++)
{
printf("%02d", i);
sum++;
if (sum == k)
{
sum = 0;//注意sum=0的位置
cout << endl;
k--;
}
}
return 0;
}阶乘之和
高精度
#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<stdio.h>
using namespace std;
int a[101] = { 0 }, b[101] = { 0 };
void mul(int x)//乘
{
int y = 0;
for (int i = 100; i >= 0; i--)
{
a[i] = a[i] * x + y; //a[101]除了a[100]其他都是初始化为0,之后每次进位都必须只根据上一位即y进位。除了第一位要利用x。
y = a[i] / 10;//y是进位
a[i] = a[i] % 10;
}
}
void add( )//加
{
int y = 0;
for (int i = 100; i >= 0; i--)
{
b[i] = b[i] +a[i] + y;
y = b[i] / 10; //y是进位
b[i] = b[i] % 10;
}
}
int main()
{
int n,i,w;
cin >> n;
a[100] = b[100] = 1;
for (i = 2; i <= n; i++)
{
mul(i); //从小到大运行,每算出一个就加上 2! 下次运算从2!*3 即3!来算。
add();
}
int m = 0;
while (b[m] == 0) m++;//去掉末尾0
for (int i = m; i <= 100; i++)
cout << b[i];
return 0;
}计数问题
int sum = 0;
int n, x,a;
cin >> n >> x;
for (int i = 1; i <=n; i++)
{
a = i;
while (a)//如果不设a=i,后面i的运算,改变i的值了,i下一次for循环从0开始。
{
if (a % 10 == x)//个位数,
sum++;
a /= 10;//个位数利用完后,将十位数变为个位数,然后再判断个位数为什么数字。
}//判断这个数每个位是什么数 一定要会这个while循环。
}
cout << sum;级数求和
int k;
double n = 0;
double i = 1;
cin >> k;
while(i++)
{
n += 1 / i;
if (n > k)
{
break;
}
}
cout << ceil(n);金币
#include<iostream>
using namespace std;
int main()
{
int i = 1,m=1,k,sum=0;
cin >> k;
while (1)
{
while (m--) //每次的m天,m为0时,下一组天数金币按m=i,即m+1来算。
{
sum += i; //m个i相加 即n天*n个金币
k--;
if (k == 0)
break;
}
i++; //金币数,每天收到i枚金币
m = i;//天数,连续m天
if (k == 0)
{
break;
}
}
cout << sum;
return 0;
}质数口袋
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main()
{
int L,sum=1,num=2;
cin >> L;
if (L == 1)
cout << "0";
else
{
for (int i = 2; num <= L; i++)
{
if (i == 2)
cout << i << endl;
bool prime = true;//注意初始化的位置
for (int j = 2; j <= i / 2 + 1; j++)
{
if (i % j == 0)//取余
{
prime = false;
break;
}
}
if (prime == true)
{
num += i;
if (num > L)
break;
cout << i << endl;
sum++;
}
}
cout << sum;
}
return 0;
}回文质数
I.判断回文数
bool isPalindrome(int x)
{
if (x < 0 || (x % 10 == 0 && x != 0)) //非0以外10的倍数肯定不是回文数。
{
return false;
}
int re = 0;
while (x > re)
{
re = re * 10 + x % 10;
x /= 10;
}
return x == re || x == re / 10;
}

图源力扣
II.判断质数
bool isprime(int y)
{
if (y != 2 && y % 2 == 0) //除2以外的偶数肯定不是质数
return false;
int n = y,sum=0;
if (isPalindrome(y) && y != 11) //除了11以外的偶数位数回文数肯定不是质数,都有11是因数
{
while (y)
{
y /= 10;
sum++;
}
if (sum % 2 == 0)
return false;
}
for (int i = 2; i <= sqrt(n); i++)
{
if (n%i == 0)
return false;
}
return true;
}#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
bool isPalindrome(int x)
{
if (x < 0 || (x % 10 == 0 && x != 0))
{
return false;
}
int re = 0;
while (x > re)
{
re = re * 10 + x % 10;
x /= 10;
}
return x == re || x == re / 10;
}
bool isprime(int y)
{
if (y != 2 && y % 2 == 0)
return false;
int n = y,sum=0;
if (isPalindrome(y) && y != 11)
{
while (y)
{
y /= 10;
sum++;
}
if (sum % 2 == 0)
return false;
}
for (int i = 2; i <= sqrt(n); i++)
{
if (n%i == 0)
return false;
}
return true;
}
int main()
{
int a, b;
cin >> a >> b;
if (b > 10000000) b = 10000000; //除了11以外的偶数位数回文数肯定不是质数。运行到10000000就可以。
for (int i = a; i <= b; i++)
{
if (isPalindrome(i))
{
if (isprime(i))
{
cout << i << endl;
}
}
}
return 0;
}小玉在游泳
#include<iostream>
using namespace std;
int main()
{
double meter=2,x;
int step=0;
cin >> x;
while (x > 0)
{
x -= meter;
meter*=0.98;
step++;
}
cout << step;
return 0;
}数字反转
注意砍掉末尾所有0
#include<iostream>
using namespace std;
int main()
{
int N,a,b,count0=1;
cin >> N;
if (N == 0)
cout << 0;
else {
if (N < 0)
{
cout << "-";
N = -N;
}
a = N;
b = N;
while (b)//判断这个数末尾有几个0;全部砍掉
{
N = b % 10;
b /= 10;
if (N == 0)
{
count0 *= 10;
}
else
break;
}
a /= count0; //砍掉末尾0
b = a;
while (b)
{
a = b;
a %= 10;
cout << a;//2
b /= 10;
}
}
return 0;
}月落乌啼算钱
- 不要被公式迷惑,不要直接用公式!!!
- 要找规律
- 题目已经说了斐波那契数列。这个数列的特点是f(n-1)+f(n-2)=f(n)
#include <stdio.h> //头文件 int main() { double f[50]; int n,i; f[0]=0; f[1]=1; f[2]=1; //递归边界条件 scanf("%d",&n); for (i=3;i<=n;i++) f[i]=f[i-1]+f[i-2]; //开始使用斐波那契数列 printf("%0.2lf",f[n]); //输出,保留两位小数 return 0; }
最长连号
#include<iostream>
using namespace std;
int main()
{
int n,serial=1,a,b,max=1;
cin >> n;
cin >> a;//第一个数
n--;
while (n--)
{
cin >> b;//其他的数,上一个数的下一个数。
if (a == b - 1)
{
serial++;
if (serial > max)
max = serial;
}
if (a != b - 1)
{
serial = 1;
}
a = b;//上一个数
}
cout << max;
return 0;
}质因数分解
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,p,a;
cin >> n;
//两个质数的乘积,这个乘积的因数只能是这两个质数与1和它本身。
//从大找小遍历的数很多,从小到大遍历找较小的约数遍历的数更少,再用n/
for (int i =2; i<=sqrt(n); i++)//判断质数
{
if (n%i == 0)//先看乘积是否可以整除
{
p = i;
break;
}
}
cout << n/p;
return 0;
}求三角形
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
int scale,row=0,a,b,newrow=0;
cin >> scale;
a = scale * scale;
for (int i = 1; i <=a ; i++)
{
printf("%02d", i);
row++;
if (row % scale == 0)
cout << endl;
}
cout << endl;
cout << setw(2 * (scale - 1)) << ""; //第二个方阵第一行01前面的空格
b = (scale + 1)*scale / 2;
row = 1;
for (int j = 1; j <= b; j++)//有一个数表示一行有几个数。另外有一个是每一行从0开始,输出一个数就++
{
printf("%02d", j);
newrow++;
if (row == newrow && row<scale)//row<scale不然会多输出一行空
{
cout << endl;
cout << setw(2 * (scale - row-1)) << "";
newrow=0;
row++;//row是一行几个数。
}
}
return 0;
}Davor
#include<iostream>
using namespace std;
int main()
{
int n, x, k;
cin >> n;
//1092k+364x=n;
for (int i = 1; i < n; i++)
{//k尽可能小,从1开始排,先排到较小的k
if ((n - 1092 * i) % 364 == 0)
{
x = (n - 1092 * i) / 364;
if (x > 100)
{
continue;
}
k = i;
break;
}
}
cout << x << endl;
cout << k << endl;
return 0;
}津津的储蓄计划
#include<iostream>
using namespace std;
int main()
{
int a[12],sum=0,rest=0;//sum为存在妈妈那里的钱
bool is = true;
for (int i = 0; i < 12; i++)
{
cin >> a[i];//a[i]表示这个月花的钱。如果把钱存给妈妈,那也算花。
}
for (int i = 0; i < 12; i++)
{
if (a[i] <= 100)//300-a[i]剩下的钱。
{
sum += 200;
a[i] += 200;//a[i]表示这个月花的钱。如果把钱存给妈妈,那也算花。
}
else if (a[i] <= 200)
{
sum += 100;
a[i] += 100;
}
rest += (300 - a[i]);//自己存的钱,
if (rest >=100)
{
//cout << "rest is >=100 "<<rest << endl;
sum += 100;
rest -= 100;//注意不只是这一个月,上一个月剩下的钱,她存到下一个月。
}
if (rest < 0)
{
//cout << "rest is <0 " << rest << endl;
cout << "-" << i+1;
is = false;
break;
}
}
if (is == true)
{
cout << sum*1.2+rest;
}
return 0;
}边栏推荐
- geotrust ov多域名ssl证书一年两千一百元包含几个域名?
- Introduction to vmware workstation and vSphere
- Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知
- Exposure X8标准版图片后期滤镜PS、LR等软件的插件
- okcc为什么云呼叫中心比传统呼叫中心更好?
- idea自動導包和自動删包設置
- Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
- [source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
- Idea autoguide package and autodelete package Settings
- Recyclerview add header
猜你喜欢

Use a mask to restrict the input of the qlineedit control

The core idea of performance optimization, dry goods sharing

How much is the tuition fee of SCM training class? How long is the study time?

Typescript practice for SAP ui5

Introduction to vmware workstation and vSphere

MySQL table insert Chinese change? Solution to the problem of No

unable to execute xxx. SH: operation not permitted

Pytoch --- use pytoch for image positioning

C language practice - binary search (half search)

Unit testing classic three questions: what, why, and how?
随机推荐
[C language] Dynamic Planning --- from entry to standing up
Li Kou interview question 02.08 Loop detection
C language practice - number guessing game
【c语言】基础篇学习笔记
Gin framework learning code
okcc为什么云呼叫中心比传统呼叫中心更好?
正大留4的主账户信息汇总
CY7C68013A之keil编译代码
Recyclerview add header
Ognl和EL表达式以及内存马的安全研究
Spring moves are coming. Watch the gods fight
[JS -- map string]
LxC limits the number of CPUs
Realize the function of data uploading
oracle 存储过程与job任务设置
Federal learning: dividing non IID samples according to Dirichlet distribution
Playing with concurrency: what are the ways of communication between threads?
二叉树解题(二)
Read "the way to clean code" - function names should express their behavior
Play with concurrency: what's the use of interruptedexception?