| |

VerySource

 Forgot password?
 Register
Search
View: 800|Reply: 4

Let's look at this program that counts the number of digits in the integer number. How can there be a problem ~~ Thank

[Copy link]

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-11 13:00:02
| Show all posts |Read mode
#include <stdio.h>
int main ()
{
  int ri, repeat;
    int count;
      long in;
int countdigit (long number, int digit);

scanf ("% d",&repeat);
for (ri = 1; ri <= repeat; ri ++) {
scanf ("% ld",&in);
count = countdigit (in, 2);
printf ("count =% d\n", count);
}
}
int countdigit (long number, int digit)
{

int a, t, count, count1, i;
count = 1; count1 = 0;
while ((t = number / 10)! = 0)
count ++;
for (i = 1; i <= count; i ++)
{a = number;
for (; a> 0; a / = 10)
(if (a% 10 == 2)
count1 ++;}}
return count1;}
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-6-5 19:30:01
| Show all posts
while((t=number/10)!=0)
    count++;
//Infinite loop
Reply

Use magic Report

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-6-7 11:15:02
| Show all posts
Sorry, I know what is wrong.
Reply

Use magic Report

0

Threads

10

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-6-7 17:00:01
| Show all posts
lz logical ability expressed doubt. . . . The function should be changed (too many useless variables...):
int countdigit(long number, int digit)
{
long a;
int count;
    for(count=0,a=number;a>0;a/=10)
    {
if(a%10==digit)
count++;
     }
return count;
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-20 00:30:01
| Show all posts
Can't you write like this
1+rand()%2
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list