欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 牛客 | OJ在线编程常见输入输出练习

牛客 | OJ在线编程常见输入输出练习

2025/4/22 8:22:58 来源:https://blog.csdn.net/qq_51870267/article/details/147362762  浏览:    关键词:牛客 | OJ在线编程常见输入输出练习

1.只有输出
言归正传,本张试卷总共包括18个题目,包括了笔试情况下的各种输入输出。
第一题不需要输入,仅需输出字符串 Hello Nowcoder! 即可通过。

#include <iostream>
using namespace std;
int main(){string s = "Hello Nowcoder!";cout << s << endl;return 0;
}

2.单组_A+B
在这里插入图片描述

#include <iostream>
using namespace std;
int main() {long long a, b;while(cin >> a >> b){cout << a + b << endl;}return 0;
}

3.多组_A+B_EOF形式
在这里插入图片描述

#include <iostream>
using namespace std;
int main(){long long a, b;while(cin >> a >> b){cout << a + b << endl;}return 0;
}

4.多组_A+B_T组形式
在这里插入图片描述

#include <iostream>
using namespace std;
int main(){int t;long long a, b;cin >> t;for(int i = 0; i < t; i++){cin >> a >> b;cout << a + b << endl;}return 0;
}

5.多组_A+B_零尾模式
在这里插入图片描述

#include <iostream>
using namespace std;
int main(){long long a, b;while(cin >> a >> b){if(a !=0 && b != 0){cout << a + b << endl;}else {break;}}return 0;
}

6.单组_一维数组
在这里插入图片描述

#include <iostream>
#include <vector>
using namespace std;
int main(){int n;cin >> n;long long res;vector<long long> nums(n);for(int i = 0; i < n; i++){cin >> nums[i];res += nums[i];}cout << res << endl;return 0;
}

7.多组_一维数组_T组形式
在这里插入图片描述

#include <iostream>
using namespace std;
int main(){int t;cin >> t;while(t--){int n;cin >> n;long long sum = 0;for(int i = 0; i < n; i++){long long a;cin >> a;sum += a;}cout << sum << endl;   }return 0;
}

8.单组_二维数组
在这里插入图片描述

#include <iostream>
using namespace std;
int main(){int n, m;cin >> n >> m;long long sum = 0, a;for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){cin >> a;sum += a;}}cout << sum << endl;return 0;
}

9.多组_二维数组_T组形式
在这里插入图片描述

#include <iostream>
using namespace std;
int main() {int t;cin >> t;while(t--){int n, m;cin >> n >> m;long long sum = 0, a;for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){cin >> a;sum += a;}}cout << sum << endl;}return 0;
}

10.单组_字符串
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main(){int n;cin >> n;string s;cin >> s;for(int i = 0; i < n/2; i++){swap(s[i], s[n - i - 1]);}cout << s << endl;return 0;
}

11.多组_字符串_T组形式
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main(){int t;cin >> t;while(t--){int n;cin >> n;string s;cin >> s;for(int i = 0; i < n/2; i++){swap(s[i], s[n - i - 1]);}cout << s << endl;}return 0;
}

12.单组_二维字符数组
在这里插入图片描述

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){int n, m;cin >> n >> m;vector<string> arr(n);for(int i = 0; i < n; i++){cin >> arr[i];}for(int i = 0; i < n/2; i++){swap(arr[i], arr[n - i - 1]);}for(int i = 0; i < n; i++){string row = arr[i];for(int j = 0; j < m/2; j++){swap(row[j], row[m - j -1]);}arr[i] = row;}for(const string& row:arr){cout << row << endl;}return 0;
}

13.多组_带空格的字符串_T组形式
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main(){int t;cin >> t;while(t--){int n;cin >> n;// 忽略空白字符,读取包含空格的整行字符cin.ignore();string s;getline(cin, s);string newS;for(char c : s){if(c != ' ') newS += c;}for(int i = 0; i < newS.size()/2; i++){swap(newS[i], newS[newS.size() - i - 1]);}cout << newS << endl;}return 0;
}

14.单组_保留小数位数
在这里插入图片描述

#include <iostream>
#include <iomanip> // 填充格式
using namespace std;
int main(){double a;cin >> a;cout << fixed << setprecision(3) << a << endl;return 0;
}

15.单组_补充前导零
在这里插入图片描述

#include <iostream>
#include <iomanip>
using namespace std;
int main(){long long a;cin >> a;cout << setw(9) << setfill('0') << a << endl;return 0;
}

16.单组_spj判断YES与NO
在这里插入图片描述

#include <iostream>
using namespace std;int main() {long long a;while(cin >> a){if(a % 2 == 1) cout << "YES" << endl;else cout << "NO" << endl;}return 0;
}
// 64 位输出请用 printf("%lld")

17.单组_spj判断浮点误差
在这里插入图片描述

#include <iostream>
#include <iomanip>
using namespace std;
const double pi = 3.14159265358979323846;
int main() {double r;cin >> r;double area = pi * r * r;cout << fixed << setprecision(6) << area << endl;return 0;
}
// 64 位输出请用 printf("%lld")

18.单组_spj判断数组之和
在这里插入图片描述
想复杂了 这道题就是一个输出就行了 没那么多复杂的思路

#include <iostream>
#include <vector>
using namepsace std;
int main() {long m, n;cin >> n >> m;vector<int> res(n);for(long i = 0; i < n - 1; i++){res[i] = 1;}res[n] = m - (n - 1);for(auto num : res){cout << num << " ";}cout << end;return 0;
}

几个关键点:

  1. 循环输入
while(cin >> a >> b){
}
  1. 固定输出
    (1)小数
    头文件 #include <iomanip>
    设置浮点数输出的精度 setprecision(),当与fixed结合使用时,n表示小数点后的位数。
    例如 cout << fixed << setprecision(3) << a << endl;
    (2)填充宽度
    设置输出字段宽度为n个字符:setw(n),
    设置填充字符为0:setfill('0'),当输出内容长度小于setw设置的宽度时,用0填充。
    例如 cout << setw(9) << setfill('0') << a << endl;

公司真题:https://www.nowcoder.com/exam/company?questionJobId=10&subTabName=written_page
专项练习https://www.nowcoder.com/exam/intelligent?questionJobId=10&subTabName=intelligent_page&tagId=273590

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词