请分析程序流程,填写划线部分缺少的代码。
答案 if(r>0) return i;
代码:
#include<iostream> #include<stdlib.h> #include<stdio.h> #include<cmath> #include<algorithm> #include<string> #include<string.h> #include<set> #include<queue> #include<stack> #include<functional> const int maxn = 10000 + 10; using namespace std; int ge_wei(int a) { if (a % 2 == 0) return (a * 2) % 10; else return (a * 2 + 5) % 10; } //计算进位 int jin_wei(char* p) { char* level[] = { "142857", "285714", "428571", "571428", "714285", "857142" }; char buf[7]; buf[6] = '\0'; strncpy(buf, p, 6); int i; for (i = 5; i >= 0; i--) { int r = strcmp(level[i], buf); if (r<0) return i + 1; while (r == 0) { p += 6; strncpy(buf, p, 6); r = strcmp(level[i], buf); if (r<0) return i + 1; ______________________________; //填空 //答案 if(r>0) return i; } } return 0; } //多位数乘以7 void f(char* s) { int head = jin_wei(s); if (head > 0) printf("%d", head); char* p = s; while (*p) { int a = (*p - '0'); int x = (ge_wei(a) + jin_wei(p + 1)) % 10; printf("%d", x); p++; } printf("\n"); } int main() { f("428571428571"); f("34553834937543"); return 0; }
打印图形 小明在X星球的城堡中发现了如下图形和文字: rank=3 * * * * * * * * * rank=5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ran=6 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 小明开动脑筋,编写了如下的程序,实现该图形的打印。
答案 f(a, rank - 1 , row , col + w / 2);
a和rank-1比较好想,后面基本靠蒙和猜了,这个题递归我感觉不是很容易理解
#include<iostream> #include<stdlib.h> #include<stdio.h> #include<cmath> #include<algorithm> #include<string> #include<string.h> #include<set> #include<queue> #include<stack> #include<functional> const int maxn = 10000 + 10; using namespace std; #define N 70 void f(char a[][N], int rank, int row, int col) { if (rank == 1) { a[row][col] = '*'; return; } int w = 1; int i; for (i = 0; i<rank - 1; i++) w *= 2; // cout << rank << " " << row << " " << col << " " << w << endl; ____________________________________________; //答案 f(a, rank - 1 , row , col + w / 2); f(a, rank - 1, row + w / 2, col); //递归 f(a, rank - 1, row + w / 2, col + w); } int main() { char a[N][N]; int i, j; for (i = 0; i<N; i++) //初始化 for (j = 0; j<N; j++) a[i][j] = ' '; f(a, 6, 0, 0); //数组赋值 for (i = 0; i<N; i++) { //打印 for (j = 0; j<N; j++) printf("%c", a[i][j]); printf("\n"); } return 0; }
