0% found this document useful (0 votes)
31 views

考試一 基礎c++

Uploaded by

B C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

考試一 基礎c++

Uploaded by

B C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

網站:ZeroJudge

編譯器:C++ Compiler
1. d483
2. a001
3. a002
4. b877
5. d063
6. a038
7. k194
8. c290
9. c294
10. c461
11. k731
12. a740
13. a148
14. b294
15. i399
16. c420
17. a215
18. a147
19. b964
20. c292
21. g488
22. C462
answer
d483✔
#include <iostream>
using namespace std;
int main()
{
cout<<"hello, world";

return 0;
}

A001✔
#include <iostream>
using namespace std;
int main()
{
string i;
cin>>i;
cout<<"hello, "<<i;

return 0;
}

A002✔
#include <iostream>
using namespace std;
int main()
{
int i,j=0;
cin>>i>>j;
cout<<i+j<<'\n';

return 0;
}

B877
#include <iostream>
using namespace std;
int main()
{
int a=0;
int b=0;
cin >> a >> b;
if(a>b){
cout<<a-b<<'\n';
}
else{
cout<<b-a<<'\n';
}

return 0;
}

d063✔
#include <iostream>
using namespace std;
int main()
{
int i=0;
cin>>i;
if(i==1){
cout<<"0";
}
else{
cout<<"1";
}

return 0;
}

A038
#include <bits/stdc++.h>
using namespace std;
#define int long long int
signed main()
{
string s;
cin>>s;
reverse(s.begin(),s.end());
int i= stoi(s);
cout<<i<<'\n';

return 0;
}

K194
#include <bits/stdc++.h>
using namespace std;
int main()
{
string ans;
cin>>ans;
int sum = 0;
for(int i=0;i<5;i++){
if(i==1){
sum+=16;
}
else if(i==2){
ans+=8;
}
else if(i==3){
ans+=4;
}
else if(i==4){
ans+=2;
}
else if(i==5){
ans+=1;
}
}
cout<<sum<<'\n';
return 0;
}

C290
#include <bits/stdc++.h>
using namespace std;
int main()
{
int s;
cin>>s;
int evensum=0;
int oddsum=0;
int position=1;
while(s>0){
int digit = s%10;
if(position%2==1){
oddsum += digit;
}
else{
evensum += digit;
}
s /=10;
position++;
}
if(evensum>=oddsum){
cout<<evensum-oddsum<<'\n';
}
else{
cout<<oddsum-evensum<<'\n';
}

return 0;
}

C294
#include <iostream>
using namespace std;
int main()
{
float a,b,c =0;
cin>>a>>b>>c;
if(a+b<=c){
cout<<"No"<<'\n';
}
else if(a*a+(b*b) < (c*c)){
cout<<"Obtuse"<<'\n';
}
else if(a*a+(b*b) == (c*c)){
cout<<"Right"<<'\n';
}
else if(a*a+(b*b) > (c*c)){
cout<<"Acute"<<'\n';
}
return 0;
}

c461
#include <iostream>
using namespace std;
int main()
{
int a,b,c=0;
cin>>a>>b>>c;
if(a==b && b==c){
cout<<"AND"<<'\n';
}
else if(a==b || b==c){
cout<<"OR"<<'\n';
}
else if(a!=b || b!=c){
cout<<"XOR"<<'\n';
}
else{
cout<<"IMPOSSIBLE"<<'\n';
}
return 0;
}

k731
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[i][j];
for(int i=0;i<n;i++){
for(int j=0;j<2;j++){
cin>>a[i][j];
}
}

return 0;
}

A740

A148
#include <iostream>
using namespace std;
int main()
{
float i[5];
cin>>i;
if((i[0]+i[1]+i[2]+i[3]+i[4])/5 >59){
cout<<"No";
}
else{
cout<<"Yes";
}
return 0;
}
B294
#include <iostream>
using namespace std;
int main()
{
int n=0;
int r=0;
int i=0;
cin>>n;
for(int i=1; i<=n; i++){
r=i;
}
cout<<r+i;
return 0;
}

(I399)

C420
#include <iostream>
using namespace std;
int main()
{
int n=0;
int i=0;
for(int n=i; n>0; n--){
cout<<" ";
}
for(int n=1; n>=1; n+=2){
cout<<"*";
}
for(int n=i; n>0; n--){
cout<<" ";
}
cout<<'\n';
return 0;
}

A215
#include <iostream>
using namespace std;
int main()
{
int n=0;
int m=0;
cin>>n>>m;
for(int n=0; n<m; n++){
if(n<m){
n=n++;
}
else{
cout<<n<<'\n';
}
}
return 0;
}

A147
#include <iostream>
using namespace std;
int main()
{
int n=0;
cin>>n;
for (int i = 1; i < n; i++) {
if (i % 7 != 0) {
cout << i << " ";
}
}
return 0;
}
b964

a015 -> c292

g488
#include <iostream>
using namespace std;
int main(){
int x=0;
int n=0;
cin>>x;
cout<<(n*(x-1))+(x*x-x)+1<<'\n';
return 0;
}
a693
W3Schools
https://www.w3schools.com/cpp/cpp_quiz.asp
Variable
超過pow(10,9) ⇒ long long int
超過long long int → string

Cin
String s;
getline(cin,s); →代替cin>> delete 空格
一次一行
有空格
不知道幾個,有空格

(getline)
vector<string/int…> v;
String token;
Stringstream ss(s);
while(getline(ss,token, ‘ ’)){
v.emplace_back(token);
}

/
Ceil 無條件進位
Round 四捨五入
floor 無條件捨去(original)

Minus一位數/取最後一位數
a/=10;
A %=10;

Find length
int len = s.length();
擷取string
String s2 = s1.substr(0,4);

Reverse

string
String s;
reverse(s.begin(),s.end());
abs()

Array
Array a ;
reverse(a,a+n);

Sort
sort(a,a+n);

Reverse apart
String “hello”;
reverse(s.begin()+1,s.begin()+4)
After cout → hlleo

Leave the loop


Break → 123_______…
Continue → 123_567…

Function
輸出類別 > start with “int”
Only take an action > start with “void”

int/void f(int x, int y){


Int ans = x-y;
return ans;
}
元素互換
String → int stoi
int → string to_string
Char → string “ ” + a
String → char a[0]

Pair

Def
pair<int,string> p;

呼叫
cout<<p.first<<p.second;

更改
p={p.first+1, p.second};

Double pair<>
pair<pair<int,int>int> p;
p.first.first
p.first,second
P.second

Vector
vector<int> v[5];
v.emplace_back(7); add
= v.push_back({1,3});
v[1].pop_back(); → delete 最後一行
Int l = v.size(); → find length
vector<int> v(5,10); → 10 10 10 10 10
fill(v.begin()+1, v.begin()+3,0);
sort(v.begin()+1,v.begin()+3,cmp);
reverse(v.begin()+1,v.begin()+3);
Vector → 都可以拿
Stack → 從後面拿
Queue → 從前面拿
Deque → 前後都可

Bfs 廣度

Dfs 深度

You might also like