当前位置:网站首页>洛谷入门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;
}
边栏推荐
- LeetCode-归并排序链表
- 缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性
- Message mechanism -- message processing
- Pytorch---使用Pytorch实现U-Net进行语义分割
- MySQL table insert Chinese change? Solution to the problem of No
- Typescript practice for SAP ui5
- Fluent icon demo
- 万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
- unable to execute xxx. SH: operation not permitted
- Pytoch --- use pytoch to predict birds
猜你喜欢
Landing guide for "prohibit using select * as query field list"
Learn what definitelytyped is through the typescript development environment of SAP ui5
Let genuine SMS pressure measurement open source code
C language practice - binary search (half search)
Keil compilation code of CY7C68013A
6月书讯 | 9本新书上市,阵容强大,闭眼入!
How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of
Ten thousand volumes are known to all, and one page of a book is always relevant. TVP reading club will take you through the reading puzzle!
unable to execute xxx. SH: operation not permitted
Federal learning: dividing non IID samples according to Dirichlet distribution
随机推荐
Mysql表insert中文变?号的问题解决办法
千亿市场规模医疗美容行业的水究竟有多浑?
June book news | 9 new books are listed, with a strong lineup and eyes closed!
idea自動導包和自動删包設置
Sword finger offer II 006 Sort the sum of two numbers in the array
UNET deployment based on deepstream
C language guessing numbers game
Pytorch-Yolov5从0运行Bug解决:
6月书讯 | 9本新书上市,阵容强大,闭眼入!
Learn what definitelytyped is through the typescript development environment of SAP ui5
Geotrust OV Multi - Domain Domain SSL Certificate rmb2100 per year contains several Domain names?
LeetCode-对链表进行插入排序
万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
Typescript practice for SAP ui5
60后关机程序
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
cookie、session、tooken
第十六周作业
What methods should service define?
There is no prompt for SQL in idea XML, and the dialect setting is useless.