//思想://1、设一组数组,另其值为1;如果=1是素数,如果不是则为和数,且数组长度比要筛选的数目+1//2、分数<=N/2即可//3、如果数组[i]==1 && i%分数 == 0则修改数组[i]=0(此时i为合数)//4、输出
#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
int count =2;
int r[101];for(int i =2; i <=100;++i)
r[i]=1;while(count <=50){
for(int i = count +1; i <=100;++i){
if(r[i]==1&& i % count ==0)
r[i]=0;}for(int i = count +1; i <=100;++i){
if(r[i]==1){
count = i;break;}}}
int p =1;for(int i =2; i <=100;++i){
if(p %10==0){
printf("\n");}printf("%d ",i);++p
}printf("\n");return1;}
2.用选择法对10个整数排序
#include <stdio.h>
#include <iostream>
using namespace std;voidquicksort(int arr[],int low,int high){
if(high <= low)return;
int i,j;
i = low;
j = high +1;
int key = arr[low];while(){
while(arr[++i]< key)if(i == high)break;while(arr[--j]> key)if(j == low)break;if(i >= j)break;
int temp = arr[i];
arr[i]= arr[j];
arr[j]= temp;}
int temp = arr[low];
arr[low]= arr[j];
arr[j]= temp;quicksort(arr, low, j -1);quicksort(arr, j +1, high);}
int main(){
int a[]={
57,68,59,52,72,28,96,33,24};quicksort(a,0,sizeof(a)/sizeof(int)-1);for(int i =0; i <sizeof(a)/sizeof(int);++i){
printf("%d ",a[i]);}printf("\n");return1;}
3.求一个3*3矩阵,对角线元素之和
//想了半天这道题有没有难点,然后才发现,嗯..这道题就是很简单,无任何陷阱...
#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
int a[3][3];for(int i =0;i<3;++i){
for(int j =0; j <3;++j){
printf("please input matrix[%d][%d]",i,j);scanf("%d",&r[i][j]);}}
int main_diagonal =0;
int count_diagonal =0;
main_diagonal = a[0][0]+ a[1][1]+ a[2][2];
count_diagonal = a[0][2]+ a[1][