作业帮 > 综合 > 作业

c语言 找数字有一样的数

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/16 19:48:12
c语言 找数字有一样的数
Problem:Two integers are considered to be friendly if all of the digits in the first number appear in the second number.Given ten integers as input display all pairs of “friends” in the set where the second number must come after (order of input) the first in the data set.
• The user will always enter 10 non-negative integers as input.Leading zero digits are ignored.
• Display no more than 5 pairs of friends per line of output.
Example Execution #1:
Enter data #1:1234
Enter data #2:4321
Enter data #3:2111
Enter data #4:12345
Enter data #5:5432
Enter data #6:23451
Enter data #7:3
Enter data #8:2341112
Enter data #9:5423
Enter data #10:3
Friends:[1234,4321] [1234,12345] [1234,23451] [1234,2341112] [4321,12345]
[4321,23451] [4321,2341112] [2111,12345] [2111,23451] [2111,2341112]
[12345,23451] [5432,23451] [5432,5423] [3,2341112] [3,5423]
[3,3]
大概就是.只要第一个数里的数字在另外一个数里全部出现.就输出.
最好输出格式和例题一样.
现在完全没有头绪.马上要交了.
#include
#include
#include
#define N 10
#define MAX_LEN 100
int friend(char * a, char * b)
{
int i;
while (*a)
{
for (i = 0; b[i] != '\0'; i++)
if (b[i] == *a)
break;
if (b[i] == '\0')
return 0;
a++;
}
return 1;
}
int main()
{
char data[N][MAX_LEN];
char s[MAX_LEN], * p;
int i, j, align = 0, n = 0;
puts("Example Execution #1:");
for (i = 0; i < N; i++)
{
printf("Enter data #%d: ", i);
scanf("%s", s);
p = s;
while (*p == '0' && p[1] != '\0') p++;
strcpy(data[n++], p);
}
printf("Friends: ");
for (i = 0; i < N; i++)
{
for (j = i + 1; j < N; j++)
if (friend(data[i], data[j]))
{
if (align % 5 != 0)
putchar(' ');
printf("[%s, %s]", data[i], data[j]);
if (align % 5 == 4)
putchar('\n');
align++;
}
}
return 0;
}