zoukankan      html  css  js  c++  java
  • c++ 求2100内的素数问题

    最近在学习c++,从最基础的联系

    这也是很常见的问题

    View Code
    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
        int j,i;
        int nums=0;//质数计数
        int s=0;   //质数之和
        for(i=2;i<=100;i++) { 
            bool isSushuo=true;
            for(j=2;j<i;j++){
                if(i%j==0)
                    break;
            }
            if(j==i){//说明比i小的都循环完了
                nums++;
                s+=i;
                if(nums%7==0){
                    cout<<i<<"\t"<<endl;
                }else{
                    cout<<i<<"\t";
                }

            }
        }
        cout<<endl;
        cout<<"2-100 所有的质数有:"<<nums<<""<<endl;
        cout<<"2-100 所有的质数之和为:"<<s<<endl;
        cin>>s;
        return 0;
    }

     

  • 相关阅读:
    ORACLE常用SQL优化hint语句
    TestNG 入门教程
    博客迁移
    WebMvcConfigurer
    Nginx 配置
    SpringBoot部署
    MyBatis 动态 SQL
    Spring Boot 验证表单
    Spring Boot session与cookie的使用
    Spirng MVC 重定向传递对象
  • 原文地址:https://www.cnblogs.com/clc2008/p/2382791.html
Copyright © 2011-2022 走看看