C++ 为什么CodeChef最近除数程序中出现错误?
我正在努力解决这个问题 我不明白为什么它给了我错误的解决方案 问题陈述是 给定两个整数A和B,求最大数≤A完全可以被B整除 输入: 输入由两行组成。 输入的第一行包含一个整数a。 输入的第二行包含一个整数B 输出: 打印最大整数≤在另一行中完全可被B整除的A 约束条件 1.≤B≤A.≤10^9 这是我的密码C++ 为什么CodeChef最近除数程序中出现错误?,c++,numbers,c++14,theory,number-theory,C++,Numbers,C++14,Theory,Number Theory,我正在努力解决这个问题 我不明白为什么它给了我错误的解决方案 问题陈述是 给定两个整数A和B,求最大数≤A完全可以被B整除 输入: 输入由两行组成。 输入的第一行包含一个整数a。 输入的第二行包含一个整数B 输出: 打印最大整数≤在另一行中完全可被B整除的A 约束条件 1.≤B≤A.≤10^9 这是我的密码 #include<bits/stdc++.h> #define ll long long using namespace std; void file_i_o() { i
#include<bits/stdc++.h>
#define ll long long
using namespace std;
void file_i_o() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("Input.txt","r",stdin);
freopen("Output.txt","w",stdout);
#endif
}
int main(int argc, char** argv) {
file_i_o();
//write your code here
ll A,B;
cin>>A>>B;
ll q = A/B;
ll n1 = B*q;
ll n2 = 0;
if(A*B > 0) {
n2 = B*(q+1);
} else {
n2 = B*(q-1);
}
if(abs(A-n1) < abs(A-n2)) {
if(n1<=A) {
cout<<n1;
}
} else {
if(n2<=A) {
cout<<n2;
}
}
return 0;
}
#包括
#定义ll long long
使用名称空间std;
作废文件{
ios_base::与_stdio同步(false);
cin.tie(空);
cout.tie(空);
#ifndef在线法官
freopen(“Input.txt”,“r”,stdin);
freopen(“Output.txt”,“w”,stdout);
#恩迪夫
}
intmain(intargc,字符**argv){
文件_i_o();
//在这里编写代码
ll A,B ;;
cin>>A>>B;
ll q=A/B;
ll n1=B*q;
ll n2=0;
如果(A*B>0){
n2=B*(q+1);
}否则{
n2=B*(q-1);
}
if(abs(A-n1) 如果(n1我认为你把问题复杂化了。我只是看看问题。它不需要那么复杂
这是我的解决方案(AC):
void solve(){
INTA,b;
读(a,b);
我不明白你为什么要写这么多代码来解决一件可以用两行代码完成的事情
这是解决方案
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
void file_i_o() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("Input.txt","r",stdin);
freopen("Output.txt","w",stdout);
#endif
}
int main(int argc, char** argv) {
file_i_o();
ll A,B;
cin>>A>>B;
ll ans = B*(A/B);
cout << ans;
return 0;
}
#包括
#定义ll long long int
使用名称空间std;
作废文件{
ios_base::与_stdio同步(false);
cin.tie(空);
cout.tie(空);
#ifndef在线法官
freopen(“Input.txt”,“r”,stdin);
freopen(“Output.txt”,“w”,stdout);
#恩迪夫
}
intmain(intargc,字符**argv){
文件_i_o();
ll A,B ;;
cin>>A>>B;
ll ans=B*(A/B);
你能试着用一些样本输入来运行它,看看你是否得到了正确的结果吗?是的,我检查了正数和负数,我也在极客中看到了这一点。是的,你的解决方案有效。谢谢如果你对答案满意,请将我的答案标记为接受。我没有任何声誉,所以我甚至不能投票或发表评论在其他人身上。
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
void file_i_o() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("Input.txt","r",stdin);
freopen("Output.txt","w",stdout);
#endif
}
int main(int argc, char** argv) {
file_i_o();
ll A,B;
cin>>A>>B;
ll ans = B*(A/B);
cout << ans;
return 0;
}