241102-C01-循环综合
原题链接:33265.笔记 汇总2024-11-10 13:58:53
发布于:江苏
四、程序
//从1开始报数,报到个位数是7 或者(||) 各个位数上的和加起来是7的倍数就跳过。给定n, 问跳过多少个数。
#include <iostream>
using namespace std;
int main() {
int n, ans = 0;
cin >> n;
for (int i=1; i<=n; i++) {
//tmp: 临时变量, 不能直接用i, 防止循环变量i被修改
int sum = 0, tmp = i; //sum: i的各个位上数字之和
while (tmp != 0) {
sum += tmp%10;
tmp /= 10;
}
if (i%10==7 || sum%7==0) {
ans++;
}
}
cout << ans;
return 0;
}
//取位数
#if 0
1234%10 = 4 //取出最后一位 mod
1234/10 = 123 //删除最后一位
int n; cin >> n;
while (n != 0){
cout << n%10 << endl;
n/=10;
}
#endif
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int a, b, c, i;
for (i=100; i<=999; i++){
a = i/100;
b = i/10%10;
c = i%10;
//pow(a, b) :计算a的b次方
if (pow(a,3) + pow(b, 3) + pow(c, 3) == i){
cout << i << endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
double h,s;
h=100;
s=h;
int i;
for(i=1;i<=9;i++){
s+=h;
h/=2;
}
cout<<"共经过:"<<s<<"米"<<endl;
cout<<"第十次反弹"<<h/2<<"米"<<endl;
return 0;
}
三、for 循环执行过程
二、while 循环
//#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main()
{
int day = 1;
string name;
double price, money=0, income, pay;
cout << "输入物品名称:";
cin >> name;
cout << "输入物品价格:";
cin >> price;
//条件循环
while (money <= price)
{
cout << "输入第"<<day<<"周的收入:";
cin >> income;
cout << "输入第"<<day<<"周的支出:";
cin >> pay;
money += (income - pay);
cout << "本周结余:" << money << endl;
p *= 1.1; // p = p*1.1
day ++;
}
cout << "可以购买了!";
return 0;
}
一、运算符:
(1). 根据功能分类
1.算术运算符: +,-,*,/,%
2.关系运算符: >, >=, <, <=, ==, !=
3.逻辑运算符: &&, ||, !
/*
逻辑与&&, 逻辑或||, 逻辑非! //(逻辑值, 0, 1)
and or not
1 && 1 = 1
1 || 0 = 1
!0 = 1
!1 = 0
*/
4.自增自减运算符, ++, --
int i = 1;
// (++i)使用之前+1,
// (i++)使用之后+1,
cout << (++i) << endl; //2
cout << i << endl; //2
5.复合运算符: +=, -=, *=, /=, %=
零、数据类型: 整数, 小数, 字符, 字符串
整数类型:(整型)
short 短整型 小杯
int 整型 中杯
// long 长整型
long long 长长整型 大杯
// long long a = 2147483648;
// cout << a << endl;
字符:
char
字符串: string
// char _ = 'i'; //字符
// cout << _ << endl;
//
// string s = "iiiii"; //字符串
// cout << s << endl;
小数:
double pi = 3.1415926; //双精度小数
float //单精度小数
bool T/F
#endif
这里空空如也
有帮助,赞一个