当前位置: 首页 > news >正文

牛客 | 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

相关文章:

  • Java中订阅消费模式(发布-订阅模式)和观察者模式的区别
  • 2025年渗透测试面试题总结-拷打题库08(题目+回答)
  • Java8-遍历list取出两个字段重新组成list集合
  • FreeSWITCH 简单图形化界面41 - 批量SIP视频呼叫测试
  • SQL注入之information_schema表
  • 浅聊docker的联合文件系统
  • 【AI 加持下的 Python 编程实战 2_07】第七章:基于 Copilot 完整演示问题分解能力在实际问题中的落地应用
  • 从事计算机视觉需要掌握哪些知识
  • 面试题:循环引用两个节点相互引用,如何判断哪个用 shared_ptr?哪个用 weak_ptr?
  • Pytorch实战
  • 软件架构师的“天、人、术、势“:构建未来系统的哲学框架
  • Linux 下依赖库的问题
  • OV-Watch(一)(IAP_F411学习)
  • 【Part 2安卓原生360°VR播放器开发实战】第一节|通过传感器实现VR的3DOF效果
  • Milvus(1):什么是 Milvus
  • 21. git apply
  • 大模型技术解析与应用 | 大语言模型:从理论到实践(第2版)| 复旦大学 | 533页
  • 深度学习方向急出成果,是先广泛调研还是边做实验边优化?
  • springboot自动装配的原理
  • 修改PointLIO项目
  • 女子“伸腿阻拦高铁关门”被行拘,事件追踪:当时发生了什么?
  • 上海农房翻建为何难?基层盼政策适度松动
  • IPO周报|本周暂无新股申购,上周上市新股中签浮盈均超1.6万
  • 陈杨梅:为爸爸寻我19年没有放弃而感动,回家既紧张又期待
  • 上海古籍书店重新开卷,在这里淘旧书获新知
  • 大理州工业投资(集团)有限公司党委副书记、副总经理赵云接受审查调查