英文:
Storing all even digits of an integer P in a string in the same order they appear in the integer P
问题
以下是您提供的代码的翻译部分:
#include <stdio.h>
void even(int p, char *s){
char s1[20];
int i,j;
i = 0;
while(p>0){
if((p-(p/10)*10) %2 == 0){
s[i] = '0' + (p-(p/10)*10);
i++;
};
p = p/10;
};
s[i]='#include <stdio.h>
void even(int p, char *s){
char s1[20];
int i,j;
i = 0;
while(p>0){
if((p-(p/10)*10) %2 == 0){
s[i] = '0' + (p-(p/10)*10);
i++;
};
p = p/10;
};
s[i]='\0';
//上面的部分从右到左截取数字并将其存储在字符串S中
j=0;
while (i>=0){
s1[j] = s[i];
i--;
j++;
};
//上面的部分应该将字符串S中的字符以相反的顺序存储在字符串S1中
i=0;
while(s1[i]!='\0'){
s[i] = s1[i];
i++;
};
s[i] = '\0';
//上面的代码应该使用字符串S1替换字符串S
}
int main(){
int p;
char s[20];
printf("Enter P: ");
scanf("%d",&p);
even(p,s);
printf("\n\nSTRING: %s",s);
}
';
//上面的部分从右到左截取数字并将其存储在字符串S中
j=0;
while (i>=0){
s1[j] = s[i];
i--;
j++;
};
//上面的部分应该将字符串S中的字符以相反的顺序存储在字符串S1中
i=0;
while(s1[i]!='#include <stdio.h>
void even(int p, char *s){
char s1[20];
int i,j;
i = 0;
while(p>0){
if((p-(p/10)*10) %2 == 0){
s[i] = '0' + (p-(p/10)*10);
i++;
};
p = p/10;
};
s[i]='\0';
//上面的部分从右到左截取数字并将其存储在字符串S中
j=0;
while (i>=0){
s1[j] = s[i];
i--;
j++;
};
//上面的部分应该将字符串S中的字符以相反的顺序存储在字符串S1中
i=0;
while(s1[i]!='\0'){
s[i] = s1[i];
i++;
};
s[i] = '\0';
//上面的代码应该使用字符串S1替换字符串S
}
int main(){
int p;
char s[20];
printf("Enter P: ");
scanf("%d",&p);
even(p,s);
printf("\n\nSTRING: %s",s);
}
'){
s[i] = s1[i];
i++;
};
s[i] = '#include <stdio.h>
void even(int p, char *s){
char s1[20];
int i,j;
i = 0;
while(p>0){
if((p-(p/10)*10) %2 == 0){
s[i] = '0' + (p-(p/10)*10);
i++;
};
p = p/10;
};
s[i]='\0';
//上面的部分从右到左截取数字并将其存储在字符串S中
j=0;
while (i>=0){
s1[j] = s[i];
i--;
j++;
};
//上面的部分应该将字符串S中的字符以相反的顺序存储在字符串S1中
i=0;
while(s1[i]!='\0'){
s[i] = s1[i];
i++;
};
s[i] = '\0';
//上面的代码应该使用字符串S1替换字符串S
}
int main(){
int p;
char s[20];
printf("Enter P: ");
scanf("%d",&p);
even(p,s);
printf("\n\nSTRING: %s",s);
}
';
//上面的代码应该使用字符串S1替换字符串S
}
int main(){
int p;
char s[20];
printf("Enter P: ");
scanf("%d",&p);
even(p,s);
printf("\n\nSTRING: %s",s);
}
请注意,我已经翻译了您提供的代码,不包括翻译问题或其他内容。如果您需要进一步的解释或有其他问题,请随时提问。
英文:
I have a task to make a function that will take an integer as it's atribute, and store every even number in tring S, but in the order those digits appear in the integer P.
Example:
Input: 2346
Now I run the function and what I need as output is a string S as:
Output: 246
Also for
Input: 20000
or as many zeroes as possible, output shouldn't include zeroes, since I don't count zeroes as even numbers, so
Output: 2
What I've done is made a function that would just put the numbers n the reverse order, since I'm getting all digits from the right to the left.
Here's my code:
#include <stdio.h>
void even(int p, char *s){
char s1[20];
int i,j;
i = 0;
while(p>0){
if((p-(p/10)*10) %2 == 0){
s[i] = '0' + (p-(p/10)*10);
i++;
};
p = p/10;
};
s[i]='
#include <stdio.h>
void even(int p, char *s){
char s1[20];
int i,j;
i = 0;
while(p>0){
if((p-(p/10)*10) %2 == 0){
s[i] = '0' + (p-(p/10)*10);
i++;
};
p = p/10;
};
s[i]='\0';
//The part above cuts off digits from right to left and stores them in the string S
j=0;
while (i>=0){
s1[j] = s[i];
i--;
j++;
};
//Part above should store the characters from the string S to the string S1 in reverse order
i=0;
while(s1[i]!='\0'){
s[i] = s1[i];
i++;
};
s[i] = '\0';
//Code above should take the string S and replace it with the string S1
}
int main(){
int p;
char s[20];
printf("Enter P: ");
scanf("%d",&p);
parni(p,s);
printf("\n\nSTRING: %s",s);
}
';
//The part above cuts off digits from right to left and stores them in the string S
j=0;
while (i>=0){
s1[j] = s[i];
i--;
j++;
};
//Part above should store the characters from the string S to the string S1 in reverse order
i=0;
while(s1[i]!='
#include <stdio.h>
void even(int p, char *s){
char s1[20];
int i,j;
i = 0;
while(p>0){
if((p-(p/10)*10) %2 == 0){
s[i] = '0' + (p-(p/10)*10);
i++;
};
p = p/10;
};
s[i]='\0';
//The part above cuts off digits from right to left and stores them in the string S
j=0;
while (i>=0){
s1[j] = s[i];
i--;
j++;
};
//Part above should store the characters from the string S to the string S1 in reverse order
i=0;
while(s1[i]!='\0'){
s[i] = s1[i];
i++;
};
s[i] = '\0';
//Code above should take the string S and replace it with the string S1
}
int main(){
int p;
char s[20];
printf("Enter P: ");
scanf("%d",&p);
parni(p,s);
printf("\n\nSTRING: %s",s);
}
'){
s[i] = s1[i];
i++;
};
s[i] = '
#include <stdio.h>
void even(int p, char *s){
char s1[20];
int i,j;
i = 0;
while(p>0){
if((p-(p/10)*10) %2 == 0){
s[i] = '0' + (p-(p/10)*10);
i++;
};
p = p/10;
};
s[i]='\0';
//The part above cuts off digits from right to left and stores them in the string S
j=0;
while (i>=0){
s1[j] = s[i];
i--;
j++;
};
//Part above should store the characters from the string S to the string S1 in reverse order
i=0;
while(s1[i]!='\0'){
s[i] = s1[i];
i++;
};
s[i] = '\0';
//Code above should take the string S and replace it with the string S1
}
int main(){
int p;
char s[20];
printf("Enter P: ");
scanf("%d",&p);
parni(p,s);
printf("\n\nSTRING: %s",s);
}
';
//Code above should take the string S and replace it with the string S1
}
int main(){
int p;
char s[20];
printf("Enter P: ");
scanf("%d",&p);
parni(p,s);
printf("\n\nSTRING: %s",s);
}
答案1
得分: 1
所以,我尝试了其他方法,它有效。我首先统计了整数 P 中有多少个偶数数字,然后将这些数字以反向顺序存储在字符串 S 中。
以下是我的新代码:
#include <stdio.h>
void even(int p, char *s){
char s1[20];
int i,j;
i = 0;
int k;
k = p;
while(p>0){
if((p-(p/10)*10) %2 == 0){
i++;
};
p = p/10;
};
j=i;
i = i-1;
p = k;
while(p>0){
if((p-(p/10)*10) %2 == 0){
s[i] = '0' + (p-(p/10)*10);
i--;
};
p = p/10;
};
s[j]='#include <stdio.h>
void even(int p, char *s){
char s1[20];
int i,j;
i = 0;
int k;
k = p;
while(p>0){
if((p-(p/10)*10) %2 == 0){
i++;
};
p = p/10;
};
j=i;
i = i-1;
p = k;
while(p>0){
if((p-(p/10)*10) %2 == 0){
s[i] = '0' + (p-(p/10)*10);
i--;
};
p = p/10;
};
s[j]='\0';
}
int main(){
int p;
char s[20];
printf("Enter P: ");
scanf("%d",&p);
even(p,s);
printf("\n\nSTRING: %s",s);
}
';
}
int main(){
int p;
char s[20];
printf("Enter P: ");
scanf("%d",&p);
even(p,s);
printf("\n\nSTRING: %s",s);
}
如果输入是:
Input: 347892
输出是:
Output: 482
并且我需要更正一下,我的代码实际上将 0 视为偶数,但我没有任务来处理零作为偶数或奇数,但将零作为异常处理将会很容易。
所以
Input: 20002
会给出相同的输出
Output: 20002
英文:
So, I tried something else out, and it worked. I first counted how many even digits there are in the integer P, and then stored those digits in the string S backward.
Here's my new code:
#include <stdio.h>
void even(int p, char *s){
char s1[20];
int i,j;
i = 0;
int k;
k = p;
while(p>0){
if((p-(p/10)*10) %2 == 0){
i++;
};
p = p/10;
};
j=i;
i = i-1;
p = k;
while(p>0){
if((p-(p/10)*10) %2 == 0){
s[i] = '0' + (p-(p/10)*10);
i--;
};
p = p/10;
};
s[j]='#include <stdio.h>
void even(int p, char *s){
char s1[20];
int i,j;
i = 0;
int k;
k = p;
while(p>0){
if((p-(p/10)*10) %2 == 0){
i++;
};
p = p/10;
};
j=i;
i = i-1;
p = k;
while(p>0){
if((p-(p/10)*10) %2 == 0){
s[i] = '0' + (p-(p/10)*10);
i--;
};
p = p/10;
};
s[j]='\0';
}
int main(){
int p;
char s[20];
printf("Enter P: ");
scanf("%d",&p);
even(p,s);
printf("\n\nSTRING: %s",s);
}
';
}
int main(){
int p;
char s[20];
printf("Enter P: ");
scanf("%d",&p);
even(p,s);
printf("\n\nSTRING: %s",s);
}
If the input is:
Input: 347892
The output is:
Output: 482
And I'd have to correct myself, my code would actually count 0 as an even number, but I wasn't tasked with dealing with zeroes as either even or odd numbers, but making a zero as an exception would be easy.
so
Input: 20002
would give the same output
Output: 20002
答案2
得分: 0
我认为你确实以困难的方式完成了这个任务。
以下是简化的方式:
#include <stdio.h>
int main(void) {
int input = 2346;
int base = 1;
while(input / base / 10) base*=10;
for(; input; input%=base, base/=10)
{
if ((input/base) && !(input / base % 2))
{
printf("%d", input/base);
}
}
return 0;
}
输出
Success #stdin #stdout 0s 4164KB
246
对于输入 2002
,输出是 22
(忽略了零,如描述所示)。
英文:
I think you really did that the hard way.
Here's the easy way:
#include <stdio.h>
int main(void) {
int input = 2346;
int base = 1;
while(input / base / 10) base*=10;
for(; input; input%=base, base/=10)
{
if ((input/base) && !(input / base % 2))
{
printf("%d", input/base);
}
}
return 0;
}
Output
Success #stdin #stdout 0s 4164KB
246
For Input 2002
, output is 22
(ignoring the zeros, as described)
答案3
得分: 0
以下是翻译好的部分:
"int" 类型的对象可以是负数或等于 0。您的函数不处理这种类型的对象。
这个循环
s[i] = 's[i] = '\0';
// 上面的部分从右到左截取数字并将其存储在字符串 S 中
j = 0;
while (i >= 0) {
s1[j] = s[i];
i--;
j++;
};
';
// 上面的部分从右到左截取数字并将其存储在字符串 S 中
j = 0;
while (i >= 0) {
s1[j] = s[i];
i--;
j++;
};
在字符数组 s1 的开头存储了终止零字符。因此,接下来的循环
i = 0;
while (s1[i] != 'i = 0;
while (s1[i] != '\0') {
s[i] = s1[i];
i++;
};
') {
s[i] = s1[i];
i++;
};
将不会被执行。
您可以使用标准的 C 函数 sprintf
来代替手动构建字符串。
另外,最好将整数的类型声明为 long long int
。在这种情况下,函数将能够处理任何标准整数类型。
以下是一个演示程序:
#include <stdio.h>
#include <limits.h>
char * extract_evens( char *s, long long int x )
{
const long long int Base = 10;
long long int multiplier = 0;
long long int y = 0;
do
{
long long int digit = x % Base;
if ( digit % 2 == 0 )
{
multiplier = multiplier == 0 ? 1 : multiplier * Base;
y = y + multiplier * digit;
}
} while ( x /= Base );
sprintf( s, "%lld", y );
return s;
}
int main(void)
{
char s[20];
extract_evens( s, 10001 );
printf( "%lld -> %s\n", 10001ll, s );
extract_evens( s, -10001 );
printf( "%lld -> %s\n", -10001ll, s );
extract_evens( s, LLONG_MAX );
printf( "%lld -> %s\n", LLONG_MAX, s );
extract_evens( s, LLONG_MIN );
printf( "%lld -> %s\n", LLONG_MIN, s );
return 0;
}
它的输出是:
10001 -> 0
-10001 -> 0
9223372036854775807 -> 222068480
-9223372036854775808 -> -2220684808
如果您不想包括零,那么可以将以下 if 语句
if ( digit % 2 == 0 )
替换为
if ( digit % 2 == 0 && digit != 0 )
如果您不能使用函数 sprintf
,那么函数的实现可以如下所示,如下面的演示程序所示。在此实现中,偶数位中排除了零。
#include <stdio.h>
#include <limits.h>
char * extract_evens( char *s, long long int x )
{
const long long int Base = 10;
long long int multiplier = 0;
long long int y = 0;
do
{
long long int digit = x % Base;
if ( digit % 2 == 0 && digit != 0 )
{
multiplier = multiplier == 0 ? 1 : multiplier * Base;
y = y + multiplier * digit;
}
} while ( x /= Base );
char *p = s;
if( y < 0 ) *p++ = '-';
else if ( y == 0 ) *p++ = '0';
for ( ; multiplier != 0; multiplier /= Base )
{
long long int digit = y / multiplier;
y %= multiplier;
if ( digit < 0 ) digit = -digit;
*p++ = digit + '0';
}
*p = '\0';
return s;
}
int main(void)
{
char s[20];
extract_evens( s, 10001 );
printf( "%lld -> %s\n", 10001ll, s );
extract_evens( s, -10001 );
printf( "%lld -> %s\n", -10001ll, s );
extract_evens( s, LLONG_MAX );
printf( "%lld -> %s\n", LLONG_MAX, s );
extract_evens( s, LLONG_MIN );
printf( "%lld -> %s\n", LLONG_MIN, s );
return 0;
}
该程序的输出是:
10001 ->
-10001 ->
9223372036854775807 -> 2226848
-9223372036854775808 -> -22268488
如果您希望在用户输入 0 时排除所有零,只需删除上一个演示程序中的以下语句:
else if ( y == 0 ) *p++ = '0';
在这种情况下,其输出将如下所示:
10001 ->
-10001 ->
9223372036854775807 -> 2226848
-9223372036854775808 -> -22268488
英文:
For starters objects of the type int
can be negative or equal to 0. Your function does not process such objects.
This loop
s[i]='\0';
//The part above cuts off digits from right to left and stores them in the string S
j=0;
while (i>=0){
s1[j] = s[i];
i--;
j++;
};
stores the terminating zero character in the beginning of the character array s1. So the next loop
i=0;
while(s1[i]!='\0'){
s[i] = s1[i];
i++;
};
will not be executed.
Instead of building the string manually you could use the standard C function sprintf
.
Also it is better to declare the type of the integer as long long int
. In this case the function will be able to process any standard integer type.
Here is a demonstrative program.
#include <stdio.h>
#include <limits.h>
char * extract_evens( char *s, long long int x )
{
const long long int Base = 10;
long long int multiplier = 0;
long long int y = 0;
do
{
long long int digit = x % Base;
if ( digit % 2 == 0 )
{
multiplier = multiplier == 0 ? 1 : multiplier * Base;
y = y + multiplier * digit;
}
} while ( x /= Base );
sprintf( s, "%lld", y );
return s;
}
int main(void)
{
char s[20];
extract_evens( s, 10001 );
printf( "%lld -> %s\n", 10001ll, s );
extract_evens( s, -10001 );
printf( "%lld -> %s\n", -10001ll, s );
extract_evens( s, LLONG_MAX );
printf( "%lld -> %s\n", LLONG_MAX, s );
extract_evens( s, LLONG_MIN );
printf( "%lld -> %s\n", LLONG_MIN, s );
return 0;
}
Its output is
10001 -> 0
-10001 -> 0
9223372036854775807 -> 222068480
-9223372036854775808 -> -2220684808
If you do not want include zeroes then substitute this if statemen
if ( digit % 2 == 0 )
for this
if ( digit % 2 == 0 && digit != 0 )
If you may not use the function sprintf
then the function implementation can look the following way as it is shown in the demonstrative program below. In this implementation zeroes are excluded from even digits.
#include <stdio.h>
#include <limits.h>
char * extract_evens( char *s, long long int x )
{
const long long int Base = 10;
long long int multiplier = 0;
long long int y = 0;
do
{
long long int digit = x % Base;
if ( digit % 2 == 0 && digit != 0 )
{
multiplier = multiplier == 0 ? 1 : multiplier * Base;
y = y + multiplier * digit;
}
} while ( x /= Base );
char *p = s;
if( y < 0 ) *p++ = '-';
else if ( y == 0 ) *p++ = '0';
for ( ; multiplier != 0; multiplier /= Base )
{
long long int digit = y / multiplier;
y %= multiplier;
if ( digit < 0 ) digit = -digit;
*p++ = digit + '0';
}
*p = '\0';
return s;
}
int main(void)
{
char s[20];
extract_evens( s, 10001 );
printf( "%lld -> %s\n", 10001ll, s );
extract_evens( s, -10001 );
printf( "%lld -> %s\n", -10001ll, s );
extract_evens( s, LLONG_MAX );
printf( "%lld -> %s\n", LLONG_MAX, s );
extract_evens( s, LLONG_MIN );
printf( "%lld -> %s\n", LLONG_MIN, s );
return 0;
}
The program output is
10001 -> 0
-10001 -> 0
9223372036854775807 -> 2226848
-9223372036854775808 -> -22268488
If you want to exclude all zeroes even when the user enters 0 then just remove this statement
else if ( y == 0 ) *p++ = '0';
in the previous demonstrative program.
In this case its output will look like
10001 ->
-10001 ->
9223372036854775807 -> 2226848
-9223372036854775808 -> -22268488
答案4
得分: 0
以下是翻译好的部分:
"它更容易、更快速、更简单,如果直接在输入上使用缓冲的过滤器。"
#include <stdio.h> /* fgets fputc ferror perror */
#include <stdlib.h> /* EXIT */
#include <string.h> /* strlen strchr */
int main(void) {
char buffer[256], *b, *b_end;
while(fgets(buffer, sizeof buffer, stdin))
for(b = buffer, b_end = b + strlen(b); b < b_end; b++)
if(strchr("2468\n", *b)) fputc(*b, stdout);
return ferror(stdin) ? (perror("stdin"), EXIT_FAILURE) : EXIT_SUCCESS;
}
"这也会处理格式错误的输入和没有额外工作的多行,(并具有错误检查。)但是,这假定您可以直接访问流作为字符设备;如果您有由其他函数输入的整数输入,这将无法正常工作。"
英文:
It's easier, faster, and simpler if one just employs a buffered filter directly on the input.
#include <stdio.h> /* fgets fputc ferror perror */
#include <stdlib.h> /* EXIT */
#include <string.h> /* strlen strchr */
int main(void) {
char buffer[256], *b, *b_end;
while(fgets(buffer, sizeof buffer, stdin))
for(b = buffer, b_end = b + strlen(b); b < b_end; b++)
if(strchr("2468\n", *b)) fputc(*b, stdout);
return ferror(stdin) ? (perror("stdin"), EXIT_FAILURE) : EXIT_SUCCESS;
}
This will also handle mis-formed input and multiple lines with no extra work, (and has error checking.) However, it assumes you have direct access to the stream as a character device; this doesn't work if you have an integer input by some other function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论