N
,判断是否为 9 的倍数及输出 nine-degree
N
直到 0 (最大到 1000 位)nine-degree
nine-degree
= 2N is a multiple of 9 and has 9-degree D.
N is not a multiple of 9.
while
回圈重复输入 N
,直到读进 0;因为数字可能很大,用字串方式读入存到字元阵列中
int main(){
char N[1001] = {0};
while(scanf("%s", N)){
if(N[0] == '0' && strlen(N) == 1){
break;
}
else{
...
}
}
return 0;
}
main()
处理好输出格式
printf("%s ", N);
int D = degree(N);
if(D){
printf("is a multiple of 9 and has 9-degree %d.", D);
}
else{
printf("is not a multiple of 9.");
}
printf("\n");
for
回圈遍历字元计算总和,只要 % 9 == 0
代表即为 9 的倍数,并更新次数;再透过 sprintf()
这个神奇的函式把整数转回字元阵列继续计算
int degree(char *str){
int i;
int multiple, D = 0;
if(strlen(str) == 1 && str[0] == '9'){
return 1;
}
while(strlen(str) >= 2){
multiple = 0;
for(i = 0; i < strlen(str); i++){
multiple = multiple + str[i] - '0';
}
if(multiple % 9 == 0){
D++;
}
sprintf(str, "%d", multiple);
}
return D;
}
#include<stdio.h>
#include<string.h>
int degree(char *str){
int i;
int multiple, D = 0;
if(strlen(str) == 1 && str[0] == '9'){
return 1;
}
while(strlen(str) >= 2){
multiple = 0;
for(i = 0; i < strlen(str); i++){
multiple = multiple + str[i] - '0';
}
if(multiple % 9 == 0){
D++;
}
sprintf(str, "%d", multiple);
}
return D;
}
int main(){
char N[1001] = {0};
while(scanf("%s", N)){
if(N[0] == '0' && strlen(N) == 1){
break;
}
else{
printf("%s ", N);
int D = degree(N);
if(D){
printf("is a multiple of 9 and has 9-degree %d.", D);
}
else{
printf("is not a multiple of 9.");
}
printf("\n");
}
memset(N, 0, sizeof(N));
}
return 0;
}
>>: Day 28 : 用於生产的机械学习 TensorFlow Extended (TFX) 介绍
Day4 接下来卡比要是着操作 LaserCannon,让他可以左右移动。 Input 在上个章节,...
在建构元中也有所谓公有(public)和私有(private)之分。截至目前为止,所提到的都是公有(...
那麽在前篇,我们已经安装建置好整个Python运行环境,接下来就可以学习如何编写简单的程序了! 以下...
我的小夥伴推荐我做贪吃蛇但吃的是我,於是就有了以下两种: 不是很完整的想法,希望大家可以从我的文字叙...
回顾一下昨天,我们已经能透过route+view让登入页面显示在画面上了,今天的任务呢,就是要把登入...