作业题(二)

1:一个数如果恰好等于它的因子之和,这个数被成为”完数”,例如:6=1+2+3.请编程找出1000以内的完数

#include <stdio.h>


int main()
{
	int i,j;
	
	
	for(i=2;i<1000;i++)
	{
		int sum = 0;
		for(j=1;j<i/2+1;j++)
		{
			if(i%j == 0)
				sum += j;
		}
		if(i == sum)
			printf("%d\n",i);
	}
	
	return 0;
}

2:有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的人退出圈子,问最后最后留下的是原来第几号的那位.
提示:用数组完成

#include <stdio.h>
#define N 10

int main()
{
	int a[N]={0};
	int i,j=0;
	int count=0;
	int temp=0;
	
	for(i=0;i<N;i++)
	{
		a[i] = i+1;
	}
	
	for(i=0;i<N;i++)
	{
		printf("%d\t",a[i]);
		if(i%10 == 9)
		{
			printf("\n");
		}
	}
	printf("\n");
	while(1)
	{
		
		if(a[j] != 0)
		{
			if(a[j] == temp)		//保存上一个非0元素,若下一个非0元素和该值相等,则退出循环
			{
				break;
			}
			temp=a[j];				
			count++;
			if(count == 3)			//把计数到3的元素置0
			{
				a[j] = 0;
				count = 0;
			}
		}
			j++;
		if(j == N)
		{
			j=0;
		}
	}


	for(i=0;i<N;i++)
	{
		if(a[i]!=0)
		{
			printf("Num=%d\n",a[i]);
		}
	}
	
	
	return 0;
}

3:编写程序,打印出9×9乘法表

#include <stdio.h>

int main()
{
	int i,j;
	
	for(i=1;i<=9;i++)
	{
		for(j=1;j<=i;j++)
		{
			printf("%d*%d=%d\t",i,j,i*j);
		}
		printf("\n");
	}
	
	
	return 0;
}

4:编写程序,打印菱形星号组合

#include <stdio.h>

int main()
{
	int x,m,y;
	
	for (y=10;y>=-10;y--)
	{
		m = y;
		for (x=0;x<=20;x++)
		{
			if (x == m || x == 20-m || x == -m || x == 20+m)
			{
				printf("*");
			}
			else
			{
				printf(" ");
			}
		}
		printf("\n");
	}
	
	return 0;
}

打印图形都可以用同样的模板,只需确定f(x)与y的函数关系,再在x==的判定处对原函数图形进行平移翻转等变化

5: 输入一个字符串,计算字符串中子串出现的次字数

#include <stdio.h>

int lenth(char *pstr)
{
	int len=0;

	while(*pstr++ != 0)
	{
		len++;
	}
	
	return len;
}



int count(char *p1,char *p2)
{
	int count=0;
	char *pa = p1;
	char *pb = p2;
	int i;
	int len=lenth(pb);
	
	while(*(pa+len-1) != '\0')
	{
		for(i = 0;i < len; i++)
		{
			if(*(pa+i) != *(pb+i))
			{
				break;
			}
			if(i == len-1)
			{
				count++;
			}
		}
		pa++;
	}
	
	return count;
}

int main()
{
	char str[100]={0};
	char srchstr[100]={0};
	
	printf("Input string:\n");
	scanf("%s",str);
	printf("Input searchstring:\n");
	scanf("%s",srchstr);
	
	printf("count=%d\n",count(str,srchstr));
	
	
	return 0;
}

6: 输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.
提示:帧头和帧尾分别是head和tail 字符串”asdheadhauboisoktail”中headhauboisoktail是合法帧

#include <stdio.h>

void fun(char *pstr)
{
	char headstr[10]="head";
	char tailstr[10]="tail";
	char *p=pstr;
	char *ph=headstr;
	char *pt=tailstr;
	int temp=1;
	
	
	while(temp == 1)				//search head
	{
		if(*p == 0)
		{
			temp = 0;
			break;
		}		
		if(*p == *ph)
		{
			ph++;
			if(*ph == 0)			//Match head success
			{
				p=p-3;
				temp = 2;
				break;
			}
		}
		else
		{
			ph = headstr;
		}
		p++;
	}
	
	
	while(temp == 2)				//search tail
	{
		if(*p == 0)
		{
			temp = 0;
			break;
		}
		
		*pstr++=*p;
		
		if(*p == *pt)
		{
			pt++;
			if(*pt == 0)			//Match tail success
			{
				*(++p)='\0';
				*pstr='\0';
				temp=3;
				break;
			}
		}
		else
		{
			pt = tailstr;
		}
		p++;
	}
	
	
}

int main()
{
	char str[100]={0};
	printf("Input string:\n");
	scanf("%s",str);
	fun(str);
	printf("%s\n",str);
	
	
	return 0;
}

7:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。

#include <stdio.h>


int main()
{
	int n;
	float a=2,b=1;
	float sum=0;
	
	for(n=0;n<20;n++)
	{
		sum+=a/b;		
		a=a+b;
		b=a-b;
	}
	
	printf("%f\n",sum);

	return 0;
}

8:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。

#include <stdio.h>


int main()
{
	int m;
	int n1,n2,n3;
	
	for(m=100;m<=999;m++)
	{
		n1=m%10;
		n2=(m/10)%10;
		n3=m/100;
		if(n1*n1*n1+n2*n2*n2+n3*n3*n3 == m)
			printf("%d\n",m);
	}

	return 0;
}

9:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制

#include <stdio.h>

int main()
{
	int a,n,sum=0;
	int i;
	
	printf("Input a,n\n");
	scanf("%d,%d",&a,&n);
	
	for(i=1;i<=n;i++)
	{
		sum=sum*10+i*a;
	}
	
	printf("sum = %d",sum);
	
	return 0;
}

10:将字符串“We Are Family!”,去除空格后打印出来。(WeAreFamily!)

#include <stdio.h>

void fun(char *pstr)
{
	char *p=pstr;
	int temp=1;
	
	while(1)
	{
		if(*p == '\0')
		{
			*pstr='\0';
			break;
		}
		if(*p == ' ')
		{
			p++;
			continue;
		}
		*pstr++=*p++;
	}
	
}


int main()
{
	char str[100]="We Are Family!";
		
	printf("%s\n",str);
	fun(str);
	printf("%s\n",str);
	
	return 0;
}

11:输入一个字符串,判断其是否是回文。(回文:即正读和反读都一样,如abcba, abccba)

#include <stdio.h>

int len(char *pstr)
{
	int count=0;
	while(*pstr++ != '\0')
	{
		count++;
	}	
	
	return count;
}


int fun(char *pstr)
{
	int left  = 0;
	int right = len(pstr)-1;
	
	while(left < right)
	{
		if(pstr[left] != pstr[right])
		{
			return 0;
		}
		left++;
		right--;	
	}
	
	return 1;
	
}


int main()
{
	char str[100]="abccba";
	int temp=fun(str);
	
	if(temp == 1)
	{
		printf("%s is huiwen\n",str);
	}
	else
	{
		printf("%s is not huiwen\n",str);
	}
	
	
	return 0;
}

12:输入一段字符串,无论是否有重复字母出现,都只打印出现过的小写字母,并按照小写字母顺序打印。
(如输入qewqwr322rqw<>211qESFSSEraZz, 打印aeqrwz)

    #include <stdio.h>
 
    int main()
    {
    	char count[256] = {0};
    	char c;
    	char i;
	
	
	while(c != '\n')
	{
		scanf ("%c", &c);
		count[c]++;
	}	
	
	for(i='a';i<'z';i++)
	{
		if(count[i] != 0 || count[i+'A'-'a'] != 0)
		{
			printf("%c",i);
		}
	}
	printf("\n");
	
	return 0;
}

13:方阵的主对角线之上称为“上三角”。
请你设计一个用于填充n阶方阵的上三角区域的程序。填充规则是:使用1,2,3…的自然数列,从左上角开始,
按照顺时针方向螺旋填充。
例如:当n=3时,输出:
1 2 3
6 4
5
当n=4时,输出:
1 2 3 4
9 10 5
8 6
7
当n=5时,输出:
1 2 3 4 5
12 13 14 6
11 15 7
10 8
9
程序运行时,要求用户输入整数n(3~20)
程序输出:方阵的上三角部分。
要求格式:每个数据宽度为4,右对齐。(所有函数写在一个函数中)

#include <stdio.h>

void print(int n)
{
	if(n<3 || n>30)
	{
		return ;
	}
	int a[31][31]={0};
	int x=1,temp=0;
	int i,j;
	while(x < (n*n+n)/2)
	{
		for(i=temp;i<n-temp*2;i++)
		{
			a[temp][i]=x++;
		}
		for(i=temp+1;i<n-temp*2;i++)
		{
			a[i][n-temp-1-i]=x++;
		}
		for(i=n-temp*2-2;i>temp;i--)
		{
			a[i][temp]=x++;
		}
		temp++;
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<n-i;j++)
		{
			printf("%4d",a[i][j]);
		}
		printf("\n");
	}
}


int main()
{
	int n;
	printf("Input n = ");
	scanf("%d",&n);
	printf("--------------------------------------------------------------\n");
	print(n);
	
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和引用\[2\]的代码,可以生成十以内的加减法作业题。以下是生成的代码示例: ```python import random def generate_add_sub_questions(): questions = \[\] for i in range(50): num1 = random.randint(0, 20) num2 = random.randint(0, 20) if random.choice(\[True, False\]): question = "{}+{}=".format(num1, num2) answer = num1 + num2 else: if num1 >= num2: question = "{}-{}=".format(num1, num2) answer = num1 - num2 else: continue questions.append((question, answer)) return questions questions = generate_add_sub_questions() print("学号:*** 姓名:***") for index, question in enumerate(questions): if (index + 1) % 5 == 0: print(question\[0\]) else: print(question\[0\], end='\t') ``` 这段代码会生成50道十以内的加减法作业题,并按照每行5道题的格式输出。每道题的格式为`num1+num2=`或`num1-num2=`,其中`num1`和`num2`是随机生成的数字。 #### 引用[.reference_title] - *1* [Python妙用|给小外甥生成10以内加减运算数学作业](https://blog.csdn.net/a55656aq/article/details/122984822)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python作业——随机生成不重复的20以内加法算式](https://blog.csdn.net/Pan_77777/article/details/125164497)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值