牛客网----Wannafly挑战赛9

https://www.nowcoder.net/acm/contest/71#question

https://www.nowcoder.net/acm/contest/71/A

给定n个正整数,请找出其中有多少个数x满足:在这n个数中存在数y=kx,其中k为大于1的整数

本题是类似于素数筛法的算法,代码比较容易理解

#include<cstring>
#include<iostream>
using namespace std;
const int len = 1e6;
int ma1[len + 1], in[len];
int main() {
    std::ios::sync_with_stdio ( false );
    cin.tie ( 0 );
    int n;
    memset ( ma1, 0, sizeof ma1 );
    while ( cin >> n ) {
        int Max = 0;
        for ( int i = 0; i < n; i++ ) {
            cin >> in[i];
            Max = max ( in[i], Max );
            ma1[in[i]] = 1;
        }
        int ans = 0;
        for ( int i = 0; i < n; i++ ) {
            if ( in[i] == 1 ) {
                ans++;
                continue;
            }
            for ( int j = 2 * in[i]; j <= Max; j += in[i] )
                if ( ma1[j] == 1 ) {
                    ans++;
                    break;
                }
        }
        cout << ans << endl;
    }
    return 0;
}

https://www.nowcoder.net/acm/contest/71/C

小W在计算一个数列{An},其中A1=1,A2=2,An+2=An+1+An。尽管他计算非常精准,但很快他就弄混了自己的草稿纸,他找出了一些他计算的结果,但他忘记了这些都是数列中的第几项。

本题python做起来比较方便,我选择了c++。而单纯的使用大数加法,会因为字符串的增长而导致超时,因此截取了末40位。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<cstdio>
using namespace std;
void  sum ( string &a, string b ) {
    reverse ( a.begin(), a.end() );
    a.push_back ( '0' );
    a.push_back ( '0' );
    string::iterator it1 = a.begin(), it2 = b.end() - 1;
    for ( ; it2 >= b.begin() ; it1++, it2-- ) {
        ( *it1 ) += ( *it2 ) - 48;
        if ( ( *it1 ) >= 58 ) {
            ( * ( it1 + 1 ) ) += ( ( *it1 ) - 48 ) / 10 ;
            ( *it1 ) = ( ( *it1 ) - 48 ) % 10 + 48;
        }
    }
    if ( ( * ( a.end() - 1 ) ) == '0' ) a.erase ( a.end() - 1 );
    if ( ( * ( a.end() - 1 ) ) == '0' ) a.erase ( a.end() - 1 );
    while ( a.size() > 40 )
        a.erase ( a.end() - 1 );
    reverse ( a.begin(), a.end() );
}
int main() {
    map<string, int>ma1;
    string s[2] = {"1", "2"};
    ma1[s[0]] = 1;
    ma1[s[1]] = 2;
    for ( int i = 0; i <= 100000; i++ ) {
        sum ( s[i % 2], s[ ( i + 1 ) % 2] );
        ma1[s[i % 2]] = i + 3;
    }
    while ( cin >> s[0] ) {
        if ( s[0].size() > 40 )
            cout << ma1[ s[0].substr ( s[0].size() - 40, 40 )] << endl;
        else
            cout << ma1[s[0]] << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值