2017级算法第一次上机-C.芸茹的课堂测试

Horner Ruler 霍纳规则没什么好说的 要注意的更多的还是细节的问题

取模运算  

循环的边界问题

#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const int maxlen = 1e6 + 10;
const int mod = 1e9 + 7;
    char str[maxlen];
long long Horner(char str[]){
    int len=strlen(str);
    long long i,j,k,ans;
    ans=0;
    for(i=0;i<len-1;i++){ 
        ans=( ((ans%mod )+ ((str[i]-'0')%mod)) % mod ) % mod  * 8 % mod ;
    }
    ans= ( (ans % mod ) + ( (str[i]-'0') % mod) ) % mod;
    return ans;
}
int main(){
    int n;
    scanf("%d",&n);
    while(n--){
        scanf("%s",str);
        printf("%lld\n",Horner(str));
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/visper/p/10100138.html