2 条题解

  • 0
    @ 2024-8-24 17:01:22

    #include <bits/stdc++.h> using namespace std; long long a,b,ans=1,cnt; int main(){ freopen("pow.in","r",stdin); freopen("pow.out","w",stdout); cin>>a>>b; for(int i=1;i<=b;i++){ ans*=a; if(ans>1000000000){ cout<<-1; return 0; } } cout<<ans; return 0; }

    • 0
      @ 2024-8-23 11:09:46

      F1

      这个题想必很多童鞋一拿到就想着用pow(a,b)来做,但是,想这么做的童鞋,请你想一想pow的特性有哪些他?好了,如果你想不起来,我帮你想。

      1.返回值是个doubleA!

      2.考没考虑pow可能会爆炸?

      所以pow用的好直接AcceptedAccepted,用不好那就是直接爆0。

      废话不说,上代码!

      #include <bits/stdc++.h>
      #include <iostream>
      using namespace std;
      
      int ret(int x,int y){
      	if(pow(x, y)>1e9) return -1;
      	else return (int)(pow(x, y));
      }
      
      int main(){
      	
      	freopen("pow.in","r",stdin);
      	freopen("pow.out","w",stdout);
      	ios::sync_with_stdio(false);
      	
      	int x, y;
      	cin >> x >> y;
      	cout << ret(x, y);
      	return 0;
      }
      

      F2

      既然用pow容易爆零,那有没有什么不用pow也能做到和pow一样的效果的代码呢?当然是有的。我们只需要使用for循环来一点一点的乘,只要n大于1e9,就直接把n赋值为-1,然后br(你懂的)

      AC code

      #include <bits/stdc++.h>
      #define ios ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
      #define LL long long int
      using namespace std;
      LL n=1,a,b;
      int main(){
      	ios;
      	freopen("pow.in","r",stdin);
      	freopen("pow.out","w",stdout);
      	cin>>a>>b;
      	for(int i=1;i<=b;i++){
      		n*=a;
      		if(n>1e9){
      			n=-1;
      			break;
      		}
      	}
      	cout<<n;
      	return 0;
      }
      

      注意,CSP-J这里直接卡Time,稍不注意就有可能会 (因为我递交的时候时间是1001s)

      • 1

      信息

      ID
      623
      时间
      1000ms
      内存
      512MiB
      难度
      8
      标签
      递交数
      170
      已通过
      29
      上传者