所以我正在编写这个简单的程序,使用此处的高斯算法计算任何日期的日期.
#include <iostream>
using namespace std;
//Using the Gaussian algorithm
int dayofweek(int date, int month, int year ){
int d=date;
if (month==1||month==2)
{int y=((year-1)%100);int c=(year-1)/100;}
else
{int y=year%100;int c=year/100;}
int m=(month+9)%12+1;
int product=(d+(2.6*m-0.2)+y+y/4+c/4-2*c);
return product%7;
}
int main(){
cout<<dayofweek(19,1,2054);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是一个非常简单的程序,更令人费解的是输出.
:In function dayofweek(int, int, int)’:
:19: warning: unused variable ‘y’
:19: warning: unused variable ‘c’
:21: warning: unused variable ‘y’
:21: warning: unused variable ‘c’
:23: error: ‘y’ was not declared in this scope
:25: …Run Code Online (Sandbox Code Playgroud) 我试图找出如何使用递归来执行n级嵌套for循环.例如,如果n = 3,则会有3个"级别"
for(z=0;z<6;z++){
for(y=0;y<6;y++){
for(x=0;x<6;x++){
if (z+y+x==f){
//do something
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
等等.
我似乎无法弄清楚如何将if循环放在最后一个for循环中以及如何从if语句访问先前for循环的变量.我知道变量嵌套循环的问题已经被问了很多次,我已经查看了所有这些.但似乎没有人帮助我.
有人能提出一种简单的方法来使用递归来实现这一点,记住我仍然是c ++的初学者,指出我正确的方向吗?
用例如下:
编写一个程序来输入骰子数m.程序将输出可能情况的总数,每个可能的n的可能情况的数量和具有最高概率的n.注意:只读入一个输入m.n由程序计算
例如,如果用户输入m = 2,则应输出程序
可能的病例总数为36.
可能性为
2 1
3 2
4 3
.
.
.
12 1
我在这里按照教程使用theos设置了一个简单的调整.但是,当我从这里运行带有标题的make时,我得到了
Making all for tweak WelcomeWagon...
Preprocessing Tweak.xm...
Compiling Tweak.xm...
In file included from Tweak.xm:1:
/opt/theos//include/SpringBoard/SpringBoard.h:7:26: error: UIApplication.h: No such file or directory
cc1objplus: warnings being treated as errors
In file included from /opt/theos//include/SpringBoard/UIApplicationDelegate-Protocol.h:7,
from /opt/theos//include/SpringBoard/SpringBoard.h:9,
from Tweak.xm:1:
/opt/theos//include/SpringBoard/NSObject-Protocol.h:7: warning: duplicate declaration for protocol ‘NSObject’
In file included from /opt/theos//include/SpringBoard/SpringBoard.h:9,
from Tweak.xm:1:
/opt/theos//include/SpringBoard/UIApplicationDelegate-Protocol.h:11: warning: duplicate declaration for protocol ‘UIApplicationDelegate’
/opt/theos//include/SpringBoard/UIApplicationDelegate-Protocol.h:35: error: duplicate property declaration ‘window’
make[2]: *** [obj/Tweak.xm.o] Error 1
make[1]: *** [internal-library-all_] Error 2
make: …Run Code Online (Sandbox Code Playgroud) 所以我正在尝试Euler项目的问题7.
通过列出前六个素数:2,3,5,7,11和13,我们可以看到第6个素数是13.什么是10个第001个素数?
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int a){
if (a==2||a==3){
return true;
}
if (a%2==0){
return false;
}
bool prime=true;
for (int b=2;b<sqrt(a);b++){
if (a%b==0)
prime=false;
}
if (prime==true)
return true;
else
return false;
}
int main(){
int infinite=0;
long long int primecounter=0;
for (int c=2;infinite==0;c++){
if (isPrime(c)==true){
primecounter++;
//cout<<c<<endl;
if (primecounter==10001)
{cout<<c;
break;}
}
}
return 0;}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所提出的.它适用于我测试的少数数字,如第6个素数等.但是,当我为第1000个素数运行它时,它给了我104021,答案是错误的.有人能告诉我我的代码有什么问题吗?