2125 - 按要求输出整数(III)

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

输出[N, M] (0<N<=M)范围以内既不能被3整除又不能被5整除的数.


题目输入

数据有多行,每一行有3个数N,M,K (0<N<=M,  0<K)。遇到一行是-1,-1, -1时,表示输入结束。

题目输出

输出对应[N, M]输出[N, M]范围内符合题目要求的数,并且每行只显示K个数,每个数之间用空格分隔。

每组不同范围的数据输出之间留一空行。(注意输出格式相当容易错)

输入/输出样例

输入格式

5 20 5
10 30 7
-1 -1 -1

输出格式

7 8 11 13 14
16 17 19

11 13 14 16 17 19 22
23 26 28 29

C语言解答

# include<stdio.h>
#include<stdbool.h>
	int main(){
	    int n,m,k;
    int i;
    bool flag=true;
	    while(scanf("%d %d %d",&n,&m,&k)!=EOF){
                if(n==-1&&m==-1&&k==-1)break;
                if(!flag) printf("\n");
              bool flagtwo=true;
              int j=0;
              for(i=n;i<=m;i++){
              if((i%3!=0)&&(i%5!=0)){
                    if(!flagtwo)printf(" ");
                    printf("%d",i);
                    j++;
                    flagtwo=false;
                    if(j%k==0){
                    printf("\n");
                    flagtwo=true;
            }

        }
        if((i==m)&&(flagtwo==true)) flag=false;
        if((i==m)&&(flagtwo!=true)) {printf("\n"); flag=false;}

     }

        }
     return 0;
	}

C++解答

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int n, m, k;  
	int count;
	int flag = 0;
	while (cin>>n>>m>>k)
	{
		if (n== -1) break;
		if (flag++ != 0)
			cout<<endl;

		count = 0;
		for (int i=n; i<=m; i++)
		{			
			if (i%3 != 0 && i%5 != 0)
			{
				if (count == 0)
					cout<<i;
				else
					cout<<" " <<i;
				
				count++;
				if (count == k)
				{
					cout<<endl;
					count = 0;
				}
			}
			
		}
		if (count != 0)
			cout<<endl;
		
	}
	return 0;
}

Java解答

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()){
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int count = 0;
            if(a==-1&&b==-1&&c==-1)
                break;
            while(a<=b){
                if(a%3!=0 && a%5!=0){
                    count++;
                    if(count%c==1){
                        if(count<c)
                        System.out.print(a);
                        else
                        System.out.print("\n"+a);}
                    else{
                        System.out.print(" "+a);
                    }
                }
                a++;
            }
            System.out.println("\n");
        }
    }
}