输出7和7的倍数,还有包含7的数字

zhao0829wang 2011-03-23 07:31:50
输出7和7的倍数,还有包含7的数字例如(17,27,37...70,71,72,73...)

【要求】
【数据输入】一个整数N。(N不大于30000)

【数据输出】从小到大排列的不大于N的与7有关的数字,每行一个。

【样例输入】
20

【样例输出】
7
14
17

我用if语句做了,但是太麻烦,想换成switch语句,但是却写不出来。 这样的该怎么写啊?
...全文
4691 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
limit111 2012-09-23
  • 打赏
  • 举报
回复
能有更快的算法吗?复杂度和n无关,和n的位数相关?
清竹小雨 2012-02-24
  • 打赏
  • 举报
回复
如果数据是1000000的呢?会很时间的,还有更好的算法吗?
tangxianghenggood 2011-03-25
  • 打赏
  • 举报
回复
与7有关 没完全理解你的意思
cdmag 2011-03-25
  • 打赏
  • 举报
回复
代码写得太不简练了~~还是看2楼吧~~
cdmag 2011-03-25
  • 打赏
  • 举报
回复
格式好像发错了

//输出7和7的倍数,还有包含7的数字例如(17,27,37...70,71,72,73...)
#include<stdio.h>
#include<stdlib.h>
#define N 200
int main()
{
int i=7,temp,temp2;
while(i<=N){
if(0==i%7){//7的倍数
printf("%d\n",i);
++i;
continue;
}
else{
temp=i;
while(temp>=7){
temp2=temp-temp/10*10;//temp2是最低位数字
if(7==temp2){
printf("%d\n",i);//包含7的数
break;
}
temp/=10;//去掉最低位
}
i++;
}

}
system("pause");
return 0;
}
cdmag 2011-03-25
  • 打赏
  • 举报
回复
最基础的方法了,还是if语句~~
//输出7和7的倍数,还有包含7的数字例如(17,27,37...70,71,72,73...)
#include<stdio.h>
#include<stdlib.h>
#define N 200
int main()
{
int i=7,temp,temp2;
while(i<=N){
if(0==i%7){//7的倍数
printf("%d\n",i);
++i;
continue;
}
else{
temp=i;
while(temp>=7){
temp2=temp-temp/10*10;//temp2是最低位数字
if(7==temp2){
printf("%d\n",i);//包含7的数
break;
}
temp/=10;//去掉最低位
}
i++;
}

}
system("pause");
return 0;
}

孙歌 2011-03-25
  • 打赏
  • 举报
回复
数学问题
screwzm 2011-03-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 qcy_1990 的回复:]

#include "StdAfx.h"
int main()
{
for(int i=7;i <300;i++)
{
if(i%7==0||i%10==7)
printf("%d\n",i);
}
}

N=300,自己可以调
[/Quote]
兄弟,你忽略了中间也有7的情况!
screwzm 2011-03-25
  • 打赏
  • 举报
回复
2楼的正解,我补充一个简洁点的!

void print_7(int m)
{
int i = 0;
char Temp[100] = "\0";
for(i=1; i<=m; i++)
{
if(i%7 == 0)
{
printf("%d ",i);
continue;
}
sprintf(Temp,"%d",i);
if(strstr(Temp,"7") != NULL)
printf("%s ",Temp);
}
return;
}
暗黑帝国 2011-03-24
  • 打赏
  • 举报
回复
#include <iostream>
#include <sstream>

using namespace std;

void s7(int n)
{
for(int i=1;i<n;++i)
{
if(i%7==0)cout<<i<<" ";
if(i%10==7)cout<<i<<" ";
}
cout<<endl;
}

int main(int argc,char** argv)
{
stringstream ss;
ss<<argv[1];
int val;
ss>>val;
s7(val);
}
Huntrees 2011-03-23
  • 打赏
  • 举报
回复
除了循环,还真没想出来好的算法。大家有什么好的算法不。
zhao0829wang 2011-03-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 witwolf 的回复:]
[code=C/C++]
#include <stdio.h>
int fun(){
int temp;
while(i> 10){
temp=i%10;
if(temp==7)
retur……
[/Quote]
厉害 ,拜谢
qq120848369 2011-03-23
  • 打赏
  • 举报
回复
循环i=0...n

如果不能%7==0,那么就sprintf成字符串,在里面找7.
qcy_1990 2011-03-23
  • 打赏
  • 举报
回复
#include "StdAfx.h"
int main()
{
for(int i=7;i <300;i++)
{
if(i%7==0||i%10==7)
printf("%d\n",i);
}
}

N=300,自己可以调
张明云 2011-03-23
  • 打赏
  • 举报
回复

/*
输出7和7的倍数,还有包含7的数字例如(17,27,37...70,71,72,73...)

【要求】
【数据输入】一个整数N。(N不大于30000)

【数据输出】从小到大排列的不大于N的与7有关的数字,每行一个。

【样例输入】
20

【样例输出】
7
14
17
*/
#include <stdio.h>
#define ARRAY 30000
int main(int argc, char *argv[])
{
int data;
int i,m,j,k,index,fenjie[5];
int array_test[ARRAY];
int flag=0;
index=0;
printf("please input a data<=30000\n");
scanf("%d",&data);
for(i=7;i<=data;i++)
{
m=i;
for(j=4;j>=0;j--)
{
fenjie[j]=m%10;
m/=10;
}
flag=(fenjie[4]==7)||(fenjie[3]==7)||(fenjie[2]==7)||(fenjie[1]==7)||(fenjie[0]==7);
if((i%7==0)||flag)
{
array_test[index++]=i;
}
}
for(k=0;k<index;k++)
{
printf("%d\n",array_test[k]);
}
return 0;
}

我这有一剂药,你拿去试试,应能能行
masmaster 2011-03-23
  • 打赏
  • 举报
回复
root@~ #cat 3.c

#include <stdio.h>
#include <stdbool.h>

int main (void) {
int n,i;
bool is7 (int n);
scanf ("%i",&n);

for(i=7;i<=n;i++) {
if(i%7==0|is7(i)) {
printf ("%i\n",i);
}
}
return 0;
}
bool is7 (int n) {
do {
if(n%10==7) {
return true;
}
n/=10;
}while(n!=0);
return false;
}

root@~ #
root@~ #./3
50
7
14
17
21
27
28
35
37
42
47
49
root@~ #
elegant87 2011-03-23
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

//判断是否是7的倍数
bool IsDivision(const int n,const int m =7)
{
return !(n%m);
}

//判断整数n中是否含有7
bool IsContain(const int n,const int m =7)
{
int temp = n;
while(temp)
{
if( m == temp%10)
return true;
else
temp /= 10;
}
return false;
}

int main()
{
int n;
cout<<"Enter n:"<<endl;
cin>>n;
for(int i=7;i<=n;++i)
{
if(IsDivision(i) || IsContain(i))
cout<<i<<endl;
}
system("pause");
return 0;
}
masmaster 2011-03-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 zhao0829wang 的回复:]
输出7和7的倍数,还有包含7的数字例如(17,27,37...70,71,72,73...)

【要求】
【数据输入】一个整数N。(N不大于30000)

【数据输出】从小到大排列的不大于N的与7有关的数字,每行一个。

【样例输入】
20

【样例输出】
7
14
17

我用if语句做了,但是太麻烦,想换成switch语句,但是却写不出来。 这样的该怎么写啊?
[/Quote]

咋没有21,28?不是要输出7的倍数吗?
無_1024 2011-03-23
  • 打赏
  • 举报
回复
嗯二楼的解法可以了 但只是一个伪代码哦 要自己完善
liutengfeigo 2011-03-23
  • 打赏
  • 举报
回复
我错了.直接看2楼的吧,跳过一楼.
加载更多回复(2)

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧