1.输入一个奇数n,输出对角线长为n的实心或者空心的菱形图案
如当n=5时,有:
*
***
*****
***
*
1 #include <iostream> 2 3 using namespace std; 4 5 int main(){ 6 cout<<"Please enter a odd number :"<<endl; 7 int odd_number; 8 cin>>odd_number; 9 while(odd_number%2==0){ 10 cout<<odd_number<<" is even number!Enter odd number !"<<endl; 11 cin>>odd_number; 12 } 13 int odds[odd_number/2+1]; 14 int number = 0; 15 for(int i=0,temp_odd = 1;temp_odd<=odd_number;temp_odd+=2,i++,number++){ 16 odds[i] = temp_odd; 17 odds[odd_number-i-1]=temp_odd; 18 } 19 for(int i=0;i<odd_number;i++) 20 cout<<odds[i]; 21 cout<<endl; 22 for(int i = 0;i<odd_number;i++){ 23 for(int j=0;j<(odd_number-odds[i])/2;j++) 24 cout<<'\40'; 25 for(int j=0;j<odds[i];j++) 26 cout<<"*"; 27 for(int j=0;j<(odd_number-odds[i])/2;j++) 28 cout<<'\40'; 29 cout<<endl; 30 31 } 32 return 0; 33 }
|
2.输入一个整数n(2<=n<=1e9),判断它是不是质数,
时间复杂度必须为O(n^0.5),不得为O(n)
质数就是2,以及其它有且只有两个约数的整数,
如3,5,7,11,13,17,19,23,29,31,......(以上为前10个奇质数)
最小的素数是2,如果一个数不是素数,则可以表示为a和b的乘积,并且ab一个小于开方,一个大于开方:则判断在2-sqrt之间是否有可以整除n的数,有则不是素数,没有则是素数。
1 #include <iostream> 2 3 using namespace std; 4 5 int main(){ 6 cout<<"Please enter a integer number:"<<endl; 7 int number; 8 while(cin>>number){ 9 int divisor = 0; 10 for(int i=1;i<=number;i++){ 11 if(number%i==0) 12 divisor++; 13 } 14 if(divisor==2) 15 cout<<number<<" is a prime number."<<endl; 16 else 17 cout<<number<<" is not a prime number."<<endl; 18 } 19 return 0; 20 } ~
|
1 #include <iostream> 2 #include <cmath> 3 4 using namespace std; 5 int isprime(int x){ 6 if(x<2) 7 return 0; 8 int temp = (int)sqrt(x); 9 10 for(int i=2;i<=temp;i++) 11 if(x%i==0) 12 return 0; 13 return 1; 14 } 15 int main(){ 16 cout<<"Please enter a number to check if it is a prime number."<<endl; 17 int n; 18 while(cin>>n){ 19 int flag = isprime(n); 20 if(flag==0) 21 cout<<n<<" is not prime number."<<endl; 22 else 23 cout<<n<<" is prime number."<<endl; 24 } 25 return 0; 26 27 }
|
3.输入一个奇数n,构造并输出一个n阶等和幻方,
即每一行每一列和两对角线上的n个数的和相等
如当n=5时,有(构造方法请自行搜索或者观察下表):
03 16 09 22 15
20 08 21 14 02
07 25 13 01 19
24 12 05 18 06
11 04 17 10 23
#include <iostream> #include <cmath>
using namespace std;
void magic(int n){ int i=0; int j=n/2; int p=1; int m[n][n]; for(int k = 0 ; k < n ; k ++) for(int l = 0 ; l < n ; l ++) m[k][l] = 0; while(p<=n*n){ //cout< m[i][j]=p++; if(i>=0&&i<=(n-3)&&j>=0&&j<=(n-2)){ if(m[i+2][j+1]==0){ //cout< i = i + 2; j = j + 1; } else{ i = i + 1; } } else if(i>=0&&i<=(n-3)&&j==(n-1)){ if(m[i+2][0]==0){ i = i + 2; j = 0; } else{ i = i + 1; } } else if(i==(n-2)&&j>=0&&j<=(n-2)){ if(m[0][j+1]==0){ i = 0; j = j + 1; } else{ i = i + 1; } } else if(i ==(n-1)&&j>=0&&j<=(n-2)){ if(m[1][j+1]==0){ i = 1; j = j + 1; } else{ i = 0; } } else if(i == (n-2)&&j==(n-1)){ if(m[0][0]==0){ i = 0; j = 0; } } else{ if(m[1][0]==0){ i = 1; j = 0; } } } for(int i = 0 ; i < n ; i ++){ for(int j = 0 ; j < n ; j++) cout<<m[i][j]<<'\40'; cout<<endl; } } int prime(int n){ if(n%2==0) return 0; else return 1; } int main(){ cout<<"Please enter a odd number to make a magic sqaure."<<endl; int odd_number; while(cin>>odd_number){ int flag = prime(odd_number); if(flag==0){ cout<<odd_number<<" is not a odd number . "<<endl; continue; } else magic(odd_number); } return 0; }
|
4.输入两个数a,b(1 <= a,b <= 1e5),统计a,b之间一共有多少个质数
若要通过链接里那个题目还需要优化,单纯做出本题并不难。
1 #include <iostream> 2 #include <cmath> 3 4 using namespace std; 5 6 int prime(int n); 7 8 int main(){ 9 cout<<"Please enter two number to count how much prime numbers between them."<<endl; 10 int low,high,num = 0; 11 int primes[10]; 12 13 while(cin>>low>>high){ 14 for(int i = low ; i <= high ; i++){ 15 int flag=prime(i); 16 if(0!=flag) 17 primes[num++]=i; 18 } 19 20 for(int i = 0 ; i < num ;i++) 21 cout<<primes[i]<<'\40'; 22 cout<<endl; 23 num=0; 24 } 25 return 0; 26 27 } 28 int prime(int n){ 29 int temp = (int)sqrt(n); 30 if(n<2) 31 return 0; 32 for(int i = 2 ; i <=temp ; i++){ 33 if(0==n%i) 34 return 0; 35 } 36 return 1; 37 }
|
阅读(1596) | 评论(0) | 转发(0) |