辽宁师范大学 • 张大为@https://daweizh.github.io/noip/

1.5 编程基础之循环控制(45)

  1. 求平均年龄http://noi.openjudge.cn/ch0105/01/
    #include <iostream>
    using namespace std;
    
    int main(){
      
        int n,a;
        double sum = 0;
        cin >> n;
        for (int i=0;i<n;i++){
            cin >> a;
            sum = sum + a;	
        }
        
        printf("%.2f",sum/n);	
    }
    
  2. 财务管理http://noi.openjudge.cn/ch0105/02/
    #include <iostream>
    using namespace std;
    
    int main(){
      
        double a;
        double sum = 0;
        for (int i=0;i<12;i++){
            cin >> a;
            sum = sum + a;	
        }
        
        printf("$%.2f",sum/12);	
    }
    
  3. 均值http://noi.openjudge.cn/ch0105/03/
    #include <iostream>
    using namespace std;
    
    int main(){
      
        int n;
        double a;
        double sum = 0;
        cin >> n;
        for (int i=0;i<n;i++){
            cin >> a;
            sum = sum + a;	
        }
        
        printf("%.4f",sum/n);	
    }
    
  4. 求整数的和与均值http://noi.openjudge.cn/ch0105/04/
    #include <iostream>
    using namespace std;
    
    int main(){
      
        int n,a,sum = 0;
        cin >> n;
        for (int i=0;i<n;i++){
            cin >> a;
            sum = sum + a;	
        }
        printf("%d %.5f",sum,(double)sum/n);	
        return 0;
    }
    
  5. 最高的分数http://noi.openjudge.cn/ch0105/05/
    #include <iostream>
    using namespace std;
    
    int main(){
      
        int n,a,max = 0;
        cin >> n;
        for (int i=0;i<n;i++){
            cin >> a;
            if (a>max){
                max = a;
            }	
        }
        printf("%d",max);	
        return 0;
    }
    
  6. 整数序列的元素最大跨度值http://noi.openjudge.cn/ch0105/06/
    #include <iostream>
    using namespace std;
    
    int main(){
      
        int n,a,max = 0,min=10000;
        cin >> n;
        for (int i=0;i<n;i++){
            cin >> a;
            if (a>max){
                max = a;
            }
            if(a<min){
                min = a;
            }
        }
        printf("%d",max-min);	
        return 0;
    }
    
  7. 奥运奖牌计数http://noi.openjudge.cn/ch0105/07/
    #include <iostream>
    using namespace std;
    
    int main(){
      
        int n,g,s,c,sg=0,ss=0,sc=0;
        cin >> n;
        for (int i=0;i<n;i++){
            cin >> g >> s >> c;
            sg = sg + g;
            ss = ss + s;
            sc = sc + c; 
        }
        printf("%d %d %d %d",sg,ss,sc,sg+ss+sc);	
        return 0;
    }
    
  8. 多边形内角和http://noi.openjudge.cn/ch0105/08/
    #include <iostream>
    using namespace std;
    
    int main(){
      
        int n,a,sum=0;
        cin >> n;
        for (int i=1;i<n;i++){
            cin >> a;
            sum = sum + a;
        }
        printf("%d",(n-2)*180-sum);	
        return 0;
    }
    
  9. 奇数求和http://noi.openjudge.cn/ch0105/09/
    #include <iostream>
    using namespace std;
    
    int main(){
        int m,n;
        int sum = 0;
    
        cin >> m >> n;
        for(int i=m;i<=n;i++){
            if(i%2==1){
                sum = sum + i;
            }
        }	
        cout << sum << endl;
        
        return 0;
    }
    
  10. 满足条件的数累加http://noi.openjudge.cn/ch0105/10/
    #include <iostream>
    using namespace std;
    
    int main(){
      
        int m,n,sum=0;
        cin >> m >> n;
        for (int i=m;i<=n;i++){
            if(i%17==0){
                sum = sum + i;
            }
        }
        printf("%d",sum);	
    
        return 0;
    }
    
  11. 整数的个数http://noi.openjudge.cn/ch0105/11/
    #include <iostream>
    using namespace std;
    
    int main(){
      
        int n,a,s1=0,s5=0,s10=0;
        cin >> n;
        for (int i=0;i<n;i++){
            cin >> a;
            if(a==1){
                s1 = s1 + 1;
            }
            if(a==5){
                s5 = s5 + 1;
            }
            if(a==10){
                s10 = s10 + 1;
            }
        }
        cout << s1 << endl;	
        cout << s5 << endl;	
        cout << s10 << endl;	
    
        return 0;
    }
    
  12. 与指定数字相同的数的个数http://noi.openjudge.cn/ch0105/12/
    #include <iostream>
    using namespace std;
    
    int main(){
        int N,m,a,sum=0;
    
        cin >> N >> m;
        for (int i=0;i<N;i++){
            cin >> a;
            if(a==m){
                sum = sum + 1;
            }
        }
        cout << sum << endl;	
    
        return 0;
    }
    
  13. 乘方计算http://noi.openjudge.cn/ch0105/13/
    #include <iostream>
    using namespace std;
    
    int main(){
        int a,n,mul=1;
    
        cin >> a >> n;
        for(int i=0;i<n;i++){
            mul = mul * a;
        }
        cout << mul << endl;
            
        return 0;
    }
    
  14. 人口增长问题http://noi.openjudge.cn/ch0105/14/
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
        double x,n;
    
        cin >> x >> n;
        printf("%.4f",x*pow((1+0.001),n));
            
        return 0;
    }
    
  15. 银行利息http://noi.openjudge.cn/ch0105/15/
    #include <iostream>
    using namespace std;
    
    int main(){
        double R,M,Y;
        
        cin >> R >> M >> Y;
        for (int i=0;i<Y;i++){
            M = M * (1 + R/100);
        }
        printf("%d",(int)M);
        
        return 0;
    } 
    
  16. 买房子http://noi.openjudge.cn/ch0105/16/
    #include <iostream>
    using namespace std;
    
    int main(){
        double sumn,hp=200,n,k;
        int m=1;
        cin >> n >> k;
        sumn = n;
        while(sumn<hp && m <40){
            sumn = sumn + n;
            hp = hp * (1+k/100);
            m = m + 1;
        }
        
        if(m<=20)
            cout << m << endl;
        else
            cout << "Impossible" << endl;
        
        return 0;
    }
    
  17. 菲波那契数列http://noi.openjudge.cn/ch0105/17/
    #include <iostream>
    using namespace std;
    
    int main(){
        int k,first=1,second=1,fb;
        
        cin >> k;
        if (k > 2)
            for (int i=3;i<=k;i++){
                fb = first + second;
                first = second;
                second = fb;
            }
        else
            fb = 1;
        cout << fb << endl;
            
        return 0;
    } 
    
  18. 鸡尾酒疗法http://noi.openjudge.cn/ch0105/18/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n;
        double a[20][2];
        
        cin >> n;
        for (int i=0;i<n;i++){
            cin >> a[i][1] >> a[i][2];
        }
        
        double x = a[0][2] / a[0][1] * 100;
        for(int i=1;i<n;i++){
            double y = a[i][2] / a[i][1] * 100;
            if (y-x>5) {
                cout << "better" << endl;	
            }else if (x-y>5){
                cout << "worse" << endl;	
            }else{
                cout << "same" << endl;	
            }
        }
            
        return 0;
    } 
    
  19. 救援http://noi.openjudge.cn/ch0105/19/
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
        int n;
        double a,b,c,t=0;
        
        cin >> n;
        for(int i=0;i<n;i++){
            cin >> a >> b >> c;
            t = t + sqrt(a*a+b*b) * 2 / 50 + 1.5 * c;
        }
        printf("%.0f",ceil(t));
        
        return 0;	
    } 
    
  20. 球弹跳高度的计算http://noi.openjudge.cn/ch0105/20/
    #include <iostream>
    using namespace std;
    
    int main(){
        double h,sum=0;
        
        cin >> h;
        
        sum = h;
        for(int i=1;i<10;i++){
            sum = sum + h;
            h = h / 2;
        }
        cout << sum << endl;
        cout << h/2 << endl;
        
        return 0;	
    } 
    
  21. 角谷猜想http://noi.openjudge.cn/ch0105/21/
    #include <iostream>
    using namespace std;
    
    int main(){
        long long int N;
        cin >> N;
        
        while(N>1){
            if(N%2==1){
                cout << N << "*3+1=" << N * 3 + 1 << endl;
                N = N * 3 + 1;
            }else{
                cout << N << "/2=" << N / 2 << endl;
                N = N / 2; 
            } 
        }
        cout << "End";
        
        return 0;	
    } 
    
  22. 津津的储蓄计划http://noi.openjudge.cn/ch0105/22/
    #include <iostream>
    using namespace std;
    
    int main(){
        long p,a,r =0,sum=0;
    
        for(int i=0;i<12;i++){
            r = r + 300;
            cin >> p;
            r = r - p;
            a = r/100;
            sum = sum + a * 100;
            r = r - a * 100;
            if (r<0){
                cout << "-" << i+1 << endl;
                break;
            }	 
        }
        if (r>0)
            cout << sum * 1.2 + r << endl;
        
        return 0;	
    } 
    
  23. 药房管理http://noi.openjudge.cn/ch0105/23/
    #include <iostream>
    using namespace std;
    
    int main(){
        long m,n,a,sum=0;
        
        cin >> m >>n;
        for(int i=0;i<n;i++){
            cin >> a;
            if (m -a >= 0)
                m = m - a;
            else
                sum = sum + 1;
        }
        cout << sum  << endl;
        
        return 0;	
    } 
    
  24. 正常血压http://noi.openjudge.cn/ch0105/24/
    #include <iostream>
    using namespace std;
    
    int main(){
        long n,a,b,sum=0,max=0;
        
        cin >> n;
        for(int i=0;i<n;i++){
            cin >> a >> b;
            if (a>=90 && a<=140 && b >=60 && b <=90){
                sum = sum + 1;		
            }else{
                if (sum > max)
                    max = sum;
                sum = 0;
            }
        }
        if (sum > max) max = sum;
        cout << max  << endl;
        
        return 0;	
    } 
    
  25. 求特殊自然数http://noi.openjudge.cn/ch0105/25/
    #include <iostream>
    using namespace std;
    
    int main(){
        int i,j,k;
        
        bool flag = false;
        for(i=0;i<7;i++){
            for(j=0;j<7;j++){
                for(k=0;k<7;k++){
                    if (i*9*9+j*9+k==i+j*7+k*7*7 && (i!=0 || j!=0 || k!=0)){
                        cout << i*9*9+j*9+k  << endl;
                        cout << k << j << i << endl;
                        cout << i << j << k << endl; 
                    }
                }
            }
        }
        
        return 0;	
    } 
    
  26. 统计满足条件的4位数个数http://noi.openjudge.cn/ch0105/26/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n,a,sum=0;
        int first,second,third,forth;
        
        cin >> n;
        for(int i=0;i<n;i++){
            cin >> a;
            forth = a % 10;
            third = (a - forth)/10 % 10;
            second = (a-forth - third * 10)/100 % 10;
            first = (a - forth - third*10 - second * 100)/1000;
            if (forth - first - second - third > 0){
                sum = sum + 1;
            }
        }
        cout << sum << endl;
        
        return 0;
    } 
    
  27. 级数求和http://noi.openjudge.cn/ch0105/27/
    #include <iostream>
    using namespace std;
    
    int main(){
        long K,n=0;
        double sn = 0;
        
        cin >> K;
        while(sn<=K){
            n = n + 1;
            sn = sn + 1.0d/n;
        }
        cout << n << endl;
            
        return 0;
    } 
    
  28. 分离整数的各个数位http://noi.openjudge.cn/ch0105/28/
    #include <iostream>
    using namespace std;
    
    int main(){
        long long int n,r;
        
        cin >> n;
        while(n>0){
            r = n % 10;
            cout << r;
            n = (n - r)/10;
            if (n>0){
                cout << " ";
            } 
        }
        cout << endl;
              
        return 0;
    } 
    
  29. 数字反转http://noi.openjudge.cn/ch0105/29/
    #include <iostream>
    using namespace std;
    
    int main(){
        long long int n,i=0;
        
        cin >> n;
        while(n!=0){
            i = i*10 + n % 10;
            n = n / 10;
        }
        printf("%d",i);
        
        return 0;
    } 
    
  30. 含k个3的数http://noi.openjudge.cn/ch0105/30/
    #include <iostream>
    using namespace std;
    
    int main(){
        long m,k,sum=0;
        
        cin >> m >> k;
        if (m % 19 ==0){
            while(m!=0){
                if (m % 10==3){
                    sum = sum + 1;
                }
                m = m / 10;
            }
            if (sum==k)
                cout << "YES" << endl;
            else
                cout << "NO" << endl;
        }else
            cout << "NO" << endl;
        
        return 0;
    } 
    
  31. 开关灯http://noi.openjudge.cn/ch0105/31/
    #include <iostream>
    using namespace std;
    
    int main(){
        long N,M;
        bool light = true;
    
        cin >> N >> M;
        cout << 1 ;
        for(int i=2;i<=N;i++){
            light = true;	
            for(int j=1;j<=M;j++){
                if(i%j==0){
                    light = ! light;
                }
            }
            if(!light)
                cout << "," << i;
        }
        
        return 0;
    } 
    
  32. 求分数序列和http://noi.openjudge.cn/ch0105/32/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n;
        double p=1,q=2,sum = 0,temp;
    
        cin >> n;
        for (int i=0;i<n;i++){
            sum = sum + q / p;
            temp = q;
            q = q + p;
            p = temp;
        }
        printf("%.4f",sum);
          
        return 0;
    } 
    
  33. 计算分数加减表达式的值http://noi.openjudge.cn/ch0105/33/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n;
        double sum = 0;
    
        cin >> n;
        for (int i=1;i<=n;i++){
            if(i%2==1)
                sum = sum + 1.0d/i;
            else
                sum = sum - 1.0d/i;
        }
        printf("%.4f",sum);
          
        return 0;
    } 
    
  34. 求阶乘的和http://noi.openjudge.cn/ch0105/34/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n;
        long long int sum = 0,product=1;
    
        cin >> n;
        for (int i=1;i<=n;i++){
            product = 1;
            for(int j=1;j<=i;j++){
                product = product * j;
            }
            sum = sum + product;
        }
        cout << sum << endl; 
          
        return 0;
    } 
    
  35. 求出e的值http://noi.openjudge.cn/ch0105/35/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n;
        double e = 1,p;
        cin >> n;
        for (int i=1;i<=n;i++){
            p = 1;
            for(int j=1;j<=i;j++){
                p = p * j;
            }
            e = e + 1/p;
        }
        
        printf("%.10f",e);	
    }
    
  36. 计算多项式的值http://noi.openjudge.cn/ch0105/36/
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
        long int n;
        double x,f = 1,p;
        cin >> x >> n ;
        f = f + x;
        for (int i=2;i<=n;i++){
            f = f + pow(x,i);
        }
        
        printf("%.2f",f);	
    }
    
  37. 雇佣兵http://noi.openjudge.cn/ch0105/37/
    #include <iostream>
    using namespace std;
    
    int main(){
        int M,N,X;
        
        cin >> M >> N >> X ;
        while(N <=M && X * N >=M){
            if (M%N==0){
                X = X - M / N;
            } else {
                X = X - M / N -1;
            }
            N = N + M/N;
        }
        printf("%d",N);
        
        return 0;	
    }
    
  38. 计算多项式的导函数http://noi.openjudge.cn/ch0105/38/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n,c[100];
        
        cin >> n ;
        for(int i=n;i>=0;i--){
            cin >> c[i];
            c[i] = c[i] * i;
        }
        
        cout << c[n];
        for(int i=n-1;i>0;i--){
            cout << " " << c[i];
        }
        cout << endl;
        
        return 0;	
    }
    
  39. 与7无关的数http://noi.openjudge.cn/ch0105/39/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n,sum=0,a;
        
        cin >> n ;
        for(int i=1;i<=n;i++){
            if(i%7!=0){
                a = i;
                while(a>0){
                    if (a % 10 == 7){
                        break;					
                    }
                    a = a / 10;	
                }
                if (a ==0){
                    sum = sum + i * i;
                }
            }
        }
        cout << sum << endl;
        
        return 0;	
    }
    
  40. 数1的个数http://noi.openjudge.cn/ch0105/40/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n,sum=0,a;
        
        cin >> n ;
        for(int i=1;i<=n;i++){
            a = i;
            while(a>0){
                if (a % 10 == 1){
                    sum = sum + 1;
                }
                a = a / 10;	
            }
        }
        cout << sum << endl;
        
        return 0;	
    }
    
  41. 数字统计http://noi.openjudge.cn/ch0105/41/
    #include <iostream>
    using namespace std;
    
    int main(){
        int L,R,sum=0,a;
        
        cin >> L >> R ;
        for(int i=L;i<=R;i++){
            a = i;
            while(a>0){
                if (a % 10 == 2){
                    sum = sum + 1;
                }
                a = a / 10;	
            }
        }
        cout << sum << endl;
        
        return 0;	
    }
    
  42. 画矩形http://noi.openjudge.cn/ch0105/42/
    #include <iostream>
    using namespace std;
    
    int main(){
        int h,w,fill;
        char c;
        
        cin >> h >> w >> c >> fill ;
        for (int i=0;i<w;i++) cout << c;
        cout << endl;
        
        for (int i=0;i<h-2;i++){
            cout << c;
            for(int j=0;j<w-2;j++)
                if (fill==0) 
                    cout << " ";
                else
                    cout << c;
            cout << c << endl;
        }
        for (int i=0;i<w;i++) cout << c;
        cout << endl;
    
        return 0;	
    }
    
  43. 质因数分解http://noi.openjudge.cn/ch0105/43/
    #include <iostream>
    using namespace std;
    
    int main(){
        long long int n,j,t;
        
        cin >> n ;
        for (int i=2;i*i<n;i++){
            if(n % i ==0 ){
                cout << n / i << endl;
                break;
            }
        }
        
        return 0;	
    }
    
  44. 第n小的质数http://noi.openjudge.cn/ch0105/44/
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
        long n,c=2,i=3,j;
        bool flag = false;
        cin >> n ;
        if (n==1)
            i = 2;
        else
            while(c<n){
                i = i + 2;
                flag = true;
                for(j=2;j<=sqrt(i);j++)
                    if (i % j==0){
                        flag = false;
                        break;
                    }
                if(flag){
                    c = c + 1;
                }
            }
        cout << i << endl;
        
        return 0;	
    }
    
  45. 金币http://noi.openjudge.cn/ch0105/45/
    #include <iostream>
    using namespace std;
    
    int main(){
        long n,coin=1,sum=0;
    
        cin >> n ;
        
        int count = 1;
        while(count <= n){
            for(int j=1;j<=coin&&count <=n;j++){
                sum = sum + coin;
                count = count + 1;
            }
            coin = coin + 1;
        }
        cout << sum << endl;
        
        return 0;	
    }
    

参考网址

  1. http://noi.openjudge.cn