電子技術工作組第一次考核任務
阿新 • • 發佈:2018-11-10
任務一:資料插入
利用sort函式:
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
int b[15]={1,2,7,12,14,15,16,19,25,65,100};
const int t=11;
int v; //插入的數
scanf("%d",&v);
b[t]=v;
sort(b,b+(t+1));
for(int i=0;i<t+1;i++)
printf("%d\t",b[i]);
return 0;
}
#include<cstdio> int main(){ int b[15]={1,2,7,12,14,15,16,19,25,65,100}; const int t=11; int v; //插入的數 scanf("%d",&v); for(int i=t-1;i>=0;i--){ if(v>=b[t-1]){ //最後一位 b[t]=v; break; } if(b[i]<=v&&v<b[i+1]){ //中間 b[i+1]=v; break; } else b[i+1]=b[i]; } if(v<=b[1]){ //最前 b[0]=v; } for(int i=0;i<t+1;i++) printf("%d\t",b[i]); return 0; }
任務二:使用者登陸驗證
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define QQ "123456" #define PD "123" int flag=3; //機會 int main(){ string qq; //賬號 string pd; //密碼 while(flag){ printf("%s","請輸入賬號:"); cin>>qq; printf("%s","請輸入密碼:"); cin>>pd; if(qq==QQ&&pd==PD){ printf("恭喜!登陸成功。\n賬號:%s\n密碼:%s\n",QQ,PD); break; } else{ printf("登陸失敗。剩餘機會%d次\n",--flag); } } if(!flag) printf("\n登陸的機會已用完!"); return 0; }
任務三:紙牌遊戲
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int arr[53]; int main(){ memset(arr,1,sizeof(arr)); //arr全部元素賦值為1,代表正面朝上 for(int i=2;i<=52;i++){ for(int j=2;j<=52;j++){ if(j%i==0){ arr[j]=!arr[j]; } } } for(int i=1;i<=52;i++){ if(arr[i]) printf("%d\t",i); } return 0; }
任務四:矩陣運算
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a[100]={0,1,0,-1,1,1,-3};
/*表示矩陣 1 0 -1
1 1 -3 */
int b[100]={0,0,3,1,2,3,1};
/*表示矩陣 0 3
1 2
3 1 */
//矩陣A 2*3 矩陣B 3*2
int sum[100];
int main(){
int n=2,m=3;
int k=1,i=1,s_t=1,j,aa,t;
while(k<=n){
memset(sum,0,sizeof(sum));
for(j=1;j<=m*n;i++,j+=n){
for(aa=0;aa<n;aa++)
sum[aa]+=a[i]*b[j+aa];
}
for(t=0;t<n;t++)
printf("%d ",sum[t]);
puts("");
k++;
}
return 0;
}
任務五:大整數加法運算
參考:https://blog.csdn.net/qq_41138935/article/details/83759244
c++ 大整數類模板
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
struct BigInteger{
static const int BASE=100000000;
static const int WIDTH=8;
vector<int> s;
BigInteger(long long num=0){*this=num;} //建構函式
BigInteger operator = (long long num){ //賦值運算子
s.clear();
do{
s.push_back(num%BASE);
num/=BASE;
}while(num>0);
return *this;
}
BigInteger operator =(const string& str){ //賦值運算子
s.clear();
int x,len=(str.length()-1)/WIDTH+1;
for(int i=0;i<len;i++){
int end=str.length()-i*WIDTH;
int start=max(0,end-WIDTH);
sscanf(str.substr(start,end-start).c_str(),"%d",&x);
s.push_back(x);
}
return *this;
}
BigInteger operator + (const BigInteger& b) const{
BigInteger c;
c.s.clear();
for(int i=0,g=0;;i++){
if(g==0&&i>=s.size()&&i>=b.s.size()) break;
int x=g;
if(i<s.size()) x+=s[i];
if(i<b.s.size()) x+=b.s[i];
c.s.push_back(x % BASE);
g=x/BASE;
}
return c;
}
BigInteger operator += (const BigInteger& b){
*this=*this+b;
return *this;
}
};
//輸入與輸出
ostream& operator << (ostream &out,const BigInteger& x){
out << x.s.back();
for(int i=x.s.size()-2;i>=0;i--){
char buf[20];
sprintf(buf,"%08d",x.s[i]);
for(int j=0;j<strlen(buf);j++) out << buf[j];
}
return out;
}
istream& operator >> (istream &in,BigInteger& x){
string s;
if(!(in>>s)) return in;
x=s;
return in;
}
int main(){
BigInteger a,b,sum;
cin>>a>>b;
cout<<a+b<<endl;
return 0;
}