#include<iostream>
using namespace std;
double power (double x, int n){
double value = 1.0;
while (n--)
value *= x;
return value;
}
//double power (double x, int n);
int main(){
int value = 0;
cout << "Enter the number:" << endl;
for (int i = 7; i >= 0; i--){
char ch;
cin >> ch;
if (ch == '1'){
value += static_cast<int>(power(2,i));
}
}
cout << value << endl;
return 0;
}
2.回文数
#include<iostream>
using namespace std;
bool symm(int n){
int i = n;
int m = 0;
while (i != 0){
m = m*10 + i%10;
i /= 10;
}
return m == n;
}
int main(){
for (int i = 11; i <= 1000; i++){
if (symm(i*i) && symm(i*i*i) && symm(i)){
cout << i <<" " <<i*i <<" "<<i*i*i <<endl;
}
}
return 0;
}
3.求Π的值
#include<iostream>
#include<cmath>
using namespace std;
double arctan(double x){
double e = x;
double spr = -x*x;
double r = 0;
int i = 1;
while (abs(e/i) > 1e-15){
double f = e/i;
//r = (i % 4 == 1) ? r +f : r - f;
r = r + f;
e = e * spr;
i += 2;
}
return r;
}
int main(){
double a = 16.0 * arctan(1/5.0);
double b = 4.0 * arctan(1/239.0);
cout <<(a - b);
}
4.骰子比赛
#include<iostream>
#include<cstdlib>
using namespace std;
int rollDice(){
int die1 = 1 + rand()%6;
int die2 = 1 + rand()%6;
int sum = die1 + die2;
cout << "Player rolled " <<die1<< " + " << die2 << " = " <<sum<<endl;
return sum;
}
enum GameStatus{WIN, LOSE,PIAYING};
int main(){
int sum, mypoint;
GameStatus status;
unsigned seed;
cout <<"Please Enter an unsigned seed:"<<endl;
cin >> seed;
srand(seed);
sum = rollDice();
switch(sum){
case 7:
case 11:
status = WIN;
break;
case 2:
case 3:
case 12:
status = LOSE;
break;
default:
status = PIAYING;
mypoint = sum;
cout << "mypoint is "<< mypoint<<endl;
break;
}
while (status == PIAYING){
sum = rollDice();
if (sum == mypoint){
status = WIN;
}
else if (sum == 7){
status = LOSE;
}
}
if (status == WIN){
cout <<"player win"<<endl;
}
else{
cout <<"player lose"<<endl;
}
return 0;
}
5.输出星星
#include<iostream>
using namespace std;
int main(){
const int N = 4;
for (int i = 1; i <= N; i++){
for (int j = 1; j <= 20; j++){
cout << " ";
}
for (int j = 1; j <= 8-2*i; j++){
cout << " ";
}
for (int j = 1; j <= 2*i-1; j++){
cout <<"*";
}
cout << endl;
}
for (int i = 1; i <= N-1; i++){
for (int j = 1; j <= 20; j++){
cout << " ";
}
for (int j = 1; j <= 7-2*i; j++ ){
cout <<"*";
}
cout << endl;
}
return 0;
}