本文将为您提供关于近似计算π的详细介绍,我们还将为您解释for循环的相关知识,同时,我们还将为您提供关于99乘法表(for循环嵌套)、C–是否可以从for循环中减少for循环的最大值?、C++11新功
本文将为您提供关于近似计算π的详细介绍,我们还将为您解释for循环的相关知识,同时,我们还将为您提供关于99乘法表(for循环嵌套)、C – 是否可以从for循环中减少for循环的最大值?、C++11新功能介绍-for循环(基于范围的循环)详解、CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)的实用信息。
本文目录一览:- 近似计算π(for循环)(for循环流程图)
- 99乘法表(for循环嵌套)
- C – 是否可以从for循环中减少for循环的最大值?
- C++11新功能介绍-for循环(基于范围的循环)详解
- CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)
近似计算π(for循环)(for循环流程图)
【问题描述】
根据公式π/4=1-1/3+1/5-1/7+… ,直到最后一项小于10^-6。计算并输出π的值。
【输入形式】
无输入
【输出形式】
π的值
【输出样例】
3.14159
【编程要求】
请用for循环实现
代码1(期末写的新版本):
#include <iostream>
using namespace std;
int main()
{
int Operator = 1;
double x, sum = 0;
for( int i=1; 1.0/i > 1e-6; i += 2, Operator *= -1 )
{
x = Operator*1.0/i;
sum += x;
}
cout << sum*4 << endl;
return 0;
}
代码2(三个月前初学时写的代码):
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float x = 1, sum = 0;
int sign = -1;
for( int i=3; fabs(x) > 1e-6; i += 2 )
{
sum += x;
x = (float)sign/i;
sign *= -1;
}
cout << 4*sum << endl;
}
99乘法表(for循环嵌套)
计算乘法表
- 两个数相乘,外层循环代表被乘数,控制行数;内层代表乘数,控制列数。
- 循环嵌套,变量名不可以重复。
- 使用 break 语句让输出的乘法表更简洁。
- 使用 String 变量,做 String 的加法。
public class MultiTabble {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) { // 控制行 被乘数
String line = "";
for (int j = 1; j <= 9; j++) { // 控制列 乘数
line += i + "*" + j + "=" + i * j + "\t"; // 利用字符串的加法将结果连接起来
}
System.out.println(line);
}
}
}
我们发现运算结果,有大量的重复结果,例如1*2和2*1、1*3和3*1等等。
这是不符合乘法口诀表的摆放规则的。
那么这时我们就要想办法把重复的等式去掉。
通过观察,在 i * j 的形式下,只要去掉 j 大于 i 的等式即可满足要求。
如何实现呢?
利用 break 语句跳出循环的这一特性来实现:当发现 j > i 时,就会跳出当前一层的循环(也就是这里的关于 j 的内循环),到达外层循环(关于 i 的外循环)。
public class MultiTabble {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) { // 控制行 被乘数
String line = "";
for (int j = 1; j <= 9; j++) { // 控制列 乘数
if (j > i) {
break; // 利用break跳出循环的操作解决重复问题
}
line += i + "*" + j + "=" + i * j + "\t"; // 利用字符串的加法将结果连接起来
}
System.out.println(line);
}
}
}
除了上面的一种方法以外,还有一个更加简单的方法实现乘法表。
就是让内循环中的变量 j 只要小于等于外循环的 i 即可。
这样内循环就不会超过外循环的最大数值,
例如,i 的值为 5,j 的值就只能是1,2,3,4,5. 这就解决了等式重复的问题。
public class MultiTable2 {
public static void main(String[] args) {
for(int i = 1; i <= 9; i++) {
String line = "";
for( int j = 1; j <= i; j++) {
line += i + "*" + j + "=" + (i*j) + "\t";
}
System.out.println(line);
}
}
}
C – 是否可以从for循环中减少for循环的最大值?
void decrement(int counter) { counter--; } int counter = 20; for (int i = 0; i < counter; i++) { for (int j = 0; j < counter,j++) { decrement(counter); } }
理想情况下,我希望看到的是每次运行for循环时计数器var都会递减,因此它运行的次数少于20次.但gdb显示在decrement()计数器内递减,但返回到for循环计数器实际上保持不变.
解决方法
for (int i = 0; i < counter; i++) { counter--; }
它在您的示例中不起作用的原因是因为您传递值并修改值的副本.如果您传递指向变量的指针而不是仅传递值,它将起作用.
C++11新功能介绍-for循环(基于范围的循环)详解
98写法:
for(表达式 1; 表达式 2; 表达式 3){
//循环体
}
11写法:
for (declaration : expression){
//循环体
}
声明+表达式+循环体
declaration:表示此处要定义一个变量,该变量的类型为要遍历序列中存储元素的类型。需要注意的是,C++ 11 标准中,declaration参数处定义的变量类型可以用 auto 关键字表示,该关键字可以使编译器自行推导该变量的数据类型。
expression:表示要遍历的序列,常见的可以为事先定义好的普通数组或者容器,还可以是用 {} 大括号初始化的序列。
char arc[] = "http://c.biancheng.net/cplus/11/";
//for循环遍历普通数组
for (char ch : arc) {
cout << ch;
}
cout << ''!'' << endl;
vector<char>myvector(arc, arc + 23);
//for循环遍历 vector 容器
for (auto ch : myvector) {
cout << ch;
}
cout << ''!'';
疑问如果if ch==多少做操作,如何知道下标。
---{}写法
for (int num : {1, 2, 3, 4, 5}) {
cout << num << " ";
}
---------遍历过程修改值
int main() {
char arc[] = "abcde";
vector<char>myvector(arc, arc + 5);
//for循环遍历并修改容器中各个字符的值
for (auto &ch : myvector) {
ch++;
}
}
。。。大多数golang java已经支持类似写法
CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)
You are given a function ff written in some basic language. The function accepts an integer value, which is immediately written into some variable xx. xx is an integer variable and can be assigned values from 00 to 232−1232−1. The function contains three types of commands:
- for nn — for loop;
- end — every command between "for nn" and corresponding "end" is executed nntimes;
- add — adds 1 to xx.
After the execution of these commands, value of xx is returned.
Every "for nn" is matched with "end", thus the function is guaranteed to be valid. "for nn" can be immediately followed by "end"."add" command can be outside of any for loops.
Notice that "add" commands might overflow the value of xx! It means that the value of xx becomes greater than 232−1232−1 after some "add" command.
Now you run f(0)f(0) and wonder if the resulting value of xx is correct or some overflow made it incorrect.
If overflow happened then output "OVERFLOW!!!", otherwise print the resulting value of xx.
Input
The first line contains a single integer ll (1≤l≤1051≤l≤105) — the number of lines in the function.
Each of the next ll lines contains a single command of one of three types:
- for nn (1≤n≤1001≤n≤100) — for loop;
- end — every command between "for nn" and corresponding "end" is executed nntimes;
- add — adds 1 to xx.
Output
If overflow happened during execution of f(0)f(0), then output "OVERFLOW!!!", otherwise print the resulting value of xx.
Examples
9
add
for 43
end
for 10
for 15
add
end
add
end
161
2
for 62
end
0
11
for 100
for 100
for 100
for 100
for 100
add
end
end
end
end
end
OVERFLOW!!!
Note
In the first example the first "add" is executed 1 time, the second "add" is executed 150 times and the last "add" is executed 10 times. Note that "for nn" can be immediately followed by "end" and that "add" can be outside of any for loops.
In the second example there are no commands "add", thus the returning value is 0.
In the third example "add" command is executed too many times, which causes xx to go over 232−1232−1.
题意:
给出for循环伪代码,计算执行add的次数(加了几次)。
思路:
很新颖的一道题,不难想到使用栈模拟。
因为栈底的值会影响栈顶的结果,因此放入累乘值来表示当前的状态,每次add只需加入栈顶的值即可。
具体实现见代码,注意判断越界。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string s;
stack<ll> st;
int main()
{
int t,i;
ll x=0,y=0;
scanf("%d",&t);
st.push(1);
int f=0;
while(t--){
scanf(" ");
cin>>s;
if(s[0]==''f''){
scanf("%I64d",&x);
st.push(min(4294967296ll,x*st.top()));
}
else if(s[0]==''a''){
y+=st.top();
if(y>=4294967296){
f=1;
break;
}
}
else if(s[0]==''e''){
st.pop();
}
}
if(f==1) printf("OVERFLOW!!!\n");
else printf("%I64d\n",y);
return 0;
}
关于近似计算π和for循环的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于99乘法表(for循环嵌套)、C – 是否可以从for循环中减少for循环的最大值?、C++11新功能介绍-for循环(基于范围的循环)详解、CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)的相关知识,请在本站寻找。
本文标签: