数组的不确定长度输入
0.前言
我们在学习数组的时候一般都会告诉你数组的长度,然后for循环去遍历。但是有一类问题是没有n的,也就是没有告诉长度的。
1.方法
第一种:(数组)
#include<iostream> #include<cstdio> using namespace std; int main() { int a[100]; int t=0; int n; while(scanf("%d",&n)!=EOF) { a[t]=n; t++; } for(int i=0; i<t; i++) { cout<<a[i]<<" "; } } //按ctrl+z 停止第二种:字符数组
#include<iostream> #include<cstdio> using namespace std; int main() { char str[10001]; //字符数组 while(cin>>str) { //不确定长度 } cout<<str; return 0; }第三种:(数组)
#include<cstdio> #include<iostream> using namespace std; int main(){ int arr[1001]; int k=0; while(cin>>arr[k]){ k++; } for(int i=0;i<k;i++){ cout<<arr[i]<<" "; } return 0; }.jztagtree{max-height:85vh;right:0px}.jzDown{top:10vh}.jztagtree li a{background-color:#448EF6}.jztagtree li a:before{border-right:10px solid #448EF6}.jztagtree li a:hover{background:#0045a6}.jztagtree li a:hover::before{border-right:10px solid #0045a6}
$("#jztoc").toc({content: ".single", headings: "h1,h2,h3"});