史丰收速算法的革命性贡献是:从高位算起,预测进位。不需要九九表,彻底颠覆了传统手算!
速算的核心基础是:1位数乘以多位数的乘法。
其中,乘以7是最复杂的,就以它为例。
因为,1/7 是个循环小数:0.142857...,如果多位数超过 142857...,就要进1
同理,2/7, 3/7, ... 6/7 也都是类似的循环小数,多位数超过 n/7,就要进n
下面的程序模拟了史丰收速算法中乘以7的运算过程。
乘以 7 的个位规律是:偶数乘以2,奇数乘以2再加5,都只取个位。
乘以 7 的进位规律是:
满 142857... 进1,
满 285714... 进2,
满 428571... 进3,
满 571428... 进4,
满 714285... 进5,
满 857142... 进6
请分析程序流程,填写划线部分缺少的代码。
答案 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;
}