⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.c

📁 任何给一些数一个和,算出这些可以有哪些方式计算得到这个和的算法.
💻 C
字号:
#include <stdio.h>
#include <string.h>

main()
{
int sum,count,i,j;
int data[100],cou[5];
char out[1000];
for(i=0;i<5;i++) cou[i]=0;
printf("This is a program which can work out all possible result that addition by some data\r\n");
printf("Please input:\r\nusage:result,count,data1,data2,data3...\r\n");
scanf("%d,%d",&sum,&count);
for(i=0;i<count&&i<100;i++)
	scanf(",%d",&data[i]);
data[count]=0;
strcpy(out,"");
for(cou[0]=0;cou[0]<=count&&cou[0]<100;cou[0]++)
	for(cou[1]=0;cou[1]<=count&&cou[1]<100;cou[1]++)
		for(cou[2]=0;cou[2]<=count&&cou[2]<100;cou[2]++)
			for(cou[3]=0;cou[3]<=count&&cou[3]<100;cou[3]++)
				for(cou[4]=0;cou[4]<=count&&cou[4]<100;cou[4]++)
					if (data[cou[0]]+data[cou[1]]+data[cou[2]]+data[cou[3]]+data[cou[4]]==sum)
						{int same=0;
						int start=0;
						for(i=0;i<5;i++)
							for(j=0;j<5;j++)
								if (cou[i]==cou[j]&&cou[i]!=count&&i!=j) same=1;
						if (same==0)
							{char temp[100],temp1[100];
							strcpy(temp,"");
							for(i=0;i<5;i++)
								if (cou[i]!=count)
									{if (start==0)
										{start=1;sprintf(temp1,"%d",data[cou[i]]);
										strcat(temp,temp1);}
									else
										{sprintf(temp1,"+%d",data[cou[i]]);
										strcat(temp,temp1);}
									}
							sprintf(temp1,"=%d\r\n",sum);
							strcat(temp,temp1);
							if (strstr(out,temp)==0) strcat(out,temp);
							}
						}
if (out[0]=='\0')
	printf("Not match number Found!\r\n");
else
	printf("%s",out);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -