C语言:输出1000内能被3整除的个数,并输出第n到m(含n,m,m,由键盘输入)个之间能被3整除的数。咋编?
1.输出每行10个,一个间隔隔开,末尾无间隔;
2.若第m个数超出1000,只输出1000以内的数;
3.若n、m范围全不在1000内,输出提示“未找到”;
4.输入错误有提示并退出。 展开
代码文本:
#include "stdio.h"
int main(int argc,char *argv[]){
int n,m,k;
printf("Enter n & m(int 0<n<=m<=1000)...\n");
if(scanf("%d%d",&n,&m)==2 && n>0 && n<=m && m<=1000){
printf("\nThere are %d integer can be divided exactly by 3.\n",1000/3);
printf("\nThe No%d. to No%d.",n,m);
if(n*=3,(m = (m*=3)<1001 ? m : 1000) && n<=m){
puts(" can be divided exactly by 3 is as follows:");
for(k=0;n<=m;n+=3)
printf(++k%10 ? "%4d" : "%4d\n",n);
if(k%10)
putchar('\n');
}
else
puts("\nNo find...");
}
else
printf("Input error, exit...\n");
return 0;
}
//增加智能判断,m,n 的输入可以先大后小,或者先小后大。
#include<stdio.h>
//输出1000内能被3整除的个数
void print_q1()
{
int sum = 0;
int i;
/*for(i = 1; i <= 1000; i++)
if(i % 3 == 0)
sum += 1;*/
for(i = 3; i <= 1000; i += 3)
sum += 1;
printf("1-1000 能被3整除的个数: %d\n", sum);
}
//输出n-m内能被3整除的数
void print_q2()
{
int m, n;
printf("输入 n, m: ");
scanf("%d%d", &n, &m);
if(m > 1000 && n > 1000)
{
printf("未找到\n");
return;
}
int temp = n;
n = n < m ? n : m;
m = m > temp ? m : temp;
if(m > 1000)
m = 1000;
temp = 0;
for(int i = n; i <= m; i++)
{
if(i % 3 == 0)
{
printf("%4d ", i);
temp++;
if(temp % 10 == 0)
putchar('\n');
}
}
putchar('\n');
}
int main()
{
print_q1();
print_q2();
return 0;
}
int main() {
int m = 0, n = 0;
int i = 0;
int k = 0,count=0;
int arr[1000];
scanf("%d%d",&n,&m);
if (n >= m) {
printf("输入数据不合法!");
return 0;
}
if (m > 1000 && n > 1000) {
printf("未找到!");
return 0;
}
for (i = n; i <=m; i++) {
if (i % 3 == 0&&i<1000) {
arr[count] = i;
count++;
}
}
for (i = 0; i < count; i++) {
if (i < count - 1) {
printf("%d ", arr[i]);
k++;
if (k % 10 == 0) {
printf("\n");
}
continue;
}
printf("%d", arr[i]);
}
return 0;
}