如何使用printf打印此示例输出?
******************************************************************************
** XYZ Corporation Date: March 27, 1989(Use current date)**
** 999 John Doe street **
** Ypsilanti, MI. 48197. **
** **
** Pay to the order of: ? **
** The amount of: ? dollars, and ? cents **
** **
** signed: **
** President, XYZ Corporation. **
** **
**--------------------------------------------------------------------------**
** SUMMARY **
** Social security number: ? **
** Regular pay: ? **
** Overtime pay: ? **
** Gross pay: ? **
** …Run Code Online (Sandbox Code Playgroud) 好吧,我在三元运算符中有一个关于逗号的问题.剪掉垃圾,代码如下:
void test_comma_in_condition(void)
{
int ia, ib, ic;
ia = ib = ic = 0;
bool condition=true;
cout<<"Original:"<<endl;
cout<<"ia: "<<ia<<endl;
cout<<"ib: "<<ib<<endl;
condition?(ia=1, ib=2):(ia=11, ib=12);
cout<<"After:"<<endl;
cout<<"ia: "<<ia<<endl;
cout<<"ib: "<<ib<<endl;
ia = ib = ic = 0;
condition?ia=1, ib=2, ic=3:ib=22,ia=21, ic=23;
cout<<"The operation must be bracketed, or you'll see..."<<endl;
cout<<"ia: "<<ia<<endl;
cout<<"ib: "<<ib<<endl;
cout<<"ic: "<<ic<<endl;
condition?ia=1, ib=2, ic=3:ia=21, ib=22, ic=23;
cout<<"The operation must be bracketed, or you'll see..."<<endl;
cout<<"ia: "<<ia<<endl;
cout<<"ib: "<<ib<<endl;
cout<<"ic: "<<ic<<endl;
return;
}
Run Code Online (Sandbox Code Playgroud)
输出将如下:
Original:
ia: 0 …Run Code Online (Sandbox Code Playgroud) 是否可以在 C++ 中进行 while 循环,直到小数达到一定精度?我想对 arctan 和 pi 进行长时间的计算,并且我希望循环运行,直到使用公式 pi=4(arctan (1.0)) 将 pi 计算到小数点后第 10 位。我正在使用泰勒级数公式手动计算反正切。我知道这些计算有内置函数,但这是一项家庭作业,所以我必须这样做。我不是在寻找问题的解决方案,只是寻找使用精度的循环是否可行。谢谢!
编辑:
我仍然坚持这个!即使在仔细阅读了每个人的提示之后,我也无法为 while 循环提出正确的论点。真的需要帮助。这是我想出的代码:
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
int i;
long double result;
long double pi;
int y=3;
int loopcount=0;
long double precision;
cout<<"Start\n";
result=1-(pow(1,y)/y);
do
{
y=y+2;
result=result+(pow(1,y)/y);
y=y+2;
result=result-(pow(1,y)/y);
pi=4*(result);
precision=(pi*(pow(10,11))/10);
loopcount++;
}
while(//This is the problem!);
cout<<"Final Arctan is:"<<endl;
cout<<setprecision(20)<<result<<endl;
cout<<"Final Pi is:"<<endl;
cout<<setprecision(9)<<pi1<<endl;
cout<<"Times looped:"<<endl;
cout<<loopcount<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 下面的引号是我的实际作业,下面是我当前的代码.有人能指出我正确的方向.
编写一个名为multiplyEvens的递归方法,它返回前n个偶数整数的乘积.例如,multiplyEvens(1)返回2,multiplyEvens(4)返回384(因为2*4*6*8 = 384).如果传递的值小于1,则抛出IllegalArgumentException.
private static int multiplyEvens(int n)
{
if(n%2 == 0)
{
System.out.println(n*n);
return multiplyEvens(n*n);
// I'm lost
}
System.out.println();
return n;
}
Run Code Online (Sandbox Code Playgroud) 我有一个作业,我需要计算输入中的对数.
这是我到目前为止:
(define x 0)
(define number-of-pairs
(lambda (v)
(if (pair? v)
(+ x 1)
(+ x 0))))
Run Code Online (Sandbox Code Playgroud)
然后我用它如下:
(number-of-pairs (cons (cons 'a 'b) 'c))
Run Code Online (Sandbox Code Playgroud)
它应该产生2,但它产生1,因为它只通过函数一次.如果我试试
(number-of-pairs 10)
Run Code Online (Sandbox Code Playgroud)
因为没有对,所以它会产生0.
我有一个大学powerpoint幻灯片声称做了一个继承的总和
1/i ^ 2
索引"i"从0到2260,不同于做相同数字的总和但从最大到最小(从1/2260 ^ 2到1/0 ^ 2).
试图在C上执行此操作,我有以下结果:
Increasing Order Sum: 1.644491e+00.
Decreasing Order Sum: 1.644491e+00.
Relative Error (abs(Incr. Sum - Decr. Sum) / abs(Incr. Sum)) : 2.700465e-15.
Run Code Online (Sandbox Code Playgroud)
有人能解释我为什么会这样吗?我对这些幻灯片没有解决方案.
提前致谢!:)
安排活动的最佳方式是什么?
我正在考虑新的日历 API,但它至少需要互联网连接才能创建新日历。
是否有任何 API 可以创建不需要互联网连接的时间表?
我正在为它制作一个虚拟机和一个C编译器.我没有得到的是大多数指令集有IMUL和IDIV但不是ISUB和IADD.由于sub通常是unsigned sub,我怎么能像x86那样做
MOV EAX,1
MOV EDX,3
SUB EAX,EDX,EAX
Run Code Online (Sandbox Code Playgroud)
得到-2?
在乌龟和野兔算法中,为什么总是让兔子前进两步,然后向前前进1步,然后将它们进行比较,我们也可以使兔子前进1步,然后再次检查其是否相等,然后再增加乌龟和野兔检查它们是否相等!我认为这将有助于更快地找到循环?
例如。这个伪指令
tortoise := firstNode
hare := firstNode
forever:
if hare == end
return 'No Loop Found'
hare := hare.next
if hare == end
return 'No Loop Found'
if hare==tortoise
return true
hare = hare.next
tortoise = tortoise.next
if hare == tortoise
return 'Loop Found'
Run Code Online (Sandbox Code Playgroud)
提前致谢
我可能犯了一个非常明显的错误,但我看不到它,我希望有人可以帮助我.
我想在我的C++程序中将2个字符串添加到对向量中.容器看起来像vector< pair < word , type > > vec.这个词可以是任何东西,类型是名词,形容词,动词等.例如< Hogwarts , noun>,< Hungry, adjective>等等.
这是我在单词bank中添加单词的地方.FileStrings只是从File I/O创建的另一个容器.这里一切都很好.
for(unsigned int i = 0; i < fileStrings.size(); ++i)
{
cout << "Adding " << fileStrings[i] << ", " << fileStrings[i + 1] << endl;
theWordBank.addWord(fileStrings[i], fileStrings[++i]);
}
Run Code Online (Sandbox Code Playgroud)
这是我迷路的地方.打印出newWord时,它与newType完全相同.我无法理解它,我不明白newWord如何从上面的代码片段中失去它的价值.
void addWord(const string& newWord, const string& newType)
{
mWords.push_back(make_pair(newWord, newType));
std::cout << "newWord " << newWord << ", " << newType << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
再一次,结果应该是类似的< Hogwarts, noun> …