跳到正文

算法·理论

如何写多组数据的题

chenshixian约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年9月27日
收录
2025年12月12日
更新
2026年8月20日
篇幅
约 1 分钟

版本

  1. v2当前
  2. v1

给定了数据组数

假设数据组数为tt组,那么可以这样写:

C++
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
      int n;
      cin>>n;
      cout<<n<<"\n";
    }
    return 0;
}

给了在输入某值时结束程序

假设如果输入00,则退出程序,可以这样写:

C++
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    while(cin>>n&&n){//也可以写n!=0
        cout<<n<<"\n";
    }
    return 0;
}

只说了有多组

可以这样写:

C++
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    while(cin>>n){
        cout<<n;
    }
    return 0;
}

会有人问了,怎么结束啊?你可以按Ctrl+G来结束,也可以直接关。

原文