Easy C项目显示重复的数字
我对此代码有问题。。。。项目应该在输入编号中显示重复编号。例如:Easy C项目显示重复的数字,c,C,我对此代码有问题。。。。项目应该在输入编号中显示重复编号。例如: $ ./a.out Enter a number: 9893746595 Repeated: 9 5 代码如下: #include <stdio.h> #include <stdlib.h> int main(void) { int a[10], b[10] ; int n,t; printf("Enter a number: "); for(n=0; n<1
$ ./a.out
Enter a number: 9893746595
Repeated: 9 5
代码如下:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a[10], b[10] ;
int n,t;
printf("Enter a number: ");
for(n=0; n<10;n++)
{
scanf("%d", &a[n]);
n=t;
a[n]=b[t];
}
for(n=0;n<10;n++)
{
for(t=n;t<10;t++)
{
if(a[n]=b[t])
printf("%d", a[n]);
}
}
return 0;
}
#包括
#包括
内部主(空)
{
int a[10],b[10];
int n,t;
printf(“输入一个数字:”);
对于(n=0;n如果(a[n]=b[t])
将b[t]
分配给a[n]
您很可能希望使用if(a[n]==b[t])
来比较这些值
使用-Wall-Wextra-Werror
标志进行编译是个好主意,这样所有警告都会被启用,并被视为错误(因此你不能简单地忽略它们)。有了这些标志,编译器会因为你做了一个意外的赋值而对你大喊大叫。如果(a[n]=b[t])
将b[t]
赋值给a[n]
您很可能希望使用if(a[n]==b[t])
来比较这些值
使用-Wall-Wextra-Werror
标志编译是一个好主意,这样所有警告都会被启用,并被视为错误(因此您不能简单地忽略它们)有了这些标志,编译器会因为你做了一个意外赋值而对你大喊大叫。你的代码在t
初始化之前将t
赋值给n
这肯定会引起问题。我还没有完全研究您的其余代码,但您应该先初始化t
,然后再使用它。如果仍然不起作用,请提供诸如它做什么或不做什么之类的信息。在t
初始化之前,您的代码将t
分配给n
这肯定会引起问题。我还没有完全研究您的其余代码,但您应该先初始化t
,然后再使用它。如果这仍然不起作用,请提供诸如它做什么或不做什么之类的信息。您可能希望查看循环的第一个。t
是从中读取的,而不是从中读取的写入到。与b
相同。并且,您正在使用b[t]
覆盖刚读入的变量a[n]
。在的条件中,如果写入=
,则表示=
如果打开编译器中的每个选项以发出警告并严格检查是否符合标准语言,它就会捕获这些警告。您可能希望查看循环的第一个。t
是从中读取的,而从未写入。b
也是如此。而且,您正在覆盖刚读入的变量a[n]
和b[t]
。在if
的条件中,您的意思是=
其中写入了=
如果您打开编译器中的每个选项来发出警告,并严格检查是否符合标准语言,它就会捕获这些警告。您的代码是假的。;-)
这里通常的方法是创建一个由10个int
s组成的数组,每个数字对应一个,并统计用户提供的数字中每个数字的出现次数
有一种惯用的方法可以一次获取一个数字的位数num
:使用num%10
获取最后一个数字,使用num/10
获取没有最后一个数字的数字。然后,您的程序可能如下所示:
int dcount[10] = {0}; // 10 ints, all initialized to 0
scanf("%d", &num);
while(num) {
dcount[num % 10]++; // increment dcount[i], where i is the last digit of num
num /= 10; // "remove" last digit from num
}
for (int i = 0; i < sizeof(dcount)/sizeof(dcount[0]); i++)
printf("%d occured %d times\n", i, dcount[i]);
int-dcount[10]={0};//10个int,全部初始化为0
scanf(“%d”和&num);
while(num){
dcount[num%10]++;//递增dcount[i],其中i是num的最后一位数字
num/=10;//“删除”num中的最后一个数字
}
对于(int i=0;i
我没有测试上面的代码,所以可能有一些小缺陷。不过,一般原则应该是明确的
希望有帮助。你的代码是假的。;-)
int main()
{
int i, number, digitCount[10];
// Before starting, set the digit count for each digit to 0
for (i = 0; i < 10; i++)
{
digitCount[i] = 0;
}
// Store the entire number in one int
printf("Enter a number: ");
scanf("%d", &number);
// Find the remainder of number / 10 in order to get the last digit
// Divide number by 10 in order to remove that digit
// Continue to peel off digits until you reach zero
while (number != 0)
{
digitCount[number % 10]++;
number /= 10;
}
// For each digit which is counted more than once, print it
printf("Repeated: ");
for (i = 0; i < 10; i++)
{
if (digitCount[i] > 1)
{
printf("%d ", digitCount[i]);
}
}
return 0;
}
这里通常的方法是创建一个由10个int
s组成的数组,每个数字对应一个,并统计用户提供的数字中每个数字的出现次数
有一种惯用的方法可以一次获取一个数字的位数num
:使用num%10
获取最后一个数字,使用num/10
获取没有最后一个数字的数字。然后,您的程序可能如下所示:
int dcount[10] = {0}; // 10 ints, all initialized to 0
scanf("%d", &num);
while(num) {
dcount[num % 10]++; // increment dcount[i], where i is the last digit of num
num /= 10; // "remove" last digit from num
}
for (int i = 0; i < sizeof(dcount)/sizeof(dcount[0]); i++)
printf("%d occured %d times\n", i, dcount[i]);
int-dcount[10]={0};//10个int,全部初始化为0
scanf(“%d”和&num);
while(num){
dcount[num%10]++;//递增dcount[i],其中i是num的最后一位数字
num/=10;//“删除”num中的最后一个数字
}
对于(int i=0;i
我没有测试上面的代码,所以可能有一些小缺陷。不过,一般原则应该是明确的
希望有帮助。intmain()
int main()
{
int i, number, digitCount[10];
// Before starting, set the digit count for each digit to 0
for (i = 0; i < 10; i++)
{
digitCount[i] = 0;
}
// Store the entire number in one int
printf("Enter a number: ");
scanf("%d", &number);
// Find the remainder of number / 10 in order to get the last digit
// Divide number by 10 in order to remove that digit
// Continue to peel off digits until you reach zero
while (number != 0)
{
digitCount[number % 10]++;
number /= 10;
}
// For each digit which is counted more than once, print it
printf("Repeated: ");
for (i = 0; i < 10; i++)
{
if (digitCount[i] > 1)
{
printf("%d ", digitCount[i]);
}
}
return 0;
}
{
整数i,数字,数字计数[10];
//开始之前,将每个数字的数字计数设置为0
对于(i=0;i<10;i++)
{
数字计数[i]=0;
}
//将整个数字存储在一个整数中
printf(“输入一个数字:”);
scanf(“%d”和编号);
//找到数字/10的余数,以获得最后一位数字
//将数字除以10以删除该数字
//继续剥除数字,直到达到零
while(数字!=0)
{
数字计数[数字%10]+;
数目/=10;
}
//对于每一个计数超过一次的数字,打印它
printf(“重复:”);
对于(i=0;i<10;i++)
{
如果(数字计数[i]>1)
{
printf(“%d”,数字计数[i]);
}
}
返回0;
}
int main()
{
整数i,数字,数字计数[10];
//开始之前,将每个数字的数字计数设置为0
对于(i=0;i<10;i++)
{
数字计数[i]=0;
}
//将整个数字存储在一个整数中
printf(“输入一个数字:”);
scanf(“%d”和编号);
//找到数字/10的余数,以获得最后一位数字
//将数字除以10以删除该数字
//继续剥手指