[英]Find the position of a max on a number
I have C program that needs to find the position of a number.我有 C 程序,需要找到一个数字的 position。 It goes like this:它是这样的:
From standard input we enter unknown number of number that are positive.从标准输入中,我们输入未知数量的正数。 The numbers have maximum of 5 digits, we read new numbers till the user enters a value that is not a number.数字最多为 5 位,我们读取新数字,直到用户输入一个不是数字的值。 I need to find the positions of the max digit of a number from right to left.我需要从右到左找到一个数字的最大数字的位置。 Use the right-most position if there are more than one instance of the max digit.如果最大位数有多个实例,请使用最右侧的 position。
The program needs to output the position and the number of times the max digit of a number was found at that position.程序需要 output 和 position 和在那个 position 处找到一个数的最大位数的次数。
For example:例如:
input:输入:
97654 48654 12345 12343 1263 12443 12643 12777 #
output: output:
0: 2
1: 3
2: 1
3: 1
4: 1
because因为
Position: 4 3 0 1 1 1 2 0
v v v v v v v v
97654 48654 12345 12343 1263 12443 12643 12777 #
THE PROGRAM WORKS FOR THIS SPECIFIC TEST CASE该程序适用于这个特定的测试用例
More test cases under the code.代码下有更多测试用例。
Here is my code:这是我的代码:
#include <stdio.h>
int main(){
int n;
int max;
int num,digit,pos,br0=0,br1=0,br2=0,br3=0,br4=0;
while (scanf("%d",&n)) {
max =0;
num = n;
pos=0;
while (num>0) {
digit = num%10;
if(digit > max){
max=digit;
pos++;
}
num/=10;
}
printf("%d\n",pos);
switch (pos) {
case 1: br0++; break;
case 2: br1++; break;
case 3: br2++; break;
case 4: br3++; break;
case 5: br4++; break;
}
}
printf("0: %d\n1: %d\n2: %d\n3: %d\n4: %d\n",br0,br1,br2,br3,br4);
return 0;
}
This program work for some test cases, such as该程序适用于某些测试用例,例如
97654 48654 12345 12343 1263 12443 12643 12777 #
123 456 789 987 654 321 #
But not for:但不适用于:
542 8965 7452 1111 12 8 6532 98745 15926 #
75386 86142 94285 15926 35724 #
The problem with your program is that within this loop你的程序的问题是在这个循环中
while (num>0) {
digit = num%10;
if(digit > max){
max=digit;
pos++;
}
num/=10;
}
the variable pos
is incremented only when a digit that is greater than previous digits is found.仅当找到大于先前数字的数字时,变量pos
才会增加。 For example If you have a number like this例如,如果您有这样的号码
51234
then the first largest digit is 4
and the variable pos
is set to 1
.那么第一个最大的数字是4
并且变量pos
设置为1
。 After that when the next largest digit is found that is the digit 5
the variable pos
is incremented and becomes equal to 2
while actually the largest digit 5
is at the position 5
.之后,当找到下一个最大数字是数字5
时,变量pos
增加并变为等于2
,而实际上最大数字5
在 position 5
处。
You need to introduce one more variable as for example您需要再引入一个变量,例如
max =0;
num = n;
pos=1;
int i = 1;
do
{
digit = num%10;
if(digit > max){
max=digit;
pos = i;
}
} while ( ( num /=10 ) && ( i++ != 5 ) );
I would write the program the following way我会用以下方式编写程序
#include <stdio.h>
int main(void)
{
enum { N = 5 };
const unsigned int Base = 10;
size_t total[N] = { 0 };
unsigned int n;
while ( scanf( "%u", &n ) == 1 )
{
unsigned int pos = 0;
unsigned int max_digit = 0;
unsigned int i = 0;
do
{
unsigned int current_digit = n % Base;
if ( max_digit < current_digit )
{
pos = i;
max_digit = current_digit;
}
} while ( ( n /= Base ) && ( ++i != N ) );
++total[pos];
}
for ( unsigned int i = 0; i < N; i++ )
{
printf( "%u: %zu\n", i, total[i] );
}
return 0;
}
For the input对于输入
542 8965 7452 1111 12 8 6532 98745 15926 #
the program output is程序 output 是
0: 3
1: 0
2: 3
3: 2
4: 1
all input from stdin
starts of as string data, it may be easier to do the work using fgets()
, and keeping the input in string format.来自stdin
输入的所有输入都以字符串数据开头,使用fgets()
完成工作可能更容易,并将输入保持为字符串格式。 (verifying that it contains numeric characters.) (验证它是否包含数字字符。)
Here is an alternate way of getting the information you describe:这是获取您描述的信息的另一种方法:
int main(void) {
char inBuf[20] = {0};
int index = 0;
int loops = 0;
int maxPos = 0;
int maxVal = 0;
printf("Enter a number : ");
while (fgets(inBuf, sizeof inBuf, stdin) && loops < 6) {
inBuf[strcspn(inBuf, "\r\n")] = 0;//remove unwanted white space
if(strstr(inBuf, "#")) return 0;//exit if "#"
if(digits_only(inBuf))
{
index = 0;
maxVal = inBuf[index];
while(inBuf[index])
{
if(inBuf[index] >= maxVal)
{
maxVal = inBuf[index];
maxPos = index;
}
index++;
}
printf("%d:%d \n", loops, maxPos);
loops++;
inBuf[0]=0;
}
else
{
printf("\n%s contains non-numeric characters, it cannot be converted.\n\nctrl-c to exit\n...Or enter a number : \n", inBuf);
}
};
return 0;
}
scanf
is the wrong tool for this. scanf
是错误的工具。 ( scanf
is (almost) always the wrong tool). ( scanf
(几乎)总是错误的工具)。 For this particular problem, you really want to treat the input as a string.对于这个特殊问题,您确实希望将输入视为字符串。 As long as you don't want to accept inputs that look like "1e3" (which is a perfectly valid representation of an integer), you could just do something like:只要您不想接受看起来像“1e3”(这是整数的完全有效表示)的输入,您就可以执行以下操作:
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
int
main(void){
int max = -1;
int br[5] = {0};
int maxpos = -1;
int len = 0;
int c;
while( (c = getchar()) != EOF ){
if( c && strchr("0123456789", c) ){
if( ++len > 5 ){
fputs("invalid input\n", stderr);
return 1;
}
assert( len > 0 && len < 6 );
if( c > max + '0' ){
maxpos = len;
max = c - '0';
}
} else if( isspace(c) ){
if( max > -1 ){
br[len - maxpos] += 1;
}
maxpos = -1;
len = 0;
max = '0' - 1;
} else {
fputs("invalid input\n", stderr);
return 1;
}
}
for( int i = 0; i < 5; i++ ){
printf("%d: %d\n", i, br[i]);
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.