当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

基础刷题(一)

Introduction to neural networks

DHCP operation

【微信小程序:单选、多选样式,背景色,圆角】

Installation and initialization of MariaDB database

程序猿入门攻略(十一)——结构体

Zibll sub theme v6.4.1wordpress open source download_ Crack the original / use it directly / no tutorial required

Use and principle of completionservice (source code analysis)

Centos8 install redis

File operation io-part1
随机推荐
46. full arrangement -dfs double hundred code
Four ways to create multithreads
When to use redis
Picture.....
Unclear about glide loading picture
Rotate dimension tool rolabelimg
Ten years' miscellaneous thoughts
神经网络入门
Common address collection
First experience of Galaxy Kirin
c# - C#用fo-dicom对CT图像的PixelData进行处理和转换
uniapp 微信小程序返回上一页并刷新
Base64详解:玩转图片Base64编码
Record a problem tracking of excessive load
1.3 - 码制
Data read / write: realize data read / write function based on C # script in unity
HCIA day 1
C language final experiment report (student achievement management system) source code
1.5 - logical operation
Decompilation normal decompilation problems. Solve them yourself