当前位置:网站首页>Several C language implementations
Several C language implementations
2022-06-30 06:36:00 【Oraer_】
root
Title Description
The number root can be obtained by adding up the numbers on each bit of a number . If the number you get is a single digit , Then this number is the number . If the result is two digits or more , Then add up these numbers . Go on like this , Until you get a single digit .
such as , about 24 Come on , hold 2 and 4 Add up to get 6, because 6 It's one digit. , therefore 6 yes 24 The number of roots . Another example 39, hold 3 and 9 Add up to get 12, because 12 Not a single digit , So we have to 1 and 2 Add up , Finally get 3, This is a single digit , therefore 3 yes 39 The number of roots .
Input :
A positive integer ( Less than 10 Of 10000 Power ).
Output :
A number , That is, input several roots of numbers .
The sample input :
24
Sample output :
6
Because the number is too big , So you can use character arrays to write .
The code is as follows :
#include<stdio.h>
int main()
{
char num[1001];
int s,i;
scanf("%s",num);
while(num[1]!='\0') // Jump out of the loop when there is only one digit left in the number , Output number
{
s=0;
i=0;
while(num[i]!='\0')
{
s=s+(num[i]-48); // Convert characters to numbers and add them up
i++;
}
i=0;
if(s==0)
break;
while(s!=0)
{
num[i++]=(s%10)+48; // Convert the added numbers into characters and store them in the array again
s=s/10;
}
num[i]='\0';
}
printf("%d\n",num[0]-48); // Convert the final result into digital output
return 0;
}
Main idea :
Convert the characters stored in the character array into numbers and add the numbers on each bit , Then it is converted into character array storage , Loop to the array with only one significant digit left , Convert it to digital output .
because 10 Of 10000 Power 9 Addition is also int Within type , So you don't have to use an array to store , After that, you can continue to find the remainder, round it and add it
Optimize :
#include<stdio.h>
int main()
{
char a[1010];
int n=0;
scanf("%s",a);
for(int i=0; a[i]!='\0'; i++)
n += a[i]-'0';
while(n>=10)
{
int m=0;
while(n!=0)
{
m += n%10;
n = n/10;
}
n=m;
}
printf("%d\n",n);
return 0;
}
边栏推荐
猜你喜欢

Force buckle ------ replace blank space

C语言:练习题三

1.3 - 码制

Talking about the struct of go

How does Altium designer hide some temporarily unnecessary classes, such as GND

图像处理7-图像增强

圖像處理7-圖像增强

Loading class `com. mysql. jdbc. Driver‘. This is deprecated. The new driver class is `com. mysql. cj. jdb

DHCP operation

C language final experiment report (student achievement management system) source code
随机推荐
New project folder based on PIO plug-in in vscode -- Interpretation
Software tools_ Shortcut_ Operation summary
Uniapp wechat applet returns to the previous page and refreshes
01. regular expression overview
1.2 (supplementary)
银河麒麟初体验
Installation and initialization of MariaDB database
Arrangement of in-depth learning materials
Initial love with mqtt
图像处理7-图像增强
Thread safe solutions, communication between threads (classic examples of producers and consumers)
Is Huatai stock trading safe? I want to open an account online.
VIM view file code
关于Glide加载图片模糊不清楚
Data read / write: realize data read / write function based on C # script in unity
ini解析學習文檔
Picture.....
Four ways to create multithreads
A small template (an abstract class, a complete process is written in a method, the uncertain part is written in the abstract method, and then the subclass inherits the abstract class, and the subclas
Rotate dimension tool rolabelimg